Skip to content
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

Added a validation check for pr approval in the deploy-pr workflow #660

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ jobs:
run: yarn build
working-directory: packages/docs/

- name: Save PR Number
run: echo "${{ github.event.pull_request.number }}" > pr_number.txt

- name: Upload PR Number Artifact
uses: actions/upload-artifact@v4
with:
name: pr-number
path: pr_number.txt

- name: Prepare Build Folder
run: |
mkdir -p build/pulls/pr-${{github.event.pull_request.number}}/
Expand Down
40 changes: 40 additions & 0 deletions .github/workflows/deploy-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,46 @@ jobs:
runs-on: ubuntu-latest

steps:

- name: Download PR Number Artifact
uses: actions/download-artifact@v4
with:
name: pr-number
path: .
github-token: ${{github.token}}
repository: ${{github.repository}}
run-id: ${{github.event.workflow_run.id}}

- name: Check PR Approval Status
id: approval_check
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_NUMBER=$(cat pr_number.txt)

RESPONSE=$(curl -s -H "Authorization: token $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/pulls/$PR_NUMBER/reviews")

if ! echo "$RESPONSE" | jq . > /dev/null 2>&1; then
echo "Error: Invalid JSON response from GitHub API."
exit 1
fi

LATEST_REVIEW=$(echo "$RESPONSE" | jq 'sort_by(.submitted_at) | last')

STATE=$(echo "$LATEST_REVIEW" | jq -r '.state')
AUTHOR_ASSOCIATION=$(echo "$LATEST_REVIEW" | jq -r '.author_association')

echo "Latest review state: $STATE"
echo "Author association: $AUTHOR_ASSOCIATION"

if [ "$STATE" != "APPROVED" ] || { [ "$AUTHOR_ASSOCIATION" != "COLLABORATOR" ] && [ "$AUTHOR_ASSOCIATION" != "OWNER" ]; }; then
echo "The latest review is not an approved review from a collaborator or owner. Exiting."
exit 1
fi

- uses: actions/download-artifact@v4
if: success()
with:
name: github-pages
path: build/
Expand All @@ -25,6 +64,7 @@ jobs:
run-id: ${{github.event.workflow_run.id}}

- name: Deploy to GitHub Pages
if: success()
uses: crazy-max/ghaction-github-pages@v2
with:
target_branch: gh-deploy
Expand Down
Loading