forked from MetaMask/core
-
Notifications
You must be signed in to change notification settings - Fork 0
263 lines (238 loc) · 10.4 KB
/
update-changelogs.yml
File metadata and controls
263 lines (238 loc) · 10.4 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
name: Update Changelogs
on:
issue_comment:
types:
- created
pull_request_target:
branches:
- main
types:
- opened
- ready_for_review
permissions:
contents: write
pull-requests: write
jobs:
is-fork:
name: Determine whether this PR is from a fork
if: (github.event_name == 'pull_request_target' && !github.event.pull_request.draft) || (github.event.issue.pull_request && startsWith(github.event.comment.body, '@metamaskbot update-changelogs'))
runs-on: ubuntu-latest
outputs:
is-fork: ${{ steps.is-fork.outputs.is-fork }}
steps:
- name: Determine whether this PR is from a fork
id: is-fork
run: |
IS_FORK="$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "$PR_NUMBER" --repo "$GITHUB_REPOSITORY")"
echo "is-fork=$IS_FORK" >> "$GITHUB_OUTPUT"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
is-release:
name: Determine whether this PR is a release PR
needs: is-fork
if: needs.is-fork.outputs.is-fork == 'false'
runs-on: ubuntu-latest
environment: default-branch
outputs:
is-release: ${{ steps.is-release.outputs.IS_RELEASE }}
head-sha: ${{ steps.pr-info.outputs.pr-head-sha }}
head-ref: ${{ steps.pr-info.outputs.pr-head-ref }}
base-ref: ${{ steps.pr-info.outputs.pr-base-ref }}
merge-base: ${{ steps.merge-base.outputs.merge-base }}
steps:
- name: Get pull request info
id: pr-info
env:
GH_TOKEN: ${{ secrets.UPDATE_CHANGELOG_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
run: |
gh pr view "$PR_NUMBER" \
--repo "$GITHUB_REPOSITORY" \
--json baseRefName,headRefOid,headRefName,title \
--jq '"pr-base-ref=\(.baseRefName)\npr-head-sha=\(.headRefOid)\npr-head-ref=\(.headRefName)\npr-title=\(.title)"' \
>> "$GITHUB_OUTPUT"
- name: Checkout repository
uses: actions/checkout@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
fetch-depth: 0
ref: ${{ steps.pr-info.outputs.pr-head-sha }}
- name: Get merge base
id: merge-base
shell: bash
env:
BASE_REF: ${{ steps.pr-info.outputs.pr-base-ref }}
run: |
set -euo pipefail
MERGE_BASE=$(git merge-base HEAD "refs/remotes/origin/$BASE_REF")
echo "merge-base=$MERGE_BASE" >> "$GITHUB_OUTPUT"
- name: Check if the pull request is a release
id: is-release
uses: MetaMask/action-is-release@v2
with:
commit-starts-with: ${{ vars.RELEASE_COMMIT_PREFIX }}
commit-message: ${{ steps.pr-info.outputs.pr-title }}
before: ${{ steps.merge-base.outputs.merge-base }}
skip-checkout: true
react-to-comment:
name: React to the comment
needs: is-release
if: needs.is-release.outputs.is-release == 'true' && github.event_name == 'issue_comment'
runs-on: ubuntu-latest
environment: default-branch
continue-on-error: true
steps:
- name: Checkout repository
uses: actions/checkout@v6
- name: React to the comment
run: |
gh api \
--method POST \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/${REPO}/issues/comments/${COMMENT_ID}/reactions" \
-f content='+1'
env:
COMMENT_ID: ${{ github.event.comment.id }}
GH_TOKEN: ${{ secrets.UPDATE_CHANGELOG_TOKEN }}
REPO: ${{ github.repository }}
update-changelogs:
name: Update changelogs
needs: is-release
if: ${{ needs.is-release.outputs.is-release == 'true' }}
runs-on: ubuntu-latest
environment: default-branch
steps:
- name: Check out the base branch
uses: actions/checkout@v6
with:
ref: ${{ needs.is-release.outputs.merge-base }}
token: ${{ secrets.UPDATE_CHANGELOG_TOKEN }}
- name: Detach HEAD (to prevent accidental pushes)
run: git checkout --detach HEAD
- name: Set up environment
uses: MetaMask/action-checkout-and-setup@v3
with:
is-high-risk-environment: false
- name: Overlay relevant files from current pull request
env:
PR_HEAD_SHA: ${{ needs.is-release.outputs.head-sha }}
PR_HEAD_REF: ${{ needs.is-release.outputs.head-ref }}
run: |
# These next two commands are also useful later when pushing
git fetch --no-tags origin "$PR_HEAD_SHA"
git fetch --no-tags origin "$PR_HEAD_REF"
git checkout "$PR_HEAD_SHA" -- '**/CHANGELOG.md' '**/package.json'
shell: bash
- name: Configure Git with name and email
run: |
# This is necessary to make a commit
# Passing `token` to the `checkout` action does not do this for us
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
shell: bash
- name: Commit relevant files from current pull request
run: |
git add -- '**/CHANGELOG.md' '**/package.json'
git commit -m "[Temporary] Add changelogs from current pull request"
shell: bash
- name: Ensure required dependency bump entries exist across all changelogs
id: update-changelogs
env:
PR_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
MERGE_BASE: ${{ needs.is-release.outputs.merge-base }}
run: |
yarn changelog:validate --checkDeps --fix --currentPr "$PR_NUMBER" --fromRef "$MERGE_BASE"
shell: bash
# If changelogs were updated but there were other validation errors
# found, we need to still create a commit below
continue-on-error: true
- name: Commit updated changelogs
id: commit-updated-changelogs
run: |
if git diff --quiet; then
# Nothing to commit; no changelogs updated
exit 0
fi
git add -- '**/CHANGELOG.md'
git commit -m "chore: Update dependency bump changelog entries"
new_commit_id="$(git log -1 --pretty='format:%H')"
echo "new-commit-id=${new_commit_id}" >> "$GITHUB_OUTPUT"
shell: bash
- name: Cherry-pick new commit on top of pull request branch and push it
id: push-changes
env:
NEW_COMMIT_ID: ${{ steps.commit-updated-changelogs.outputs.new-commit-id }}
PR_HEAD_SHA: ${{ needs.is-release.outputs.head-sha }}
PR_HEAD_REF: ${{ needs.is-release.outputs.head-ref }}
run: |
if [[ -n "$NEW_COMMIT_ID" ]]; then
git checkout "$PR_HEAD_SHA"
git cherry-pick "$NEW_COMMIT_ID"
git push origin "HEAD:$PR_HEAD_REF"
echo "changes-pushed=true" >> "$GITHUB_OUTPUT"
else
echo "changes-pushed=false" >> "$GITHUB_OUTPUT"
fi
shell: bash
- name: Comment result
if: always()
uses: actions/github-script@v9
env:
CHANGES_PUSHED: ${{ steps.push-changes.outputs.changes-pushed }}
PUSH_CHANGES_OUTCOME: ${{ steps.push-changes.outcome }}
UPDATE_CHANGELOGS_OUTCOME: ${{ steps.update-changelogs.outcome }}
PR_NUMBER: ${{ github.event.issue.number || github.event.pull_request.number }}
with:
github-token: ${{ secrets.UPDATE_CHANGELOG_TOKEN }}
script: |
const {
CHANGES_PUSHED,
PUSH_CHANGES_OUTCOME,
UPDATE_CHANGELOGS_OUTCOME,
PR_NUMBER,
} = process.env;
// List and minimize any existing changelog update comments.
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.PR_NUMBER,
});
for (const comment of comments) {
if (comment.body.includes('<!-- Changelog update comment -->')) {
await github.graphql(`
mutation($commentId: ID!, $classifier: ReportedContentClassifiers!) {
minimizeComment(input: {subjectId: $commentId, classifier: $classifier}) {
minimizedComment {
isMinimized
}
}
}
`, {
commentId: comment.node_id,
classifier: 'OUTDATED',
});
}
}
function getCommentBody() {
if (CHANGES_PUSHED === 'true' && UPDATE_CHANGELOGS_OUTCOME === 'failure') {
return `⚠️ Changelogs updated and pushed, but some validation errors remain. Check the [workflow run](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) for details.`;
} else if (CHANGES_PUSHED === 'true') {
return '✅ Changelogs updated and pushed.';
} else if (PUSH_CHANGES_OUTCOME === 'failure') {
return `❌ Failed to push changelog fixes. Check the [workflow run](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) for details.`;
} else if (UPDATE_CHANGELOGS_OUTCOME === 'failure') {
return `❌ Changelog validation failed. Check the [workflow run](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) for details.`;
} else if (UPDATE_CHANGELOGS_OUTCOME === 'skipped' || PUSH_CHANGES_OUTCOME === 'skipped') {
return `❌ Workflow failed before changelog validation. Check the [workflow run](${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}/actions/runs/${process.env.GITHUB_RUN_ID}) for details.`;
} else {
return '✅ No changelog changes needed.';
}
}
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: process.env.PR_NUMBER,
body: `${getCommentBody()}\n\n<!-- Changelog update comment -->`,
});