-
-
Notifications
You must be signed in to change notification settings - Fork 19
Add composite workflow action for checking user permissions prior to updating snapshots #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+90
−0
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1e9bf50
Add reusable workflow for checking user permissions prior to updating
martinRenou 7d7c685
Update
martinRenou 2ce0a38
Apply suggestions from code review
martinRenou 59b0c3d
Missing shell property
martinRenou bdeeb68
Add more steps
martinRenou ec5cfab
Missing shell
martinRenou File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| name: Check user permission and checkout the repository before running the update snapshots workflow | ||
| description: | | ||
| Check user permission and checkout the repository before running the update snapshots workflow. | ||
| Reacts with either a thumb up or a thumb down to the comment depending on the user rights. | ||
|
|
||
| inputs: | ||
| # Mandatory inputs | ||
| github_token: | ||
| description: "The GitHub token to use" | ||
| required: true | ||
|
|
||
| runs: | ||
| using: composite | ||
| steps: | ||
| - name: Get commenter association | ||
| id: association | ||
| env: | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
| shell: bash -l {0} | ||
| run: | | ||
| association=$(gh api \ | ||
| repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }} \ | ||
| --jq '.author_association') | ||
|
|
||
| echo "association=$association" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Fail if user is not authorized | ||
| if: | | ||
| !contains(fromJSON('["OWNER","COLLABORATOR","MEMBER"]'), steps.association.outputs.association) | ||
| shell: bash -l {0} | ||
| run: | | ||
| gh api repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions --raw-field 'content=-1' | ||
| echo "User not authorized to update snapshots" | ||
| exit 1 | ||
| env: | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
|
|
||
| - name: React positively to the triggering comment | ||
| shell: bash -l {0} | ||
| run: | | ||
| gh api repos/${{ github.repository }}/issues/comments/${{ github.event.comment.id }}/reactions --raw-field 'content=+1' | ||
| env: | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
|
|
||
| - name: Checkout | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| token: ${{ inputs.github_token }} | ||
|
|
||
| - name: Configure git to use https | ||
| shell: bash -l {0} | ||
| run: git config --global hub.protocol https | ||
|
|
||
| - name: Get PR Info | ||
| id: pr | ||
| shell: bash -l {0} | ||
| env: | ||
| PR_NUMBER: ${{ github.event.issue.number }} | ||
| GH_TOKEN: ${{ inputs.github_token }} | ||
| GH_REPO: ${{ github.repository }} | ||
| COMMENT_AT: ${{ github.event.comment.created_at }} | ||
| run: | | ||
| pr="$(gh api /repos/${GH_REPO}/pulls/${PR_NUMBER})" | ||
| head_sha="$(echo "$pr" | jq -r .head.sha)" | ||
| pushed_at="$(echo "$pr" | jq -r .pushed_at)" | ||
|
|
||
| if [[ $(date -d "$pushed_at" +%s) -gt $(date -d "$COMMENT_AT" +%s) ]]; then | ||
| echo "Updating is not allowed because the PR was pushed to (at $pushed_at) after the triggering comment was issued (at $COMMENT_AT)" | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "head_sha=$head_sha" >> $GITHUB_OUTPUT | ||
|
|
||
| - name: Checkout the branch from the PR that triggered the job | ||
| shell: bash -l {0} | ||
| env: | ||
| GITHUB_TOKEN: ${{ inputs.github_token }} | ||
| run: gh pr checkout ${{ github.event.issue.number }} | ||
|
|
||
| - name: Validate the fetched branch HEAD revision | ||
| shell: bash -l {0} | ||
| env: | ||
| EXPECTED_SHA: ${{ steps.pr.outputs.head_sha }} | ||
| run: | | ||
| actual_sha="$(git rev-parse HEAD)" | ||
|
|
||
| if [[ "$actual_sha" != "$EXPECTED_SHA" ]]; then | ||
| echo "The HEAD of the checked out branch ($actual_sha) differs from the HEAD commit available at the time when trigger comment was submitted ($EXPECTED_SHA)" | ||
| exit 1 | ||
| fi | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe for a follow up PR, but I wonder if we could add a comment or replace the previous 👍 with a 👎, to have a visual indication that something did not work. Same for the step below.
Otherwise it looks good, thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense! Yes a comment saying the bot failed, with a link to the logs, would be great. Plus, the link to the logs could probably always show up in a comment, so that users know where to look for.
Agreed to make that as a follow-up 👍🏽