|
1 | 1 | name: agent-scan |
2 | 2 |
|
3 | 3 | on: |
4 | | - # zizmor: ignore[dangerous-triggers] - DO NOT add action/checkout in this workflow as it uses pull_request_target |
5 | 4 | pull_request_target: |
6 | 5 | types: |
7 | | - - opened |
8 | | - - reopened |
9 | | - |
10 | | -concurrency: |
11 | | - group: agent-scan-${{ github.event.pull_request.number }} |
12 | | - cancel-in-progress: true |
| 6 | + - closed |
| 7 | + - labeled |
13 | 8 |
|
14 | 9 | permissions: |
15 | | - issues: write |
16 | 10 | pull-requests: write |
17 | 11 |
|
18 | 12 | jobs: |
19 | | - agentscan: |
| 13 | + rename: |
| 14 | + if: >- |
| 15 | + github.event.pull_request.state == 'closed' |
| 16 | + && !github.event.pull_request.merged |
| 17 | + && contains(github.event.pull_request.labels.*.name, 'possible bot') |
| 18 | + && (github.event.action == 'closed' || github.event.label.name == 'possible bot') |
20 | 19 | runs-on: ubuntu-latest |
21 | 20 | steps: |
22 | | - - name: AgentScan |
23 | | - id: agentscan |
24 | | - uses: MatteoGabriele/agentscan-action@8112fb79b33fafb8506159df20129a34209ac410 # v2.5.0 |
25 | | - with: |
26 | | - github-token: ${{ secrets.GITHUB_TOKEN }} |
27 | | - mode: silent |
28 | | - - name: Handle flagged PR |
29 | | - if: contains(fromJSON('["automation","mixed"]'), steps.agentscan.outputs.classification) || steps.agentscan.outputs.community-flagged == 'true' |
30 | | - env: |
31 | | - CLASSIFICATION: ${{ steps.agentscan.outputs.classification }} |
32 | | - COMMUNITY_FLAGGED: ${{ steps.agentscan.outputs.community-flagged }} |
33 | | - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 21 | + - uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
34 | 22 | with: |
35 | 23 | script: | |
36 | | - const prNumber = context.payload.pull_request.number; |
37 | | - const classification = process.env.CLASSIFICATION; |
38 | | - const communityFlagged = process.env.COMMUNITY_FLAGGED === 'true'; |
39 | | - const shouldClose = classification === 'automation' || communityFlagged; |
40 | | -
|
41 | | - const issue = context.payload.pull_request |
42 | | - const labels = issue.labels?.map(l => l.name) || [] |
43 | | -
|
44 | | - if (!labels.includes('possible bot')) { |
45 | | - await github.rest.issues.addLabels({ |
46 | | - owner: context.repo.owner, |
47 | | - repo: context.repo.repo, |
48 | | - issue_number: prNumber, |
49 | | - labels: ['possible bot'], |
50 | | - }) |
51 | | - } |
52 | | -
|
53 | | - const comments = await github.paginate(github.rest.issues.listComments, { |
| 24 | + await github.rest.pulls.update({ |
54 | 25 | owner: context.repo.owner, |
55 | 26 | repo: context.repo.repo, |
56 | | - issue_number: prNumber, |
57 | | - per_page: 100, |
| 27 | + pull_number: context.payload.pull_request.number, |
| 28 | + title: '🚨 unwelcome pr from bot 🚨', |
58 | 29 | }) |
59 | | -
|
60 | | - const alreadyCommented = comments.some( |
61 | | - c => c.user.type === 'Bot' && c.body.includes('AI-assisted contribution guidelines') |
62 | | - ) |
63 | | -
|
64 | | - if (!alreadyCommented) { |
65 | | - const closingNote = shouldClose |
66 | | - ? "We're closing this for now as the account looks automated. If we got that wrong, please just reopen the PR and we'll take another look." |
67 | | - : 'If this was flagged in error, we apologise! 😳 Just let us know. 🙏' |
68 | | -
|
69 | | - await github.rest.issues.createComment({ |
70 | | - owner: context.repo.owner, |
71 | | - repo: context.repo.repo, |
72 | | - issue_number: prNumber, |
73 | | - body: [ |
74 | | - "We've flagged this as a potential contribution without a human behind it. We welcome the thoughtful use of AI tools when contributing, but ask all contributors to follow [two core principles](https://roe.dev/blog/using-ai-in-open-source):", |
75 | | - '', |
76 | | - '1. **Never let an LLM speak for you** - all comments, issues, and PR descriptions should be written in your own words, reflecting your own understanding.', |
77 | | - '2. **Never let an LLM think for you** - only submit contributions you fully understand and can explain.', |
78 | | - '', |
79 | | - 'Please review these AI-assisted contribution guidelines and update this contribution if needed.', |
80 | | - '', |
81 | | - closingNote, |
82 | | - ].join('\n'), |
83 | | - }) |
84 | | - } else { |
85 | | - core.info('Possible-bot comment already exists - skipping comment.') |
86 | | - } |
87 | | -
|
88 | | - if (shouldClose && issue.state === 'open' && !alreadyCommented) { |
89 | | - await github.rest.pulls.update({ |
90 | | - owner: context.repo.owner, |
91 | | - repo: context.repo.repo, |
92 | | - pull_number: prNumber, |
93 | | - state: 'closed', |
94 | | - title: '🚨 unwelcome pr from bot 🚨', |
95 | | - }) |
96 | | - } |
97 | | -
|
98 | | - const actionTaken = [ |
99 | | - 'Added `possible bot` label', |
100 | | - alreadyCommented ? null : 'posted policy comment', |
101 | | - shouldClose && !alreadyCommented ? 'closed PR' : null, |
102 | | - ].filter(Boolean).join(', ') |
103 | | -
|
104 | | - core.summary |
105 | | - .addHeading('AgentScan: Possible Bot Flag', 2) |
106 | | - .addTable([ |
107 | | - [{ data: 'Property', header: true }, { data: 'Value', header: true }], |
108 | | - ['Pull Request', `#${prNumber}`], |
109 | | - ['Classification', classification], |
110 | | - ['Community flagged', String(communityFlagged)], |
111 | | - ['Action', actionTaken || 'No action (already handled)'], |
112 | | - ]) |
113 | | -
|
114 | | - await core.summary.write() |
0 commit comments