Skip to content

Commit 7a2be07

Browse files
authored
Merge pull request #7 from CMIP-REF/regen-gha
2 parents a28ef69 + 1fb019f commit 7a2be07

5 files changed

Lines changed: 132 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
name: "Regenerate sample data"
2+
description: "Runs the regenerate sample data workflow and commits the result"
3+
4+
runs:
5+
using: "composite"
6+
steps:
7+
- uses: ./.github/actions/setup
8+
with:
9+
python-version: 3.12
10+
11+
- name: Verify registry
12+
run: |
13+
git config --global user.name "$GITHUB_ACTOR"
14+
git config --global user.email "$CI_COMMIT_EMAIL"
15+
16+
make fetch-test-data
17+
make registry.txt
18+
git diff
19+
20+
git add data registry.txt
21+
git commit -m "Updated sample data"
22+
git push

.github/workflows/pr-comment.yaml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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 }}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Regenerate sample data
2+
3+
on:
4+
workflow_dispatch:
5+
6+
jobs:
7+
update-data:
8+
runs-on: "ubuntu-latest"
9+
steps:
10+
- name: Check out repository
11+
uses: actions/checkout@v4
12+
with:
13+
fetch-depth: 0
14+
token: ${{ secrets.PAT }}
15+
- uses: ./.github/actions/regenerate

changelog/.gitkeep

Whitespace-only changes.

changelog/7.feature.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Added an action to regenerate the sample data in Pull Requests.
2+
3+
A comment containing `\regenerate` will trigger the action.

0 commit comments

Comments
 (0)