diff --git a/.github/workflows/cleanup-task.yml b/.github/workflows/cleanup-task.yml new file mode 100644 index 0000000..29b640f --- /dev/null +++ b/.github/workflows/cleanup-task.yml @@ -0,0 +1,99 @@ +--- +name: Cleanup Inactive Server Files +on: + schedule: + - cron: '0 6 * * *' # Every day at 6 + workflow_dispatch: + +permissions: + contents: write + pull-requests: write + +jobs: + cleanup-files: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v7 + - name: Read filenames in server dir + id: file_path + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + # this creates a multiline string, the response gets truncated otherwise + run: | + { + echo 'server_domains<> "$GITHUB_OUTPUT" + - name: echo server-domains + run: echo "${{ steps.file_path.outputs.server_domains }}" + # when active servers are not empty the response gets formatted + - name: Request active server files + id: request + run: | + ACTIVE_SERVERS=$(curl -L "http://api.joinmastodon.org/active-servers/" ) + + if [ -n "$ACTIVE_SERVERS" ]; then + formatted=$(echo $ACTIVE_SERVERS | tr '[",]' ' ') + echo "active_servers=$formatted" >> $GITHUB_OUTPUT + else + echo "no active servers found" + exit 1 + fi + - name: echo active servers + run: echo "${{ steps.request.outputs.active_servers }}" + - name: Compare active servers with server files + id: files + # filter out which items from the server domains list are missing from the active server list + run: | + ACTIVE_SERVERS="${{ steps.request.outputs.active_servers }}" + DOMAINS="${{ steps.file_path.outputs.server_domains }}" + diff=$(grep -vxf <(echo "$ACTIVE_SERVERS" | tr ' ' '\n') <(echo "$DOMAINS" | tr ' ' '\n') || true) + MISSING=$(echo "$diff" | tr '\n' ' ') + echo "missing_domains=$MISSING" >> $GITHUB_OUTPUT + + if [ -z "$MISSING" ]; then + echo "all elements from server domains are in ACTIVE_SERVERS" + else + echo "missing elements $MISSING" + fi + shell: bash + - name: echo missing-domains + run: echo "${{ steps.files.outputs.missing_domains }}" + # build paths for the missing domains and delete them if the file can be found + # the removal is currently disabled to keep the server files + - name: Remove file of inactive server + if: ${{ steps.files.outputs.missing_domains != '' }} + run: | + MISSING="${{ steps.files.outputs.missing_domains }}" + + for domain in $MISSING; do + server_path="./servers/$domain.yaml" + default_server_path="./default_servers/$domain.yaml" + if [ -f $server_path ]; then + rm $server_path + echo "$server_path removed." + if [ -f $default_server_path ]; then + rm $default_server_path + echo "$default_server_path removed." + fi + elif [ -f $default_server_path ]; then + rm $default_server_path + echo "$default_server_path removed." + else + echo "$server_path and $default_server_path not fund." + exit 1 + fi + done + shell: bash + # Create or update the pull request + - name: Create Pull Request + uses: peter-evans/create-pull-request@v8 + with: + commit-message: Remove inactive server files + title: Cleanup Server Files (automated) + author: GitHub Actions + body: Remove inactive server files, automated with GitHub Actions + branch: cleanup-servers + base: main