|
| 1 | +--- |
| 2 | +name: WP Versions Data Fetcher |
| 3 | + |
| 4 | +on: |
| 5 | + schedule: |
| 6 | + - cron: '0 5 * * *' |
| 7 | + workflow_dispatch: |
| 8 | + inputs: |
| 9 | + max_retries: |
| 10 | + description: 'Max retries to fetch data from WPOrg API' |
| 11 | + default: 3 |
| 12 | + required: false |
| 13 | + type: number |
| 14 | + |
| 15 | +concurrency: |
| 16 | + group: ${{ github.repository }}-${{ github.workflow }} |
| 17 | + cancel-in-progress: true |
| 18 | + |
| 19 | +permissions: {} |
| 20 | + |
| 21 | +env: |
| 22 | + ARTIFACTS_BRANCH: 'artifacts' |
| 23 | + FEATURE_BRANCH: 'sync-wp-versions' |
| 24 | + FILE_PATH: 'wp-versions.json' |
| 25 | + MAX_RETRIES: ${{ inputs.max_retries || 3 }} |
| 26 | + WP_ORG_API_URL: 'https://api.wordpress.org/core/stable-check/1.0/' |
| 27 | + |
| 28 | +jobs: |
| 29 | + fetch-versions: |
| 30 | + runs-on: ubuntu-latest |
| 31 | + timeout-minutes: 10 |
| 32 | + permissions: |
| 33 | + contents: write |
| 34 | + pull-requests: write |
| 35 | + steps: |
| 36 | + - name: Checkout repository |
| 37 | + uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.1 |
| 38 | + with: |
| 39 | + ref: ${{ env.ARTIFACTS_BRANCH }} |
| 40 | + |
| 41 | + - name: Check if feature branch exists |
| 42 | + id: remote-branch |
| 43 | + run: | |
| 44 | + echo "exists=$([[ -z $(git ls-remote --heads origin ${{ env.FEATURE_BRANCH }}) ]] && echo "0" || echo "1")" >> $GITHUB_OUTPUT |
| 45 | +
|
| 46 | + - name: Create feature branch |
| 47 | + if: steps.remote-branch.outputs.exists == '0' |
| 48 | + run: | |
| 49 | + git checkout -b ${{ env.FEATURE_BRANCH }} |
| 50 | +
|
| 51 | + - name: Checkout feature branch |
| 52 | + if: steps.remote-branch.outputs.exists == '1' |
| 53 | + run: | |
| 54 | + git fetch --all --prune |
| 55 | + git checkout ${{ env.FEATURE_BRANCH }} |
| 56 | + git pull --no-rebase |
| 57 | +
|
| 58 | + - name: Fetch WP Versions |
| 59 | + run: | |
| 60 | + BACKOFF=10 |
| 61 | + for i in $(seq 1 $MAX_RETRIES); do |
| 62 | + echo "Fetching WP versions, attempt $i" |
| 63 | + RES_CODE=$(curl -sL -w "%{http_code}" $WP_ORG_API_URL -o /tmp/wp-versions.json) |
| 64 | + if [ $RES_CODE -eq 200 ]; then |
| 65 | + mv /tmp/wp-versions.json $FILE_PATH |
| 66 | + echo "::notice::WP versions data has been fetched successfully." |
| 67 | + break |
| 68 | + fi |
| 69 | + echo "Failed to fetch WP versions, attempt $i" |
| 70 | + if [ $i -eq $MAX_RETRIES ]; then |
| 71 | + echo "::error::Failed to fetch WP versions from $WP_ORG_API_URL after $MAX_RETRIES attempts" |
| 72 | + exit 1 |
| 73 | + fi |
| 74 | + echo "Retrying in $BACKOFF seconds" |
| 75 | + sleep $BACKOFF |
| 76 | + BACKOFF=$((BACKOFF * 2)) |
| 77 | + done |
| 78 | +
|
| 79 | + - name: Track modified files |
| 80 | + id: version-file |
| 81 | + run: | |
| 82 | + IS_MODIFIED=$(git ls-files --modified wp-versions.json) |
| 83 | + if [ -n "$IS_MODIFIED" ]; then |
| 84 | + DIFF=$(git diff ${{ env.ARTIFACTS_BRANCH }} -- $FILE_PATH) |
| 85 | + echo "# Modified WP Versions Data" >> $GITHUB_STEP_SUMMARY |
| 86 | + echo "<details><summary>WP Versions Data Diff</summary>" >> $GITHUB_STEP_SUMMARY |
| 87 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 88 | + echo "\`\`\`diff" >> $GITHUB_STEP_SUMMARY |
| 89 | + echo "$DIFF" >> $GITHUB_STEP_SUMMARY |
| 90 | + echo "\`\`\`" >> $GITHUB_STEP_SUMMARY |
| 91 | + echo "</details>" >> $GITHUB_STEP_SUMMARY |
| 92 | + echo "modified=true" >> $GITHUB_OUTPUT |
| 93 | + else |
| 94 | + echo "::notice::WP versions data has not been modified." |
| 95 | + exit 0 |
| 96 | + fi |
| 97 | +
|
| 98 | + - name: Set git user |
| 99 | + if: steps.version-file.outputs.modified == 'true' |
| 100 | + run: | |
| 101 | + # See: https://api.github.com/users/github-actions[bot] |
| 102 | + git config --global user.name "github-actions[bot]" |
| 103 | + git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 104 | +
|
| 105 | + - name: Commit WP Versions Data |
| 106 | + if: steps.version-file.outputs.modified == 'true' |
| 107 | + run: | |
| 108 | + git add $FILE_PATH |
| 109 | + git commit -m "Update $FILE_PATH on $(date -u)" --signoff |
| 110 | +
|
| 111 | + - name: Push changes |
| 112 | + if: steps.version-file.outputs.modified == 'true' |
| 113 | + run: | |
| 114 | + git push origin ${{ env.FEATURE_BRANCH }} |
| 115 | +
|
| 116 | + - name: Create Pull Request |
| 117 | + run: | |
| 118 | + body="## Summary |
| 119 | + Update WP versions data file with the \`${WP_ORG_API_URL}\` endpoint response." |
| 120 | + delimiter="${body//$'\n'/'%0A'}" |
| 121 | + echo "body<<${delimiter}" >> $GITHUB_OUTPUT |
| 122 | + echo "$body" >> $GITHUB_OUTPUT |
| 123 | + echo "${delimiter}" >> $GITHUB_OUTPUT |
| 124 | + gh pr create --base ${{ env.ARTIFACTS_BRANCH }} --head ${{ env.FEATURE_BRANCH }} --title "Update WP Versions Data" --body "$body" --label "scope:artifacts" || true |
| 125 | + env: |
| 126 | + GH_TOKEN: ${{ github.token }} |
0 commit comments