Add lint and format auto-fix workflow to automatically commit fixes on PR failures#7542
Add lint and format auto-fix workflow to automatically commit fixes on PR failures#7542
Conversation
|
|
@copilot I don't just want the check to exist, I want the workflow to run the lint action if the check fails and then commit the change |
|
@lesliecdubs Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub. You can ask me to try again later by mentioning me in a new comment. If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: Sorry for the inconvenience! |
|
@copilot I don't just want the check to exist, I want the workflow to automatically run the lint action on the PR if the check fails, and then commit the change |
Co-authored-by: lesliecdubs <3902488+lesliecdubs@users.noreply.github.com>
I've added a new workflow The workflow:
This ensures that lint failures are automatically resolved without requiring contributors to fix them manually. |
|
@copilot update this PR so that if the "format" check also fails, we run the proper format command and then commit the changes |
|
@lesliecdubs Unfortunately I hit an unexpected error while processing your comment. I've automatically reported this to GitHub. You can ask me to try again later by mentioning me in a new comment. If you want to contact GitHub about this error, please mention the following identifier so they can better serve you: Sorry for the inconvenience! |
|
@copilot update this PR so that if the "format" check also fails, we run the proper format command and then commit the changes |
Co-authored-by: lesliecdubs <3902488+lesliecdubs@users.noreply.github.com>
I've updated the workflow (commit 67cce29) to handle both lint and format failures: Changes:
The workflow now intelligently runs only the necessary fix commands based on which jobs failed. |
There was a problem hiding this comment.
Pull request overview
This PR adds a new GitHub Actions workflow that automatically fixes lint and formatting issues when CI checks fail on pull requests. The workflow monitors CI workflow completion, detects which specific jobs (lint or format) failed, runs the appropriate auto-fix commands, and commits the fixes back to the PR with a notification comment.
Changes:
- Added
.github/workflows/lint-autofix.yml- A workflow_run-triggered workflow that automatically applies and commits lint and format fixes when CI fails on PRs
| # Only run on pull requests where lint or format failed | ||
| if: > | ||
| github.event.workflow_run.event == 'pull_request' && | ||
| github.event.workflow_run.conclusion == 'failure' |
There was a problem hiding this comment.
The workflow will fail when attempting to auto-fix lint/format issues on pull requests from forks. When a PR comes from a fork, the GitHub App token won't have permission to push to the fork repository. The workflow should check if the PR is from a fork and skip execution in that case, similar to how other workflows in this repository handle forks (e.g., deploy_preview.yml checks github.event.pull_request.head.repo.full_name == 'primer/react').
Add a check in the job-level condition to skip fork PRs. You can check this by comparing the head repo with the base repo, or by adding a check in the "Get Pull Request" step to set an output indicating whether it's from a fork, then use that in subsequent step conditions.
| # Only run on pull requests where lint or format failed | |
| if: > | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.conclusion == 'failure' | |
| # Only run on pull requests from this repository where lint or format failed | |
| if: > | |
| github.event.workflow_run.event == 'pull_request' && | |
| github.event.workflow_run.conclusion == 'failure' && | |
| github.event.workflow_run.head_repository.full_name == github.repository |
| - name: Comment on PR | ||
| if: steps.check_jobs.outputs.should_fix == 'true' && steps.pr.outputs.number != '' | ||
| uses: actions/github-script@5c56fde4671bc2d3592fb0f2c5b5bab9ddae03b1 | ||
| with: | ||
| github-token: ${{ steps.generate_token.outputs.token }} | ||
| script: | | ||
| const lintFailed = '${{ steps.check_jobs.outputs.lint_failed }}' === 'true'; | ||
| const formatFailed = '${{ steps.check_jobs.outputs.format_failed }}' === 'true'; | ||
|
|
||
| let message = '🤖 '; | ||
| if (lintFailed && formatFailed) { | ||
| message += 'Lint and formatting issues have been automatically fixed and committed to this PR.'; | ||
| } else if (lintFailed) { | ||
| message += 'Lint issues have been automatically fixed and committed to this PR.'; | ||
| } else if (formatFailed) { | ||
| message += 'Formatting issues have been automatically fixed and committed to this PR.'; | ||
| } | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: ${{ steps.pr.outputs.number }}, | ||
| body: message | ||
| }); |
There was a problem hiding this comment.
The workflow will create a PR comment even when no fixes were actually committed. The git-auto-commit-action by default doesn't fail or skip when there are no changes to commit - it just doesn't create a commit. However, the "Comment on PR" step will still run and post a misleading message saying issues were fixed.
To fix this, you can capture the output from the git-auto-commit-action step (it sets a changes_detected output) and only run the comment step when changes were actually committed. For example:
- Add an
idto the "Commit and push changes" step - Update the "Comment on PR" condition to also check
steps.commit_step_id.outputs.changes_detected == 'true'
|
|
||
| - name: Run lint auto-fix | ||
| if: steps.check_jobs.outputs.lint_failed == 'true' && steps.pr.outputs.head_ref != '' | ||
| run: | |
There was a problem hiding this comment.
The workflow doesn't run npm run lint:md auto-fix, but the CI workflow includes a markdown linting step (npm run lint:md in the lint job). While markdownlint-cli2 doesn't have a built-in --fix flag, if markdown linting failures can be auto-fixed in the future, this workflow should be updated to include those fixes as well. Consider adding a comment noting this limitation or investigating if there's a fix command available for markdown linting.
| run: | | |
| run: | | |
| # Note: CI runs `npm run lint:md` for markdown linting, but markdownlint-cli2 | |
| # does not currently support auto-fix. If a markdown lint auto-fix command | |
| # (e.g. `npm run lint:md:fix`) is added in the future, update this step to run it. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Overview
This PR adds an automated lint and format fixing workflow that runs when lint or format checks fail on pull requests. The workflow automatically applies fixes and commits them back to the PR.
New workflow:
.github/workflows/lint-autofix.ymlThe workflow:
lintjob and/orformatjob failednpm run formatnpm run lint:fix,npm run lint:css:fixChangelog
New
.github/workflows/lint-autofix.yml- Automated lint and format fixing workflow that:npm run format) when format job failsnpm run lint:fix) when lint job failsnpm run lint:css:fix) when lint job failsgithub-actions[bot]Changed
N/A
Removed
N/A
Rollout strategy
Testing & Reviewing
The workflow will automatically trigger when:
lintand/orformatjob in failure stateTo test:
Merge checklist
Note: This PR only adds a GitHub Actions workflow file. Browser testing and integration tests are not applicable.
Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.