Skip to content

Commit a2801e5

Browse files
authored
feat: add bot workflow to update attributions (#29)
1 parent 1e18d45 commit a2801e5

File tree

1 file changed

+191
-0
lines changed

1 file changed

+191
-0
lines changed
Lines changed: 191 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,191 @@
1+
name: Update Attibutions
2+
3+
on:
4+
issue_comment:
5+
types: created
6+
7+
jobs:
8+
is-fork-pull-request:
9+
name: Determine whether this issue comment was on a pull request from a fork
10+
if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, '@metamaskbot update-attributions') }}
11+
runs-on: ubuntu-latest
12+
outputs:
13+
IS_FORK: ${{ steps.is-fork.outputs.IS_FORK }}
14+
steps:
15+
- uses: actions/checkout@v4
16+
- name: Determine whether this PR is from a fork
17+
id: is-fork
18+
run: echo "IS_FORK=$(gh pr view --json isCrossRepository --jq '.isCrossRepository' "${PR_NUMBER}" )" >> "$GITHUB_OUTPUT"
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
PR_NUMBER: ${{ github.event.issue.number }}
22+
23+
react-to-comment:
24+
name: React to the comment
25+
runs-on: ubuntu-latest
26+
needs: is-fork-pull-request
27+
# Early exit if this is a fork, since later steps are skipped for forks
28+
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
29+
steps:
30+
- name: Checkout repository
31+
uses: actions/checkout@v4
32+
- name: React to the comment
33+
run: |
34+
gh api \
35+
--method POST \
36+
-H "Accept: application/vnd.github+json" \
37+
-H "X-GitHub-Api-Version: 2022-11-28" \
38+
"/repos/${REPO}/issues/comments/${COMMENT_ID}/reactions" \
39+
-f content='+1'
40+
env:
41+
COMMENT_ID: ${{ github.event.comment.id }}
42+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43+
REPO: ${{ github.repository }}
44+
45+
prepare:
46+
name: Prepare dependencies
47+
runs-on: ubuntu-latest
48+
needs: is-fork-pull-request
49+
# Early exit if this is a fork, since later steps are skipped for forks
50+
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
51+
outputs:
52+
COMMIT_SHA: ${{ steps.commit-sha.outputs.COMMIT_SHA }}
53+
steps:
54+
- name: Checkout repository
55+
uses: actions/checkout@v4
56+
- name: Checkout pull request
57+
run: gh pr checkout "${PR_NUMBER}"
58+
env:
59+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
60+
PR_NUMBER: ${{ github.event.issue.number }}
61+
- run: corepack enable
62+
- name: Setup Node.js
63+
uses: actions/setup-node@v4
64+
with:
65+
node-version-file: '.nvmrc'
66+
cache: 'yarn'
67+
- name: Install Yarn dependencies
68+
run: yarn --immutable
69+
- name: Get commit SHA
70+
id: commit-sha
71+
run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
72+
73+
update-attributions:
74+
name: Update Attributions
75+
runs-on: ubuntu-latest
76+
needs:
77+
- prepare
78+
steps:
79+
- name: Checkout repository
80+
uses: actions/checkout@v4
81+
- name: Checkout pull request
82+
run: gh pr checkout "${PR_NUMBER}"
83+
env:
84+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
85+
PR_NUMBER: ${{ github.event.issue.number }}
86+
- run: corepack enable
87+
- name: Setup Node.js
88+
uses: actions/setup-node@v4
89+
with:
90+
node-version-file: '.nvmrc'
91+
cache: 'yarn'
92+
- name: Install dependencies from cache
93+
run: yarn --immutable --immutable-cache
94+
- name: Generate Atributions
95+
run: yarn attributions:generate
96+
- name: Cache attributions file
97+
uses: actions/cache/save@v3
98+
with:
99+
path: attribution.txt
100+
key: cache-build-${{ needs.prepare.outputs.COMMIT_SHA }}
101+
102+
commit-updated-attributions:
103+
name: Commit the updated Attributions
104+
runs-on: ubuntu-latest
105+
needs:
106+
- prepare
107+
- is-fork-pull-request
108+
- update-attributions
109+
if: ${{ needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
110+
steps:
111+
- name: Checkout repository
112+
uses: actions/checkout@v4
113+
with:
114+
# Use PAT to ensure that the commit later can trigger status check workflows
115+
token: ${{ secrets.GITHUB_TOKEN }}
116+
- name: Checkout pull request
117+
run: gh pr checkout "${PR_NUMBER}"
118+
env:
119+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
120+
PR_NUMBER: ${{ github.event.issue.number }}
121+
- name: Get commit SHA
122+
id: commit-sha
123+
run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
124+
- name: Restore attributions file
125+
uses: actions/cache/restore@v3
126+
with:
127+
path: attribution.txt
128+
key: cache-build-${{ needs.prepare.outputs.COMMIT_SHA }}
129+
fail-on-cache-miss: true
130+
- name: Check whether there are attributions changes
131+
id: attributions-changes
132+
run: |
133+
if git diff --exit-code
134+
then
135+
echo "HAS_CHANGES=false" >> "$GITHUB_OUTPUT"
136+
else
137+
echo "HAS_CHANGES=true" >> "$GITHUB_OUTPUT"
138+
fi
139+
- name: Commit the updated attributions
140+
if: steps.attributions-changes.outputs.HAS_CHANGES == 'true'
141+
run: |
142+
git config --global user.name 'MetaMask Bot'
143+
git config --global user.email '[email protected]'
144+
git commit -am "Update Attributions"
145+
git push
146+
- name: Post comment
147+
run: |
148+
if [[ $HAS_CHANGES == 'true' ]]
149+
then
150+
gh pr comment "${PR_NUMBER}" --body 'Attributions updated'
151+
else
152+
gh pr comment "${PR_NUMBER}" --body 'No attributions changes'
153+
fi
154+
env:
155+
HAS_CHANGES: ${{ steps.attributions-changes.outputs.HAS_CHANGES }}
156+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
157+
PR_NUMBER: ${{ github.event.issue.number }}
158+
159+
check-status:
160+
name: Check whether the attributions update succeeded
161+
runs-on: ubuntu-latest
162+
needs:
163+
- commit-updated-attributions
164+
outputs:
165+
PASSED: ${{ steps.set-output.outputs.PASSED }}
166+
steps:
167+
- name: Set PASSED output
168+
id: set-output
169+
run: echo "PASSED=true" >> "$GITHUB_OUTPUT"
170+
171+
failure-comment:
172+
name: Comment about the attributions update failure
173+
if: ${{ always() && needs.is-fork-pull-request.outputs.IS_FORK == 'false' }}
174+
runs-on: ubuntu-latest
175+
needs:
176+
- is-fork-pull-request
177+
- check-status
178+
steps:
179+
- uses: actions/checkout@v4
180+
with:
181+
token: ${{ secrets.GITHUB_TOKEN }}
182+
- name: Post comment if the update failed
183+
run: |
184+
passed="${{ needs.check-status.outputs.PASSED }}"
185+
if [[ $passed != "true" ]]; then
186+
gh pr comment "${PR_NUMBER}" --body "Attributions update failed. You can [review the logs or retry the attributions update here](${ACTION_RUN_URL})"
187+
fi
188+
env:
189+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
190+
PR_NUMBER: ${{ github.event.issue.number }}
191+
ACTION_RUN_URL: "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"

0 commit comments

Comments
 (0)