|
40 | 40 | # PDF. Note, this should be the same directory as the input
|
41 | 41 | # paper.md
|
42 | 42 | path: paper/paper.pdf
|
43 |
| - code-style: |
44 |
| - name: Code Style Suggestions |
45 |
| - runs-on: ubuntu-latest |
46 |
| - steps: |
47 |
| - - uses: julia-actions/julia-format@v3 |
48 | 43 | test:
|
49 | 44 | name: Tests ${{ matrix.version }} - ${{ matrix.os }} - ${{ matrix.arch }} - ${{ github.event_name }}
|
50 | 45 | runs-on: ${{ matrix.os }}
|
@@ -120,4 +115,169 @@ jobs:
|
120 | 115 | env:
|
121 | 116 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
122 | 117 | LOG_USING_RXINFER: "false"
|
| 118 | + format-check: |
| 119 | + name: Code Format Check |
| 120 | + runs-on: ubuntu-latest |
| 121 | + # Don't run on PRs that come from forks as they won't have permission to create PRs |
| 122 | + if: github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository |
| 123 | + permissions: |
| 124 | + contents: write # Needed to push commits |
| 125 | + pull-requests: write # Needed to create PRs and write comments |
| 126 | + steps: |
| 127 | + - uses: actions/checkout@v4 |
| 128 | + with: |
| 129 | + ref: ${{ github.head_ref }} |
| 130 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 131 | + - uses: julia-actions/setup-julia@v2 |
| 132 | + - uses: julia-actions/cache@v2 |
| 133 | + |
| 134 | + # Find existing format PR if any |
| 135 | + - name: Find existing format PR |
| 136 | + id: find_pr |
| 137 | + uses: actions/github-script@v7 |
| 138 | + with: |
| 139 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 140 | + script: | |
| 141 | + const prNumber = ${{ github.event.pull_request.number }}; |
| 142 | + const owner = context.repo.owner; |
| 143 | + const repo = context.repo.repo; |
| 144 | + |
| 145 | + // Look for open PRs with our auto-format branch pattern that targets this PR's branch |
| 146 | + const prs = await github.rest.pulls.list({ |
| 147 | + owner, |
| 148 | + repo, |
| 149 | + state: 'open', |
| 150 | + base: '${{ github.head_ref }}' |
| 151 | + }); |
| 152 | + |
| 153 | + const formatPr = prs.data.find(pr => pr.head.ref.startsWith('auto-format-') && |
| 154 | + pr.title === "🤖 Auto-format Julia code"); |
| 155 | + |
| 156 | + if (formatPr) { |
| 157 | + console.log(`Found existing format PR: #${formatPr.number}`); |
| 158 | + return formatPr.number; |
| 159 | + } |
| 160 | + |
| 161 | + return ''; |
| 162 | + |
| 163 | + - name: Run formatter check |
| 164 | + id: format_check |
| 165 | + run: | |
| 166 | + if ! make lint; then |
| 167 | + echo "format_needs_fix=true" >> $GITHUB_OUTPUT |
| 168 | + else |
| 169 | + echo "format_needs_fix=false" >> $GITHUB_OUTPUT |
| 170 | + fi |
| 171 | + |
| 172 | + # Close any existing formatting PR if the check now passes |
| 173 | + - name: Close existing format PR if check passes |
| 174 | + if: steps.format_check.outputs.format_needs_fix == 'false' && steps.find_pr.outputs.result != '' |
| 175 | + uses: actions/github-script@v7 |
| 176 | + with: |
| 177 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 178 | + script: | |
| 179 | + const formatPrNumber = Number(${{ steps.find_pr.outputs.result }}); |
| 180 | +
|
| 181 | + if (formatPrNumber === 0) { |
| 182 | + return; |
| 183 | + } |
| 184 | +
|
| 185 | + const owner = context.repo.owner; |
| 186 | + const repo = context.repo.repo; |
| 187 | + |
| 188 | + // Close the PR with a comment |
| 189 | + await github.rest.issues.createComment({ |
| 190 | + owner, |
| 191 | + repo, |
| 192 | + issue_number: formatPrNumber, |
| 193 | + body: `Closing this PR as the code formatting issues in the original PR have been resolved.` |
| 194 | + }); |
| 195 | + |
| 196 | + await github.rest.pulls.update({ |
| 197 | + owner, |
| 198 | + repo, |
| 199 | + pull_number: formatPrNumber, |
| 200 | + state: 'closed' |
| 201 | + }); |
| 202 | + |
| 203 | + console.log(`Closed format PR #${formatPrNumber} as the original PR now passes formatting checks.`); |
| 204 | + |
| 205 | + - name: Apply formatter if needed |
| 206 | + if: steps.format_check.outputs.format_needs_fix == 'true' |
| 207 | + run: | |
| 208 | + make format |
| 209 | + |
| 210 | + - name: Commit changes and create/update PR |
| 211 | + if: steps.format_check.outputs.format_needs_fix == 'true' |
| 212 | + uses: peter-evans/create-pull-request@v7 |
| 213 | + with: |
| 214 | + token: ${{ secrets.GITHUB_TOKEN }} |
| 215 | + commit-message: "🤖 Auto-format Julia code" |
| 216 | + title: "🤖 Auto-format Julia code" |
| 217 | + body: | |
| 218 | + This PR was automatically created to fix Julia code formatting issues. |
| 219 | + |
| 220 | + The formatting was applied using JuliaFormatter according to the project's style guidelines. |
| 221 | + |
| 222 | + Please review the changes and merge if appropriate. |
| 223 | + branch: auto-format-${{ github.event.pull_request.number }} |
| 224 | + base: ${{ github.head_ref }} |
| 225 | + delete-branch: true |
| 226 | + labels: | |
| 227 | + automated pr |
| 228 | + code style |
| 229 | + id: create-pr |
| 230 | + |
| 231 | + - name: Comment on original PR |
| 232 | + if: steps.format_check.outputs.format_needs_fix == 'true' && steps.create-pr.outputs.pull-request-number && steps.find_pr.outputs.result == '' |
| 233 | + uses: actions/github-script@v7 |
| 234 | + with: |
| 235 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 236 | + script: | |
| 237 | + const prNumber = ${{ github.event.pull_request.number }}; |
| 238 | + const formatPrNumber = ${{ steps.create-pr.outputs.pull-request-number }}; |
| 239 | + const formatPrUrl = `https://github.com/${{ github.repository }}/pull/${formatPrNumber}`; |
| 240 | + |
| 241 | + await github.rest.issues.createComment({ |
| 242 | + owner: context.repo.owner, |
| 243 | + repo: context.repo.repo, |
| 244 | + issue_number: prNumber, |
| 245 | + body: `## 🤖 Code Formatting |
| 246 | + |
| 247 | + This PR has some code formatting issues. I've created [PR #${formatPrNumber}](${formatPrUrl}) with the necessary formatting changes. |
| 248 | + |
| 249 | + You can merge that PR into this branch to fix the code style check. |
| 250 | + |
| 251 | + Alternatively, you can run \`make format\` locally and push the changes yourself.` |
| 252 | + }); |
| 253 | + |
| 254 | + - name: Comment on original PR for updated formatting PR |
| 255 | + if: steps.format_check.outputs.format_needs_fix == 'true' && steps.create-pr.outputs.pull-request-number && steps.find_pr.outputs.result != '' |
| 256 | + uses: actions/github-script@v7 |
| 257 | + with: |
| 258 | + github-token: ${{ secrets.GITHUB_TOKEN }} |
| 259 | + script: | |
| 260 | + const prNumber = ${{ github.event.pull_request.number }}; |
| 261 | + const formatPrNumber = ${{ steps.create-pr.outputs.pull-request-number }}; |
| 262 | + const formatPrUrl = `https://github.com/${{ github.repository }}/pull/${formatPrNumber}`; |
| 263 | + |
| 264 | + await github.rest.issues.createComment({ |
| 265 | + owner: context.repo.owner, |
| 266 | + repo: context.repo.repo, |
| 267 | + issue_number: prNumber, |
| 268 | + body: `## 🤖 Code Formatting |
| 269 | + |
| 270 | + Your PR still has some code formatting issues. I've updated [PR #${formatPrNumber}](${formatPrUrl}) with the necessary formatting changes. |
| 271 | + |
| 272 | + You can merge that PR into this branch to fix the code style check. |
| 273 | + |
| 274 | + Alternatively, you can run \`make format\` locally and push the changes yourself.` |
| 275 | + }); |
| 276 | + |
| 277 | + # Fail the job if formatting was needed and applied |
| 278 | + - name: Fail if formatting was needed |
| 279 | + if: steps.format_check.outputs.format_needs_fix == 'true' |
| 280 | + run: | |
| 281 | + 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." |
| 282 | + exit 1 |
123 | 283 |
|
0 commit comments