|
| 1 | +# Run the regeneration when a comment is made on a PR that contains the text "/regenerate". |
| 2 | +# Draws from a very useful blog article: https://grem1.in/post/gha-comment-trigger/ |
| 3 | + |
| 4 | +name: PR Comment handling |
| 5 | +on: |
| 6 | + issue_comment: |
| 7 | + types: |
| 8 | + - created |
| 9 | + |
| 10 | +jobs: |
| 11 | + regenerate: |
| 12 | + name: Regenerate sample data |
| 13 | + if: ${{ github.event.issue.pull_request && github.event.comment.body == '/regenerate' }} |
| 14 | + steps: |
| 15 | + - name: Put a reaction to the comment |
| 16 | + run: gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 17 | + env: |
| 18 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 19 | + NODE_ID: ${{ github.event.comment.node_id }} |
| 20 | + |
| 21 | + - name: Check if PR is open |
| 22 | + run: | |
| 23 | + STATE=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json state --jq .state) |
| 24 | + if [ "$STATE" != "OPEN" ]; then |
| 25 | + echo "Cannot build for closed PRs" |
| 26 | + ( |
| 27 | + echo "**${{ github.workflow }}**" |
| 28 | + echo "Cannot build regenerate the smaple data for a closed PR." |
| 29 | + ) | \ |
| 30 | + gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F - |
| 31 | + gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_DOWN}){reaction{content}subject{id}}}" |
| 32 | + gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 33 | + exit 1 |
| 34 | + fi |
| 35 | + env: |
| 36 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 38 | + NODE_ID: ${{ github.event.comment.node_id }} |
| 39 | + |
| 40 | + - name: Get PR HEAD Ref |
| 41 | + id: getRef |
| 42 | + run: echo "pr_ref=$(gh pr view $PR_NUMBER --repo ${{ github.repository }} --json headRefOid | jq -r '.headRefOid')" >> $GITHUB_OUTPUT |
| 43 | + env: |
| 44 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 45 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 46 | + |
| 47 | + - name: Checkout source code from Github |
| 48 | + uses: actions/checkout@v4 |
| 49 | + with: |
| 50 | + fetch-depth: 0 |
| 51 | + ref: ${{ steps.getRef.outputs.pr_ref }} |
| 52 | + token: ${{ secrets.PAT }} |
| 53 | + |
| 54 | + - name: Run the regeneration |
| 55 | + uses: ./.github/actions/regenerate |
| 56 | + |
| 57 | + - name: Final Comment |
| 58 | + run: | |
| 59 | + gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_UP}){reaction{content}subject{id}}}" |
| 60 | + gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 61 | + ( |
| 62 | + echo "**${{ github.workflow }}**" |
| 63 | + echo "The regenerate task is done!" |
| 64 | + echo |
| 65 | + echo "You can find the workflow here:" |
| 66 | + echo "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 67 | + ) | \ |
| 68 | + gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F - |
| 69 | + env: |
| 70 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 71 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 72 | + NODE_ID: ${{ github.event.comment.node_id }} |
| 73 | + |
| 74 | + notify-job: |
| 75 | + needs: [regenerate] |
| 76 | + if: ${{ always() && contains(needs.*.result, 'failure') }} |
| 77 | + steps: |
| 78 | + - name: Notify on Failure |
| 79 | + run: | |
| 80 | + gh api graphql --silent --raw-field query="mutation AddReaction {addReaction(input:{subjectId:\"$NODE_ID\",content:THUMBS_DOWN}){reaction{content}subject{id}}}" |
| 81 | + gh api graphql --silent --raw-field query="mutation RemoveReaction {removeReaction(input:{subjectId:\"$NODE_ID\",content:EYES}){reaction{content}subject{id}}}" |
| 82 | + ( |
| 83 | + echo "**${{ github.workflow }}**" |
| 84 | + echo "**Something went wrong!**" |
| 85 | + echo |
| 86 | + echo "**Details:** ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}" |
| 87 | + ) | \ |
| 88 | + gh pr comment "${PR_NUMBER}" --repo ${{ github.repository }} -F - |
| 89 | + env: |
| 90 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 91 | + PR_NUMBER: ${{ github.event.issue.number }} |
| 92 | + NODE_ID: ${{ github.event.comment.node_id }} |
0 commit comments