Skip to content

Commit edc87d6

Browse files
feat(pr-classifiers): Add an action that runs the PR classifiers (#188)
1 parent bce11fe commit edc87d6

4 files changed

Lines changed: 196 additions & 0 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
### Features
66

7+
- PR Classifiers - Add an action that runs the internal `getsentry/classifiers` action on a pull request, so repositories no longer copy the token, checkout and upload steps. The fact sheet is read from the repository itself, at `.github/classifiers.toml`. ([#188](https://github.com/getsentry/github-workflows/pull/188))
78
- Skip CI - Add an action that tells whether a push to a pull request only changes files that need no CI, such as Markdown, since a head whose run passed. Jobs gated on its `skip` output don't run for a CHANGELOG fix-up. ([#187](https://github.com/getsentry/github-workflows/pull/187))
89

910
## 3.5.0

‎README.md‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ Tells whether a push to a pull request only changes files that need no CI, such
2828

2929
**[📖 View full documentation →](skip-ci/README.md)**
3030

31+
### PR Classifiers
32+
33+
Classifies a pull request with the internal classifiers action and labels it, without checking out or running the pull request's code.
34+
35+
**[📖 View full documentation →](pr-classifiers/README.md)**
36+
3137
## Legacy Reusable Workflows (v2)
3238

3339
> ⚠️ **Deprecated**: Reusable workflows have been converted to composite actions in v3. Please migrate to the composite actions above.

‎pr-classifiers/README.md‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# PR Classifiers
2+
3+
Classifies a pull request with the internal [getsentry/classifiers](https://github.com/getsentry/classifiers) action and puts the result on the pull request. Which classifiers run, and the labels they publish, are documented in that repository.
4+
5+
This action wraps the steps every repository used to copy: getting a token for the internal repository, checking it out at a pinned commit, running its action and uploading the result. The label is a hint for reviewers, not an approval to merge.
6+
7+
## Usage
8+
9+
Create `.github/workflows/pr-classifiers.yml` in your repository:
10+
11+
```yaml
12+
name: PR classifiers
13+
14+
on:
15+
pull_request_target:
16+
# Keep `edited`: it is the only event fired when the base branch changes.
17+
types: [opened, synchronize, reopened, ready_for_review, edited, converted_to_draft]
18+
19+
permissions: {}
20+
21+
concurrency:
22+
# Two groups per pull request; do not simplify this expression. See "Concurrency" below.
23+
group: pr-classifiers-${{ github.event.pull_request.number }}-${{ github.event.action == 'edited' && !github.event.changes.base && 'text' || 'score' }}
24+
cancel-in-progress: false
25+
26+
jobs:
27+
classify:
28+
name: PR classifiers
29+
runs-on: ubuntu-latest
30+
timeout-minutes: 10
31+
permissions:
32+
contents: read
33+
pull-requests: write
34+
steps:
35+
- uses: getsentry/github-workflows/pr-classifiers@<sha>
36+
with:
37+
app-client-id: ${{ vars.CLASSIFIERS_APP_CLIENT_ID }}
38+
app-private-key: ${{ secrets.CLASSIFIERS_APP_KEY }}
39+
ai-gateway-api-key: ${{ secrets.AI_GATEWAY_API_KEY }}
40+
```
41+
42+
Pin to a specific commit SHA, never a branch or tag: the job holds secrets on pull requests from forks.
43+
44+
The repository also needs a fact sheet at `.github/classifiers.toml` on its default branch. It tells the classifier which paths ship, where that code runs, and which are tests, docs or build configuration. The format is described in the [classifiers documentation](https://github.com/getsentry/classifiers/blob/main/classifiers/pr/risk/README.md#fact-sheets). A repository without a fact sheet is not classified, and the job stays green.
45+
46+
## Rules for the calling workflow
47+
48+
A composite action cannot set a workflow's trigger, permissions or concurrency, so these stay in your workflow file:
49+
50+
- **Never check out or run code from the pull request in this job.** The workflow runs on `pull_request_target` and holds secrets. The classifier reads the pull request's commits itself and executes none of them.
51+
- **Permissions.** `permissions: {}` at the top; `contents: read` and `pull-requests: write` on the one job. `pull-requests: write` is only needed for `publish-label` and `publish-comment`.
52+
- **No `workflow_dispatch`** if the result will gate anything: a manual run has no head commit to check against.
53+
- **No condition on drafts.** Drafts are scored.
54+
55+
### Concurrency
56+
57+
Keep the `concurrency` block exactly as in the example. It puts runs started by a title or description edit in a different group from the runs that score a commit. GitHub keeps only one pending run per group, so in one shared group a pending edit run would replace the pending run for a new commit and then be skipped, leaving the previous commit's label on the new commit. `cancel-in-progress: false` lets every started run reach its label clear.
58+
59+
## Inputs
60+
61+
| Input | Required | Default | Description |
62+
|-------|----------|---------|-------------|
63+
| `app-client-id` | Yes | | Client ID of the GitHub App that can read `getsentry/classifiers` (the client ID, not the numeric App ID) |
64+
| `app-private-key` | Yes | | Private key of that GitHub App |
65+
| `ai-gateway-api-key` | Yes | | AI Gateway key for the model call. Use a dedicated key with a spend cap: anyone can start runs by opening a pull request |
66+
| `classifiers-ref` | No | pinned commit | Commit of `getsentry/classifiers` to run |
67+
| `publish-label` | No | `true` | `true` lets the action own the labels it publishes |
68+
| `publish-comment` | No | `false` | `true` keeps one summary comment on the pull request |
69+
| `upload-result` | No | `true` | `true` uploads the public result JSON as the artifact `pr-classifiers-<number>` |
70+
71+
The fact sheet is read from the pull request's base commit, so a pull request that changes the sheet is scored with the sheet already merged, and such a pull request is never `low`.
72+
73+
## Outputs
74+
75+
| Output | Description |
76+
|--------|-------------|
77+
| `status` | How the run ended; `ok` when the pull request was classified |
78+
| `risk-label` | The final label, or empty when the status is not `ok` |
79+
| `model-label` | The model's label before the rules that block low |
80+
| `head-sha`, `base-sha` | The commits that were scored |
81+
| `label-published` | `true` only when the label was added to the unchanged pull request |
82+
| `summary` | Markdown summary of the result |
83+
84+
`summary` holds text derived from the pull request, such as file paths. Pass it through an environment variable and never put `${{ }}` inside a `run:` script.
85+
86+
The statuses, what a run publishes, and what a workflow must check before acting on a `low` are documented in the [classifiers README](https://github.com/getsentry/classifiers#statuses).

‎pr-classifiers/action.yml‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
name: 'PR Classifiers'
2+
description: 'Classifies a pull request with the internal getsentry/classifiers action, without checking out or running the pull request'
3+
author: 'Sentry'
4+
5+
inputs:
6+
app-client-id:
7+
description: 'Client ID of the GitHub App that can read getsentry/classifiers (the client ID, not the numeric App ID).'
8+
required: true
9+
app-private-key:
10+
description: 'Private key of that GitHub App.'
11+
required: true
12+
ai-gateway-api-key:
13+
description: 'AI Gateway key for the model call. Use a dedicated key with a spend cap.'
14+
required: true
15+
classifiers-ref:
16+
description: 'Commit of getsentry/classifiers to run. Leave unset to use the commit this action was released with.'
17+
required: false
18+
default: '3f2158c236f47bfb7e918a7350399769a59569ec'
19+
publish-label:
20+
description: "'true' lets the action own the labels it publishes on the pull request. Needs `pull-requests: write`."
21+
required: false
22+
default: 'true'
23+
publish-comment:
24+
description: "'true' keeps one summary comment on the pull request. Needs `pull-requests: write`."
25+
required: false
26+
default: 'false'
27+
upload-result:
28+
description: "'true' uploads the public result JSON as a workflow artifact."
29+
required: false
30+
default: 'true'
31+
32+
outputs:
33+
status:
34+
description: 'How the run ended; `ok` when the pull request was classified.'
35+
value: ${{ steps.classify.outputs.status }}
36+
risk-label:
37+
description: 'The final label (low, medium or high), or empty when the status is not ok.'
38+
value: ${{ steps.classify.outputs.risk-label }}
39+
model-label:
40+
description: "The model's label before the rules that block low; empty unless the status is ok."
41+
value: ${{ steps.classify.outputs.model-label }}
42+
head-sha:
43+
description: 'Head commit that was scored; empty unless the status is ok.'
44+
value: ${{ steps.classify.outputs.head-sha }}
45+
base-sha:
46+
description: 'Base commit that was scored; empty unless the status is ok.'
47+
value: ${{ steps.classify.outputs.base-sha }}
48+
label-published:
49+
description: "'true' only when the label was added to the unchanged pull request."
50+
value: ${{ steps.classify.outputs.label-published }}
51+
summary:
52+
description: 'Markdown summary of the result. It holds text derived from the pull request (file paths): pass it through an environment variable, never inline it in a script.'
53+
value: ${{ steps.classify.outputs.summary }}
54+
55+
runs:
56+
using: 'composite'
57+
steps:
58+
# No step here checks out or runs code from the pull request. The only checkout is
59+
# getsentry/classifiers at a pinned commit; the classifier reads the pull request's
60+
# commits itself, into an isolated bare repository.
61+
- name: Get a token for the classifiers repository
62+
id: app
63+
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
64+
with:
65+
client-id: ${{ inputs.app-client-id }}
66+
private-key: ${{ inputs.app-private-key }}
67+
repositories: classifiers
68+
# The token can read this one repository's contents and nothing else.
69+
permission-contents: read
70+
71+
- name: Fetch the classifiers
72+
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
73+
with:
74+
repository: getsentry/classifiers
75+
ref: ${{ inputs.classifiers-ref }}
76+
token: ${{ steps.app.outputs.token }}
77+
path: .classifiers
78+
persist-credentials: false
79+
80+
- name: Classify the pull request
81+
id: classify
82+
# A local path in a composite action resolves against the workspace, where the
83+
# previous step put the checkout.
84+
uses: ./.classifiers/actions/pr
85+
env:
86+
# On this step only.
87+
AI_GATEWAY_API_KEY: ${{ inputs.ai-gateway-api-key }}
88+
with:
89+
repo: ${{ github.repository }}
90+
pr-number: ${{ github.event.pull_request.number }}
91+
expected-head-sha: ${{ github.event.pull_request.head.sha }}
92+
result-path: pr-classifiers-result.json
93+
publish-label: ${{ inputs.publish-label }}
94+
publish-comment: ${{ inputs.publish-comment }}
95+
96+
- name: Upload the result
97+
if: ${{ always() && inputs.upload-result == 'true' }}
98+
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
99+
with:
100+
name: pr-classifiers-${{ github.event.pull_request.number }}
101+
path: pr-classifiers-result.json
102+
if-no-files-found: warn
103+
retention-days: 30

0 commit comments

Comments
 (0)