|
| 1 | +name: Rebuild Approved PR |
| 2 | + |
| 3 | +on: |
| 4 | + issue_comment: |
| 5 | + types: [created] |
| 6 | + |
| 7 | +jobs: |
| 8 | + rebuild_approved_pr: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + if: > |
| 11 | + github.event.comment.user.login == 'Gamebuster19901' && |
| 12 | + github.event.comment.body == '@WilderForge rebuild' && |
| 13 | + github.event.issue.pull_request != null |
| 14 | + steps: |
| 15 | + - name: Fetch Approval Workflow Run |
| 16 | + id: fetch_approval_run |
| 17 | + run: | |
| 18 | + # Fetch the pull request details |
| 19 | + PR_URL=$(jq -r '.pull_request.url' <<< '${{ toJson(github.event.issue) }}') |
| 20 | + echo "PR URL: $PR_URL" |
| 21 | + |
| 22 | + PR_DETAILS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" "$PR_URL") |
| 23 | + # echo "PR Details: $PR_DETAILS" # Debugging: output PR details to see its contents |
| 24 | + |
| 25 | + PR_NUMBER=$(echo "$PR_DETAILS" | jq -r '.number') |
| 26 | + echo "PR Number: $PR_NUMBER" # Debugging: output the PR number |
| 27 | +
|
| 28 | + # Get the commit SHA directly from the pull request head object |
| 29 | + PR_SHA=$(echo "$PR_DETAILS" | jq -r '.head.sha') |
| 30 | + echo "PR SHA: $PR_SHA" # Debugging: output the PR SHA |
| 31 | +
|
| 32 | + if [ -z "$PR_SHA" ]; then |
| 33 | + echo "Pull request head commit SHA is null. Exiting." |
| 34 | + exit 1 |
| 35 | + fi |
| 36 | +
|
| 37 | + # Get the list of workflow runs for the repository |
| 38 | + WORKFLOWS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 39 | + "https://api.github.com/repos/${{ github.repository }}/actions/runs") |
| 40 | + # echo "Workflow Runs: $WORKFLOWS" # Debugging: output workflow runs data |
| 41 | +
|
| 42 | + # Find the latest completed run of the 'Validate Approval' workflow that matches the head SHA |
| 43 | + APPROVAL_RUN=$(echo "$WORKFLOWS" | jq -r \ |
| 44 | + '.workflow_runs[] | select(.name == "Validate Approval" and .head_sha == "'$PR_SHA'" and .status == "completed") | .id' | head -n 1) |
| 45 | + echo "Approval Run: $APPROVAL_RUN" # Debugging: output the approval run ID |
| 46 | +
|
| 47 | + if [ -z "$APPROVAL_RUN" ]; then |
| 48 | + echo "The head of this PR has not been validated. Exiting." |
| 49 | + exit 1 |
| 50 | + fi |
| 51 | +
|
| 52 | + # Save the approval run ID to environment variable for use in later steps |
| 53 | + echo "APPROVAL_RUN=$APPROVAL_RUN" >> $GITHUB_ENV |
| 54 | +
|
| 55 | + - name: Check Approval |
| 56 | + id: check_approval_status |
| 57 | + run: | |
| 58 | + # Fetch the details of the approval workflow run using the saved APPROVAL_RUN ID |
| 59 | + APPROVAL_STATUS=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 60 | + "https://api.github.com/repos/${{ github.repository }}/actions/runs/${{ env.APPROVAL_RUN }}") |
| 61 | +
|
| 62 | + CONCLUSION=$(echo "$APPROVAL_STATUS" | jq -r '.conclusion') |
| 63 | + STATUS=$(echo "$APPROVAL_STATUS" | jq -r '.status') |
| 64 | +
|
| 65 | + echo "Approval Workflow Status: $STATUS" # Debugging: output the workflow status |
| 66 | + echo "Approval Workflow Conclusion: $CONCLUSION" # Debugging: output the workflow conclusion |
| 67 | +
|
| 68 | + if [[ "$CONCLUSION" != "success" || "$STATUS" != "completed" ]]; then |
| 69 | + echo "The head of this PR has not been validated. Exiting." |
| 70 | + exit 1 |
| 71 | + fi |
| 72 | +
|
| 73 | + echo "The head of this PR has been validated." |
| 74 | +
|
| 75 | + - name: Trigger Build Commit Workflow |
| 76 | + if: success() |
| 77 | + run: | |
| 78 | + # Get the source branch of the PR (from the pull_request object) |
| 79 | + PR_BRANCH="${{ github.event.pull_request.head.ref }}" |
| 80 | + |
| 81 | + # Define the API endpoint for dispatching the workflow |
| 82 | + WORKFLOW_URL="https://api.github.com/repos/${{ github.repository }}/actions/workflows/build.yml/dispatches" |
| 83 | + |
| 84 | + # Trigger the workflow for the branch of the pull request |
| 85 | + echo "Triggering workflow for branch: $PR_BRANCH" |
| 86 | + RESPONSE=$(curl -s -w "%{http_code}" -o response.json -X POST \ |
| 87 | + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ |
| 88 | + -d '{"ref": "refs/heads/'${PR_BRANCH}'", "inputs": {"sha": "${{ github.event.review.commit_id }}"}}' \ |
| 89 | + "$WORKFLOW_URL") |
| 90 | + |
| 91 | + # Check if the HTTP status code is 2xx (successful) |
| 92 | + if [[ "$RESPONSE" -lt 200 || "$RESPONSE" -ge 300 ]]; then |
| 93 | + echo "Error triggering the workflow: HTTP $RESPONSE" |
| 94 | + cat response.json |
| 95 | + exit 1 |
| 96 | + else |
| 97 | + echo "Successfully triggered the workflow." |
| 98 | + fi |
0 commit comments