diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 0ecafa039..a6d3d936e 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -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 = ''; + + // 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; + } + + 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}`);