|
| 1 | +name: Format Check |
| 2 | +on: |
| 3 | + push: |
| 4 | + branches: |
| 5 | + - main |
| 6 | + tags: ['*'] |
| 7 | + pull_request: |
| 8 | + workflow_dispatch: |
| 9 | +concurrency: |
| 10 | + # Skip intermediate builds: always. |
| 11 | + # Cancel intermediate builds: only if it is a pull request build. |
| 12 | + group: ${{ github.workflow }}-${{ github.ref }} |
| 13 | + cancel-in-progress: ${{ startsWith(github.ref, 'refs/pull/') }} |
| 14 | +jobs: |
| 15 | + format-check: |
| 16 | + name: Code Format Check |
| 17 | + runs-on: ubuntu-latest |
| 18 | + # Don't run on PRs that come from forks as they won't have permission to create PRs |
| 19 | + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository |
| 20 | + permissions: |
| 21 | + contents: write # Needed to push commits |
| 22 | + issues: write # Needed to create PRs and write comments |
| 23 | + pull-requests: write # Needed to create PRs and write comments |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + ref: ${{ github.head_ref }} |
| 28 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + - uses: julia-actions/setup-julia@v2 |
| 30 | + - uses: julia-actions/cache@v2 |
| 31 | + |
| 32 | + # Find existing format PR if any |
| 33 | + - name: Find existing format PR |
| 34 | + id: find_pr |
| 35 | + uses: actions/github-script@v7 |
| 36 | + with: |
| 37 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 38 | + script: | |
| 39 | + const prNumber = ${{ github.event.pull_request.number }}; |
| 40 | + const owner = context.repo.owner; |
| 41 | + const repo = context.repo.repo; |
| 42 | + |
| 43 | + // Look for open PRs with our auto-format branch pattern that targets this PR's branch |
| 44 | + const prs = await github.rest.pulls.list({ |
| 45 | + owner, |
| 46 | + repo, |
| 47 | + state: 'open', |
| 48 | + base: '${{ github.head_ref }}' |
| 49 | + }); |
| 50 | + |
| 51 | + const formatPr = prs.data.find(pr => pr.head.ref.startsWith('auto-format-') && |
| 52 | + pr.title === "🤖 Auto-format Julia code"); |
| 53 | + |
| 54 | + if (formatPr) { |
| 55 | + console.log(`Found existing format PR: #${formatPr.number}`); |
| 56 | + return formatPr.number; |
| 57 | + } |
| 58 | + |
| 59 | + return ''; |
| 60 | + |
| 61 | + - name: Run formatter check |
| 62 | + id: format_check |
| 63 | + run: | |
| 64 | + if ! make check-format; then |
| 65 | + echo "format_needs_fix=true" >> $GITHUB_OUTPUT |
| 66 | + else |
| 67 | + echo "format_needs_fix=false" >> $GITHUB_OUTPUT |
| 68 | + fi |
| 69 | + |
| 70 | + # Close any existing formatting PR if the check now passes |
| 71 | + - name: Close existing format PR if check passes |
| 72 | + if: steps.format_check.outputs.format_needs_fix == 'false' && steps.find_pr.outputs.result != '' |
| 73 | + uses: actions/github-script@v7 |
| 74 | + with: |
| 75 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 76 | + script: | |
| 77 | + const formatPrNumber = Number(${{ steps.find_pr.outputs.result }}); |
| 78 | +
|
| 79 | + if (formatPrNumber === 0) { |
| 80 | + return; |
| 81 | + } |
| 82 | +
|
| 83 | + const owner = context.repo.owner; |
| 84 | + const repo = context.repo.repo; |
| 85 | + |
| 86 | + // Close the PR with a comment |
| 87 | + await github.rest.issues.createComment({ |
| 88 | + owner, |
| 89 | + repo, |
| 90 | + issue_number: formatPrNumber, |
| 91 | + body: `Closing this PR as the code formatting issues in the original PR have been resolved.` |
| 92 | + }); |
| 93 | + |
| 94 | + await github.rest.pulls.update({ |
| 95 | + owner, |
| 96 | + repo, |
| 97 | + pull_number: formatPrNumber, |
| 98 | + state: 'closed' |
| 99 | + }); |
| 100 | + |
| 101 | + console.log(`Closed format PR #${formatPrNumber} as the original PR now passes formatting checks.`); |
| 102 | + |
| 103 | + - name: Apply formatter if needed |
| 104 | + if: steps.format_check.outputs.format_needs_fix == 'true' |
| 105 | + run: | |
| 106 | + make format |
| 107 | + |
| 108 | + - name: Commit changes and create/update PR |
| 109 | + if: steps.format_check.outputs.format_needs_fix == 'true' |
| 110 | + uses: peter-evans/create-pull-request@v7 |
| 111 | + with: |
| 112 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 113 | + commit-message: "🤖 Auto-format Julia code" |
| 114 | + title: "🤖 Auto-format Julia code" |
| 115 | + body: | |
| 116 | + This PR was automatically created to fix Julia code formatting issues. |
| 117 | + |
| 118 | + The formatting was applied using JuliaFormatter according to the project's style guidelines. |
| 119 | + |
| 120 | + Please review the changes and merge if appropriate. |
| 121 | + branch: auto-format-${{ github.event.pull_request.number }} |
| 122 | + base: ${{ github.head_ref }} |
| 123 | + delete-branch: true |
| 124 | + labels: | |
| 125 | + automated pr |
| 126 | + code style |
| 127 | + id: create-pr |
| 128 | + |
| 129 | + - name: Comment on original PR |
| 130 | + if: steps.format_check.outputs.format_needs_fix == 'true' && steps.create-pr.outputs.pull-request-number && steps.find_pr.outputs.result == '' |
| 131 | + uses: actions/github-script@v7 |
| 132 | + with: |
| 133 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 134 | + script: | |
| 135 | + const prNumber = ${{ github.event.pull_request.number }}; |
| 136 | + const formatPrNumber = ${{ steps.create-pr.outputs.pull-request-number }}; |
| 137 | + const formatPrUrl = `https://github.com/${{ github.repository }}/pull/${formatPrNumber}`; |
| 138 | + |
| 139 | + await github.rest.issues.createComment({ |
| 140 | + owner: context.repo.owner, |
| 141 | + repo: context.repo.repo, |
| 142 | + issue_number: prNumber, |
| 143 | + body: `## 🤖 Code Formatting |
| 144 | + |
| 145 | + This PR has some code formatting issues. I've created [PR #${formatPrNumber}](${formatPrUrl}) with the necessary formatting changes. |
| 146 | + |
| 147 | + You can merge that PR into this branch to fix the code style check. |
| 148 | + |
| 149 | + Alternatively, you can run \`make format\` locally and push the changes yourself.` |
| 150 | + }); |
| 151 | + |
| 152 | + - name: Comment on original PR for updated formatting PR |
| 153 | + if: steps.format_check.outputs.format_needs_fix == 'true' && steps.create-pr.outputs.pull-request-number && steps.find_pr.outputs.result != '' |
| 154 | + uses: actions/github-script@v7 |
| 155 | + with: |
| 156 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 157 | + script: | |
| 158 | + const prNumber = ${{ github.event.pull_request.number }}; |
| 159 | + const formatPrNumber = ${{ steps.create-pr.outputs.pull-request-number }}; |
| 160 | + const formatPrUrl = `https://github.com/${{ github.repository }}/pull/${formatPrNumber}`; |
| 161 | + |
| 162 | + await github.rest.issues.createComment({ |
| 163 | + owner: context.repo.owner, |
| 164 | + repo: context.repo.repo, |
| 165 | + issue_number: prNumber, |
| 166 | + body: `## 🤖 Code Formatting |
| 167 | + |
| 168 | + Your PR still has some code formatting issues. I've updated [PR #${formatPrNumber}](${formatPrUrl}) with the necessary formatting changes. |
| 169 | + |
| 170 | + You can merge that PR into this branch to fix the code style check. |
| 171 | + |
| 172 | + Alternatively, you can run \`make format\` locally and push the changes yourself.` |
| 173 | + }); |
| 174 | + |
| 175 | + # Fail the job if formatting was needed and applied |
| 176 | + - name: Fail if formatting was needed |
| 177 | + if: steps.format_check.outputs.format_needs_fix == 'true' |
| 178 | + run: | |
| 179 | + echo "::error::Code formatting issues detected. A PR with fixes has been created, but this check is failing to indicate that formatting needs to be fixed." |
| 180 | + exit 1 |
0 commit comments