|
| 1 | +name: PR Risk Experiment |
| 2 | + |
| 3 | +on: |
| 4 | + # The action reads PR Git objects through the API; no step checks out or runs PR code. |
| 5 | + pull_request_target: |
| 6 | + types: [opened, synchronize, reopened, ready_for_review, edited] |
| 7 | + |
| 8 | +permissions: |
| 9 | + contents: read |
| 10 | + pull-requests: write |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: pr-risk-${{ github.event.pull_request.number }} |
| 14 | + cancel-in-progress: true |
| 15 | + |
| 16 | +jobs: |
| 17 | + risk: |
| 18 | + name: Score PR risk |
| 19 | + runs-on: ubuntu-24.04 |
| 20 | + timeout-minutes: 10 |
| 21 | + if: github.event.pull_request.draft == false |
| 22 | + env: |
| 23 | + RESULT_PATH: risk-pr-result.json |
| 24 | + |
| 25 | + steps: |
| 26 | + - name: Classify PR risk |
| 27 | + id: risk |
| 28 | + uses: getsentry/pr-risk-action@1cdeab5ada06969e5376666b3a79f444c2f41a67 |
| 29 | + env: |
| 30 | + AI_GATEWAY_API_KEY: ${{ secrets.AI_GATEWAY_API_KEY }} |
| 31 | + with: |
| 32 | + repo: ${{ github.repository }} |
| 33 | + pr-number: ${{ github.event.pull_request.number }} |
| 34 | + expected-head-sha: ${{ github.event.pull_request.head.sha }} |
| 35 | + result-path: ${{ env.RESULT_PATH }} |
| 36 | + |
| 37 | + - name: Publish risk label |
| 38 | + if: ${{ !cancelled() && steps.risk.outcome != 'skipped' }} |
| 39 | + uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 |
| 40 | + env: |
| 41 | + PR_NUMBER: ${{ github.event.pull_request.number }} |
| 42 | + with: |
| 43 | + script: | |
| 44 | + const fs = require('node:fs'); |
| 45 | + if (!fs.existsSync(process.env.RESULT_PATH)) { |
| 46 | + core.warning('Risk analysis did not produce a result; no label was published.'); |
| 47 | + return; |
| 48 | + } |
| 49 | + const result = JSON.parse(fs.readFileSync(process.env.RESULT_PATH, 'utf8')); |
| 50 | + const prNumber = Number(process.env.PR_NUMBER); |
| 51 | + const repo = context.repo; |
| 52 | + const colors = { low: '0E8A16', medium: 'FBCA04', high: 'D93F0B' }; |
| 53 | + const riskLabels = Object.keys(colors).map(risk => `risk: ${risk}`); |
| 54 | + if (!Number.isSafeInteger(prNumber) || prNumber <= 0 |
| 55 | + || result.number !== prNumber || result.repo !== `${repo.owner}/${repo.repo}` |
| 56 | + || typeof result.status !== 'string' |
| 57 | + || (result.status === 'ok' && !Object.hasOwn(colors, result.risk_label))) { |
| 58 | + throw new Error('Risk result does not match this PR or a valid risk class.'); |
| 59 | + } |
| 60 | + const snapshot = result.snapshot; |
| 61 | + if (result.status === 'pr_changed' || !snapshot?.source_head_sha || !snapshot?.source_base_sha) { |
| 62 | + core.warning('Risk analysis has no current revision; no label was published.'); |
| 63 | + return; |
| 64 | + } |
| 65 | + const { data: pr } = await github.rest.pulls.get({ ...repo, pull_number: prNumber }); |
| 66 | + if (pr.state !== 'open' || pr.head.sha !== snapshot.source_head_sha |
| 67 | + || pr.base.sha !== snapshot.source_base_sha) { |
| 68 | + core.warning('PR changed after risk analysis; no label was published.'); |
| 69 | + return; |
| 70 | + } |
| 71 | + const target = result.status === 'ok' ? `risk: ${result.risk_label}` : null; |
| 72 | + const existing = pr.labels.map(label => label.name); |
| 73 | + if (target && !existing.includes(target)) { |
| 74 | + try { |
| 75 | + await github.rest.issues.getLabel({ ...repo, name: target }); |
| 76 | + } catch (error) { |
| 77 | + if (error.status !== 404) throw error; |
| 78 | + try { |
| 79 | + await github.rest.issues.createLabel({ |
| 80 | + ...repo, name: target, color: colors[result.risk_label], |
| 81 | + description: `PR risk score: ${result.risk_label}`, |
| 82 | + }); |
| 83 | + } catch (error) { |
| 84 | + if (error.status !== 422) throw error; |
| 85 | + // Another PR may have created the shared repository label. |
| 86 | + await github.rest.issues.getLabel({ ...repo, name: target }); |
| 87 | + } |
| 88 | + } |
| 89 | + await github.rest.issues.addLabels({ ...repo, issue_number: prNumber, labels: [target] }); |
| 90 | + } |
| 91 | + // A failed classification must not leave an earlier low/medium/high verdict. |
| 92 | + for (const name of existing.filter(name => riskLabels.includes(name) && name !== target)) { |
| 93 | + try { |
| 94 | + await github.rest.issues.removeLabel({ ...repo, issue_number: prNumber, name }); |
| 95 | + } catch (error) { |
| 96 | + if (error.status !== 404) throw error; |
| 97 | + } |
| 98 | + } |
| 99 | + const { data: latest } = await github.rest.pulls.get({ ...repo, pull_number: prNumber }); |
| 100 | + if (target && (latest.state !== 'open' || latest.head.sha !== pr.head.sha |
| 101 | + || latest.base.sha !== pr.base.sha || latest.title !== pr.title || latest.body !== pr.body)) { |
| 102 | + await github.rest.issues.removeLabel({ ...repo, issue_number: prNumber, name: target }); |
| 103 | + core.warning('PR changed while publishing; removed the outdated risk label.'); |
| 104 | + return; |
| 105 | + } |
| 106 | + if (!target) core.warning(`PR remains unclassified: ${result.status}. Previous risk labels were cleared.`); |
| 107 | + core.info(target ? `Published ${target}` : 'No risk label published.'); |
| 108 | +
|
| 109 | + - name: Upload risk result |
| 110 | + if: always() |
| 111 | + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 |
| 112 | + with: |
| 113 | + name: risk-pr-result |
| 114 | + path: ${{ env.RESULT_PATH }} |
| 115 | + if-no-files-found: warn |
| 116 | + retention-days: 30 |
0 commit comments