-
Notifications
You must be signed in to change notification settings - Fork 292
Add Copilot agent to lint CI for automatic lint fix on PRs #2407
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -76,3 +76,66 @@ jobs: | |
| sarif_file: lintrunner.sarif | ||
| category: lintrunner | ||
| checkout_path: ${{ github.workspace }} | ||
|
|
||
| copilot-fix-lint: | ||
| name: Copilot Fix Lint Errors | ||
| needs: lint-python-format | ||
| # Only run when lint-python-format fails on pull requests from the same repo (not forks) | ||
| if: >- | ||
| always() | ||
| && needs.lint-python-format.result == 'failure' | ||
| && github.event_name == 'pull_request' | ||
| && github.event.pull_request.head.repo.full_name == github.repository | ||
| runs-on: ubuntu-latest | ||
| concurrency: | ||
| group: copilot-fix-lint-pr-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
| permissions: | ||
| issues: write | ||
| steps: | ||
| - name: Request Copilot to fix lint errors | ||
| uses: actions/github-script@v7 | ||
| with: | ||
| script: | | ||
| const prNumber = context.payload.pull_request.number; | ||
| const marker = '<!-- copilot-lint-fix -->'; | ||
|
|
||
| // Avoid duplicate comments on workflow re-runs by checking all comment pages | ||
| const comments = await github.paginate( | ||
| github.rest.issues.listComments, | ||
| { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| per_page: 100, | ||
| }, | ||
| ); | ||
|
|
||
| if (comments.some(c => c.body?.includes(marker))) { | ||
| core.info('Copilot lint fix already requested; skipping.'); | ||
| return; | ||
| } | ||
|
Comment on lines
+114
to
+117
|
||
|
|
||
| const runUrl = `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`; | ||
|
|
||
| const body = [ | ||
| marker, | ||
| '@copilot The lint check failed for this PR. Please fix all the lint errors and push the fixes to this branch.', | ||
| '', | ||
| `Failed workflow run: ${runUrl}`, | ||
| '', | ||
| 'To reproduce and fix:', | ||
| '1. Run `lintrunner --all-files` to see all lint errors', | ||
| '2. Run `lintrunner --all-files -a` to auto-fix formatting issues', | ||
| '3. Manually fix any remaining lint errors that cannot be auto-fixed', | ||
| '4. Commit and push the fixes', | ||
| ].join('\n'); | ||
|
|
||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| body: body, | ||
| }); | ||
|
|
||
| core.info(`Requested Copilot to fix lint errors on PR #${prNumber}`); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
actions/github-scriptis callinggithub.rest.issues.*APIs (listComments/createComment), which require theissuespermission. This job currently sets onlypull-requests: write, and job-levelpermissionsoverrides other scopes tonone, so the API calls can fail with 403. Addissues: write(and keeppull-requests: writeonly if needed).