Skip to content

Commit

Permalink
feat: new attribution github workflow (#9856)
Browse files Browse the repository at this point in the history
## **Description**

This pull request introduces a GitHub Action workflow dedicated to
managing attributions effectively within our project. The key components
of this new workflow include:

- Local Scripts for Attributions Management:
- `yarn build:attribution`: Updates attributions to reflect current
dependencies.
- `yarn test:attribution-check`: Checks if attributions are up-to-date
with the latest changes.
- GitHub Actions:
   - A check to ensure attributions are up-to-date on all pull requests.
- An action that allows team members to update attributions directly by
commenting `@metamskbot update-attributions` on any PR.

## **Related issues**

Fixes: [#1550](MetaMask/mobile-planning#1550)

## **Manual testing steps**

### ***Locally***

- Run `yarn build:attribution` to update the attributions file.
- Run `yarn test:attribution-check` to ensure all attributions are
current.
- 
### ***On GitHub (CI/PR)***

- Open and push changes to a PR.
- Intentionally remove an entry from attribution.txt to simulate an
error.
- Observe that CI fails due to missing attributions.
- Comment `@metmaskbot update-attributions` on the PR.
- A bot will respond with a thumbs up, followed by a comment indicating
whether the attributions were successfully updated.

For a practical demonstration, refer to the pull request in my fork: [PR
Demonstration](cryptodev-2s#4)
### ***Note***
This functionality is currently operational only on a fork as it depends
on the integration of the bot action into the develop branch.

## **Pre-merge author checklist**

- [ ] I’ve followed [MetaMask Coding
Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md).
- [ ] I've completed the PR template to the best of my ability
- [ ] I’ve included tests if applicable
- [ ] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [ ] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
  • Loading branch information
cryptodev-2s authored Jun 5, 2024
1 parent b9bfc7c commit d63fcbf
Show file tree
Hide file tree
Showing 8 changed files with 6,013 additions and 4,009 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/check-attributions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Check Attributions

on:
push:
branches: release/*
pull_request:
branches: release/*
types:
- opened
- reopened
- synchronize
- ready_for_review

jobs:
check-attributions:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'yarn'
- name: Install dependencies from cache
run: yarn --immutable
- name: Check attributions changes
run: yarn test:attribution-check
166 changes: 166 additions & 0 deletions .github/workflows/update-attributions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
name: Update Attibutions

on:
issue_comment:
types: created

jobs:
react-to-comment:
name: React to the comment
runs-on: ubuntu-latest
if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, '@metamaskbot update-attributions') }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- 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 }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}

prepare:
name: Prepare dependencies
runs-on: ubuntu-latest
if: ${{ github.event.issue.pull_request && startsWith(github.event.comment.body, '@metamaskbot update-attributions') }}
outputs:
COMMIT_SHA: ${{ steps.commit-sha.outputs.COMMIT_SHA }}
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Checkout pull request
run: gh pr checkout "${PR_NUMBER}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'yarn'
- name: Install Yarn dependencies
run: yarn --immutable
- name: Get commit SHA
id: commit-sha
run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"

update-attributions:
name: Update Attributions
runs-on: ubuntu-latest
needs:
- prepare
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Checkout pull request
run: gh pr checkout "${PR_NUMBER}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
cache: 'yarn'
- name: Install dependencies from cache
run: yarn --immutable --immutable-cache
- name: Generate Atributions
run: yarn build:attribution
- name: Cache attributions file
uses: actions/cache/save@v3
with:
path: attribution.txt
key: cache-build-${{ needs.prepare.outputs.COMMIT_SHA }}

commit-updated-attributions:
name: Commit the updated Attributions
runs-on: ubuntu-latest
needs:
- prepare
- update-attributions
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
# Use PAT to ensure that the commit later can trigger status check workflows
token: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout pull request
run: gh pr checkout "${PR_NUMBER}"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
- name: Get commit SHA
id: commit-sha
run: echo "COMMIT_SHA=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
- name: Restore attributions file
uses: actions/cache/restore@v3
with:
path: attribution.txt
key: cache-build-${{ needs.prepare.outputs.COMMIT_SHA }}
fail-on-cache-miss: true
- name: Check whether there are attributions changes
id: attributions-changes
run: |
if git diff --exit-code
then
echo "HAS_CHANGES=false" >> "$GITHUB_OUTPUT"
else
echo "HAS_CHANGES=true" >> "$GITHUB_OUTPUT"
fi
- name: Commit the updated attributions
if: steps.attributions-changes.outputs.HAS_CHANGES == 'true'
run: |
git config --global user.name 'MetaMask Bot'
git config --global user.email 'metamaskbot@users.noreply.github.com'
git commit -am "Update Attributions"
git push
- name: Post comment
run: |
if [[ $HAS_CHANGES == 'true' ]]
then
gh pr comment "${PR_NUMBER}" --body 'Attributions updated'
else
gh pr comment "${PR_NUMBER}" --body 'No attributions changes'
fi
env:
HAS_CHANGES: ${{ steps.attributions-changes.outputs.HAS_CHANGES }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}

check-status:
name: Check whether the attributions update succeeded
runs-on: ubuntu-latest
needs:
- commit-updated-attributions
outputs:
PASSED: ${{ steps.set-output.outputs.PASSED }}
steps:
- name: Set PASSED output
id: set-output
run: echo "PASSED=true" >> "$GITHUB_OUTPUT"

failure-comment:
name: Comment about the attributions update failure
runs-on: ubuntu-latest
needs:
- check-status
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Post comment if the update failed
run: |
passed="${{ needs.check-status.outputs.PASSED }}"
if [[ $passed != "true" ]]; then
gh pr comment "${PR_NUMBER}" --body "Attributions update failed. You can [review the logs or retry the attributions update here](${ACTION_RUN_URL})"
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR_NUMBER: ${{ github.event.issue.number }}
ACTION_RUN_URL: '${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}'
Loading

0 comments on commit d63fcbf

Please sign in to comment.