Skip to content

Commit a160b19

Browse files
committed
Add workflow to generate wp-versions data
1 parent e2ef28d commit a160b19

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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+
sleep $BACKOFF
75+
BACKOFF=$((BACKOFF * 2))
76+
done
77+
78+
- name: Track modified files
79+
id: version-file
80+
run: |
81+
IS_MODIFIED=$(git ls-files --modified wp-versions.json)
82+
if [ -n "$IS_MODIFIED" ]; then
83+
DIFF=$(git diff ${{ env.ARTIFACTS_BRANCH }} -- $FILE_PATH)
84+
echo "# Modified WP Versions Data" >> $GITHUB_STEP_SUMMARY
85+
echo "<details><summary>WP Versions Data Diff</summary>" >> $GITHUB_STEP_SUMMARY
86+
echo "" >> $GITHUB_STEP_SUMMARY
87+
echo "\`\`\`diff" >> $GITHUB_STEP_SUMMARY
88+
echo "$DIFF" >> $GITHUB_STEP_SUMMARY
89+
echo "\`\`\`" >> $GITHUB_STEP_SUMMARY
90+
echo "</details>" >> $GITHUB_STEP_SUMMARY
91+
echo "modified=true" >> $GITHUB_OUTPUT
92+
else
93+
echo "::notice::WP versions data has not been modified."
94+
exit 0
95+
fi
96+
97+
- name: Set git user
98+
if: steps.version-file.outputs.modified == 'true'
99+
run: |
100+
# See: https://api.github.com/users/github-actions[bot]
101+
git config --global user.name "github-actions[bot]"
102+
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
103+
104+
- name: Commit WP Versions Data
105+
if: steps.version-file.outputs.modified == 'true'
106+
run: |
107+
git add $FILE_PATH
108+
git commit -m "Update $FILE_PATH on $(date -u)" --signoff
109+
110+
- name: Push changes
111+
if: steps.version-file.outputs.modified == 'true'
112+
run: |
113+
git push origin ${{ env.FEATURE_BRANCH }}
114+
115+
- name: Create Pull Request
116+
run: |
117+
body="## Summary
118+
Update WP versions data file with the \`${WP_ORG_API_URL}\` endpoint response."
119+
delimiter="${body//$'\n'/'%0A'}"
120+
echo "body<<${delimiter}" >> $GITHUB_OUTPUT
121+
echo "$body" >> $GITHUB_OUTPUT
122+
echo "${delimiter}" >> $GITHUB_OUTPUT
123+
gh pr create --base ${{ env.ARTIFACTS_BRANCH }} --head ${{ env.FEATURE_BRANCH }} --title "Update WP Versions Data" --body "$body" --label "scope:artifacts" || true
124+
env:
125+
GH_TOKEN: ${{ github.token }}

0 commit comments

Comments
 (0)