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