Apply Auto Fixes #299
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
| name: Apply Auto Fixes | |
| on: | |
| workflow_run: | |
| workflows: ["Generate and Format"] | |
| types: | |
| - completed | |
| # Ensure that only one 'apply_patch' job runs at a time for the same PR. | |
| concurrency: | |
| # The group is the pull request number. | |
| group: ${{ github.event.workflow_run.pull_requests[0].number }}-apply-patch | |
| cancel-in-progress: true | |
| jobs: | |
| apply_patch: | |
| # Only run on successful completion of the 'Generate and Format' workflow | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| steps: | |
| - name: Check if patch artifact exists | |
| id: check_artifact | |
| run: | | |
| # Use the GitHub API to check if the 'auto-fixes-patch' artifact exists for the triggering workflow run. | |
| # The artifacts_url is a pre-authenticated API endpoint. | |
| # We use jq to parse the JSON response and see if our artifact is in the list. | |
| artifact_json=$(curl -s "${{ github.event.workflow_run.artifacts_url }}") | |
| if echo "$artifact_json" | jq -e '.artifacts[] | select(.name == "auto-fixes-patch")' > /dev/null; then | |
| echo "Patch artifact found." | |
| echo "exists=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "No patch artifact was uploaded." | |
| echo "exists=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Checkout PR branch | |
| if: steps.check_artifact.outputs.exists == 'true' | |
| uses: actions/checkout@v4 | |
| with: | |
| repository: ${{ github.event.workflow_run.head_repository.full_name }} | |
| ref: ${{ github.event.workflow_run.head_branch }} | |
| token: ${{ secrets.CODE_AUTO_FORMAT }} | |
| - name: Download patch artifact | |
| if: steps.check_artifact.outputs.exists == 'true' | |
| uses: actions/download-artifact@v4 | |
| with: | |
| name: auto-fixes-patch | |
| path: . | |
| github-token: ${{ secrets.GITHUB_TOKEN }} | |
| run-id: ${{ github.event.workflow_run.id }} | |
| - name: Apply patch | |
| if: steps.check_artifact.outputs.exists == 'true' | |
| run: | | |
| # Applying the patch will fail if it's empty, but we've already checked for that. | |
| git apply auto_fixes.patch | |
| - name: Clean up patch file | |
| if: steps.check_artifact.outputs.exists == 'true' | |
| run: | | |
| rm auto_fixes.patch | |
| - name: Commit changes | |
| if: steps.check_artifact.outputs.exists == 'true' | |
| uses: EndBug/add-and-commit@v9 | |
| with: | |
| message: "style: apply auto-formatting and generation" | |
| author_name: ${{ github.event.workflow_run.actor.login }} | |
| author_email: ${{ github.event.workflow_run.actor.login }}@users.noreply.github.com | |
| commit: --signoff |