Repository navigation
ci: Add PR risk (Jev) workflow #6615
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| name: PR Risk (Jev) | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, synchronize, reopened, ready_for_review, edited] | ||
| workflow_dispatch: | ||
| inputs: | ||
| pr_number: | ||
| description: Pull request number to analyze | ||
| required: true | ||
| type: number | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
|
|
||
| concurrency: | ||
| group: pr-risk-jev-${{ github.event.pull_request.number || inputs.pr_number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| risk: | ||
| name: PR risk (Jev) | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 10 | ||
| if: github.event_name == 'workflow_dispatch' || github.event.pull_request.draft == false | ||
| steps: | ||
| # The Action reads PR Git objects without checking out or executing PR code. | ||
| - name: Classify PR risk | ||
| id: risk | ||
| uses: getsentry/pr-risk-action@1cdeab5ada06969e5376666b3a79f444c2f41a67 # main | ||
| env: | ||
| AI_GATEWAY_API_KEY: ${{ secrets.AI_GATEWAY_API_KEY }} | ||
| with: | ||
| repo: ${{ github.repository }} | ||
| pr-number: ${{ github.event.pull_request.number || inputs.pr_number }} | ||
| expected-head-sha: ${{ github.event.pull_request.head.sha }} | ||
| result-path: risk-pr-result.json | ||
|
|
||
| - name: Publish risk label | ||
| if: ${{ !cancelled() && steps.risk.outcome != 'skipped' }} | ||
| uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0 | ||
| env: | ||
| PR_NUMBER: ${{ github.event.pull_request.number || inputs.pr_number }} | ||
| with: | ||
| script: | | ||
| // Only this trusted workflow publishes labels; PR source is never executed. | ||
| const fs = require('node:fs'); | ||
| if (!fs.existsSync('risk-pr-result.json')) { | ||
| core.warning('Risk analysis did not produce a result; no label was published.'); | ||
| return; | ||
| } | ||
| const result = JSON.parse(fs.readFileSync('risk-pr-result.json', 'utf8')); | ||
| const prNumber = Number(process.env.PR_NUMBER); | ||
| const repo = context.repo; | ||
| const colors = { low: '0E8A16', medium: 'FBCA04', high: 'B60205' }; | ||
| const riskLabels = Object.keys(colors).map(risk => `risk: ${risk}`); | ||
| if (!Number.isSafeInteger(prNumber) || prNumber <= 0 | ||
| || result.number !== prNumber || result.repo !== `${repo.owner}/${repo.repo}` | ||
| || typeof result.status !== 'string' | ||
| || (result.status === 'ok' && !Object.hasOwn(colors, result.risk_label))) { | ||
| throw new Error('Risk result does not match this PR or a valid risk class.'); | ||
| } | ||
| const snapshot = result.snapshot; | ||
| if (result.status === 'pr_changed' || !snapshot?.source_head_sha || !snapshot?.source_base_sha) { | ||
| core.warning('Risk analysis has no current revision; no label was published.'); | ||
| return; | ||
| } | ||
| const { data: pr } = await github.rest.pulls.get({ ...repo, pull_number: prNumber }); | ||
| if (pr.state !== 'open' || pr.head.sha !== snapshot.source_head_sha | ||
| || pr.base.sha !== snapshot.source_base_sha) { | ||
| core.warning('PR changed after risk analysis; no label was published.'); | ||
| return; | ||
| } | ||
| const target = result.status === 'ok' ? `risk: ${result.risk_label}` : null; | ||
| const existing = pr.labels.map(label => label.name); | ||
| if (target && !existing.includes(target)) { | ||
| try { | ||
| await github.rest.issues.getLabel({ ...repo, name: target }); | ||
| } catch (error) { | ||
| if (error.status !== 404) throw error; | ||
| try { | ||
| await github.rest.issues.createLabel({ | ||
| ...repo, name: target, color: colors[result.risk_label], | ||
| description: `PR risk score: ${result.risk_label}`, | ||
| }); | ||
| } catch (error) { | ||
| if (error.status !== 422) throw error; | ||
| // Another PR may have created the shared repository label. | ||
| await github.rest.issues.getLabel({ ...repo, name: target }); | ||
| } | ||
| } | ||
| await github.rest.issues.addLabels({ ...repo, issue_number: prNumber, labels: [target] }); | ||
| } | ||
| // A failed classification must not leave an earlier low/medium/high verdict. | ||
| for (const name of existing.filter(name => riskLabels.includes(name) && name !== target)) { | ||
| try { | ||
| await github.rest.issues.removeLabel({ ...repo, issue_number: prNumber, name }); | ||
| } catch (error) { | ||
| if (error.status !== 404) throw error; | ||
| } | ||
| } | ||
| const { data: latest } = await github.rest.pulls.get({ ...repo, pull_number: prNumber }); | ||
| if (target && (latest.state !== 'open' || latest.head.sha !== pr.head.sha | ||
| || latest.base.sha !== pr.base.sha || latest.title !== pr.title || latest.body !== pr.body)) { | ||
| await github.rest.issues.removeLabel({ ...repo, issue_number: prNumber, name: target }); | ||
| core.warning('PR changed while publishing; removed the outdated risk label.'); | ||
| return; | ||
| } | ||
| if (!target) core.warning(`PR remains unclassified: ${result.status}. Previous risk labels were cleared.`); | ||
| core.info(target ? `Published ${target}` : 'No risk label published.'); | ||
|
|
||
| - name: Upload risk result | ||
| if: always() | ||
| uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7 | ||
| with: | ||
| name: pr-risk-jev-${{ github.event.pull_request.number || inputs.pr_number }} | ||
| path: risk-pr-result.json | ||
| if-no-files-found: warn | ||
| retention-days: 30 | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Label creation missing issues write
High Severity
The workflow grants
pull-requests: writebut notissues: write.issues.createLabelneedsissues: writeonGITHUB_TOKEN, so first-use creation ofrisk: low/medium/highfails with 403, the publish step errors, and no risk label is applied.Additional Locations (1)
.github/workflows/pr-risk-jev.yml#L82-L86Reviewed by Cursor Bugbot for commit 86df059. Configure here.