-
Notifications
You must be signed in to change notification settings - Fork 6
186 lines (166 loc) · 7.34 KB
/
gutenberg-packages-update.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: Gutenberg packages update
on:
# Allow for the workflow to be manually run if needed.
workflow_dispatch:
schedule:
# Once a day (https://crontab.guru/once-a-day)
- cron: '0 0 * * *'
permissions:
pull-requests: write
contents: write
# Cancel previous workflow run groups that have not completed.
concurrency:
# Group workflow runs by workflow name, along with the head branch ref of the pull request
# or otherwise the branch or tag ref.
group: ${{ github.workflow }}-${{ github.event_name == 'pull_request' && github.head_ref || github.ref }}
cancel-in-progress: true
jobs:
check-gutenberg-release:
name: Check for a new Gutenberg release
runs-on: ubuntu-latest
outputs:
latest-version: ${{ steps.latest-release.outputs.version }}
should-update: ${{ steps.release-status.outputs.outdated }}
steps:
- name: Get latest release version
id: latest-release
run: echo "::set-output name=version::$(gh api -X GET repos/wordpress/gutenberg/releases/latest --jq '.name')"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Get release version from last PR
id: last-release
run: |
PR_TITLE=$(gh api -X GET search/issues -f q='${{ env.QUERY }}' -f sort='created' -f order='desc' --jq '.items.[0].title')
LAST_VERSION=$(sed -r 's/.+ v(.+) .+/\1/' <<< "$PR_TITLE")
if ! egrep -q '^[0-9][0-9]*(\.[0-9][0-9]*)*$' <<< "$LAST_VERSION"; then
LAST_VERSION='0.0.0'
fi
echo "::set-output name=version::$(echo "$LAST_VERSION")"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
QUERY: 'repo:rtCamp/theme-elementary is:pr author:rtBot is:merged in:title Update Gutenberg packages after'
- name: Determine if package updates are needed
id: release-status
run: |
echo "Last version: $LAST_VER"
echo "Latest version: $LATEST_VER"
echo "::set-output name=outdated::$(php -r 'echo json_encode(version_compare($argv[1], $argv[2], ">"));' "$LATEST_VER" "$LAST_VER")"
env:
LAST_VER: ${{ steps.last-release.outputs.version }}
LATEST_VER: ${{ steps.latest-release.outputs.version }}
close-latest-pr:
name: Close latest open PR if one exists
# Run job if there is a new Gutenberg release.
if: needs.check-gutenberg-release.outputs.should-update == 'true'
runs-on: ubuntu-latest
needs: check-gutenberg-release
steps:
- name: Get latest open PR
id: latest-pr
run: |
PR_NUM=$(gh api -X GET search/issues -f q='${{ env.QUERY }}' -f sort='created' -f order='desc' --jq '.items.[0].number')
echo "::set-output name=num::$(echo $PR_NUM)"
echo "Latest PR number: ${PR_NUM}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
QUERY: 'repo:rtCamp/theme-elementary is:pr author:rtBot is:open in:title Update Gutenberg packages after'
# Needed to later close PR.
- name: Checkout repo
if: steps.latest-pr.num != ''
uses: actions/checkout@v3
- name: Close latest open PR
if: steps.latest-pr.num != ''
run: gh pr close ${{ env.PR_NUM }} --delete-branch
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUM: ${{ steps.latest-pr.num }}
update-packages:
name: Update Gutenberg npm dependencies
# Run job if there is a new Gutenberg release.
if: needs.check-gutenberg-release.outputs.should-update == 'true'
runs-on: ubuntu-latest
needs: check-gutenberg-release
steps:
- name: Checkout
uses: actions/checkout@v3
with:
# Fetch history for all branches and tags to allow for successful merge of base branch if needed.
fetch-depth: 0
- name: Determine branch name
id: branches
run: |
echo "::set-output name=base::$(echo ${GITHUB_REF#refs/heads/})"
echo "::set-output name=head::$(echo "update/gutenberg-v$VERSION-packages")"
env:
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }}
- name: Setup Node
uses: actions/[email protected]
with:
node-version-file: '.nvmrc'
cache: npm
- name: Configure git user
run: |
git config user.name rtBot
git config user.email '[email protected]'
- name: Check if remote branch exists
id: remote-branch
run: echo ::set-output name=exists::$([[ -z $(git ls-remote --heads origin "$HEAD_BRANCH" ) ]] && echo "0" || echo "1")
env:
HEAD_BRANCH: ${{ steps.branches.outputs.head }}
- name: Create branch to base pull request on
if: steps.remote-branch.outputs.exists == 0
run: git checkout -b "$HEAD_BRANCH"
env:
HEAD_BRANCH: ${{ steps.branches.outputs.head }}
- name: Fetch existing branch to add commits to
if: steps.remote-branch.outputs.exists == 1
run: |
git checkout "$HEAD_BRANCH"
git merge --no-edit "$BASE_BRANCH"
env:
BASE_BRANCH: ${{ steps.branches.outputs.base }}
HEAD_BRANCH: ${{ steps.branches.outputs.head }}
- name: Install Node dependencies
run: npm ci --ignore-scripts
env:
CI: true
- name: Check package updates
id: packages
run: |
# Get list of latest package versions.
PACKAGES=$(npm outdated --parseable | cut -d':' -f 4 | grep @wordpress | paste -s -d' ' || echo 0)
echo "::set-output name=list::$(echo "$PACKAGES")"
- name: Update packages
if: steps.packages.outputs.list != 0
run: npm i $(echo "$PACKAGES")
env:
PACKAGES: ${{ steps.packages.outputs.list }}
- name: Commit and push changes
if: steps.packages.outputs.list != 0
run: |
git add --all .
git commit -m "Update WordPress NPM packages based on Gutenberg release v$VERSION" -m "Signed-off-by: $(git config user.name) <$(git config user.email)>"
git push -u origin "$HEAD_BRANCH"
env:
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }}
HEAD_BRANCH: ${{ steps.branches.outputs.head }}
- name: Create a pull request
id: pr-create
if: steps.packages.outputs.list != 0 && steps.remote-branch.outputs.exists == 0
run: |
PR_BODY=$(node .github/bin/gutenberg-packages-update-pr-body.js "${{ steps.packages.outputs.list }}")
PR_URL=$(gh pr create --base "$BASE_BRANCH" --title "Update Gutenberg packages after v$VERSION release" --body "$PR_BODY" --label Dependencies --label NPM | grep https://)
echo "::set-output name=pr-url::$PR_URL"
env:
VERSION: ${{ needs.check-gutenberg-release.outputs.latest-version }}
GITHUB_TOKEN: ${{secrets.GH_BOT_TOKEN}}
BASE_BRANCH: ${{ steps.branches.outputs.base }}
HEAD_BRANCH: ${{ steps.branches.outputs.head }}
- name: Add PR reviewers
if: steps.pr-create.outputs.pr-url != ''
# Add a PR reviewer so that a notification can be sent to the concerned person.
run: gh pr edit "$PR_URL" --add-reviewer "$REVIEWER_USERNAME"
env:
GITHUB_TOKEN: ${{ secrets.GH_BOT_TOKEN }}
PR_URL: ${{ steps.pr-create.outputs.pr-url }}
REVIEWER_USERNAME: thelovekesh