Add Copilot agent to lint CI for automatic lint fix on PRs#2407
Add Copilot agent to lint CI for automatic lint fix on PRs#2407
Conversation
Agent-Logs-Url: https://github.com/microsoft/Olive/sessions/8ec606c9-d765-438a-9810-012c2033d057 Co-authored-by: xiaoyu-work <85524621+xiaoyu-work@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Adds a follow-up GitHub Actions job that, when linting fails on same-repo pull requests, posts a deduplicated PR comment mentioning @copilot with steps to reproduce and fix lint issues.
Changes:
- Introduces a new
copilot-fix-lintjob in the lint workflow. - Adds logic to avoid duplicate “request Copilot” comments via an HTML marker.
- Includes a link to the failed workflow run and local reproduction commands in the PR comment.
.github/workflows/lint.yml
Outdated
| # Only run when lint fails on pull requests from the same repo (not forks) | ||
| if: >- | ||
| failure() |
There was a problem hiding this comment.
The job-level condition is broader than the PR description and may not run when intended. failure() can become true due to failures in other jobs (for example optional-lint), and dependent jobs are often skipped when needs fails unless you include always(). Prefer gating explicitly on the dependency result, for example checking needs.lint-python-format.result == 'failure' and combining with always() plus the pull_request and non-fork checks.
| # Only run when lint fails on pull requests from the same repo (not forks) | |
| if: >- | |
| failure() | |
| # 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 | ||
| permissions: |
There was a problem hiding this comment.
actions/github-script is calling github.rest.issues.* APIs (listComments/createComment), which require the issues permission. This job currently sets only pull-requests: write, and job-level permissions overrides other scopes to none, so the API calls can fail with 403. Add issues: write (and keep pull-requests: write only if needed).
| permissions: | |
| permissions: | |
| issues: write |
.github/workflows/lint.yml
Outdated
| // Avoid duplicate comments on workflow re-runs | ||
| const { data: comments } = await github.rest.issues.listComments({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: prNumber, | ||
| per_page: 100, | ||
| }); |
There was a problem hiding this comment.
Deduplication can miss prior markers because only the first 100 comments are fetched. If a PR has more than 100 comments, the marker could be on a later page and this job would post a duplicate request. Consider paginating through comments (or searching most-recent pages first), or updating an existing comment instead of creating a new one.
| // Avoid duplicate comments on workflow re-runs | |
| const { data: comments } = await github.rest.issues.listComments({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| issue_number: prNumber, | |
| per_page: 100, | |
| }); | |
| // 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; | ||
| } |
There was a problem hiding this comment.
There is a race window where two workflow runs (for example a re-run started before the prior run posts the comment) can both pass the "no marker" check and create duplicate comments. If duplicates are a concern, consider adding a workflow concurrency group for this job/workflow or switching to an idempotent update strategy (for example editing an existing marker comment).
Agent-Logs-Url: https://github.com/microsoft/Olive/sessions/950d51d5-2dc1-4df4-a471-7577bbadea9a Co-authored-by: xiaoyu-work <85524621+xiaoyu-work@users.noreply.github.com>
copilot-fix-lintjob that triggers whenlint-python-formatfails on a PRalways() && needs.lint-python-format.result == 'failure'instead offailure()to gate explicitly on the lint job resultissues: writepermission since the script usesgithub.rest.issues.*APIsgithub.paginateinstead of singlelistCommentscall to handle PRs with 100+ commentsconcurrencygroup to prevent race condition between concurrent workflow runs