Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

- 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))
- 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))

## 3.5.0
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ Tells whether a push to a pull request only changes files that need no CI, such

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

### PR Classifiers

Classifies a pull request with the internal classifiers action and labels it, without checking out or running the pull request's code.

**[📖 View full documentation →](pr-classifiers/README.md)**

## Legacy Reusable Workflows (v2)

> ⚠️ **Deprecated**: Reusable workflows have been converted to composite actions in v3. Please migrate to the composite actions above.
Expand Down
86 changes: 86 additions & 0 deletions pr-classifiers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# PR Classifiers

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.

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.

## Usage

Create `.github/workflows/pr-classifiers.yml` in your repository:

```yaml
name: PR classifiers

on:
pull_request_target:
# Keep `edited`: it is the only event fired when the base branch changes.
types: [opened, synchronize, reopened, ready_for_review, edited, converted_to_draft]

permissions: {}

concurrency:
# Two groups per pull request; do not simplify this expression. See "Concurrency" below.
group: pr-classifiers-${{ github.event.pull_request.number }}-${{ github.event.action == 'edited' && !github.event.changes.base && 'text' || 'score' }}
cancel-in-progress: false

jobs:
classify:
name: PR classifiers
runs-on: ubuntu-latest
timeout-minutes: 10
permissions:
contents: read
pull-requests: write
steps:
- uses: getsentry/github-workflows/pr-classifiers@<sha>
with:
app-client-id: ${{ vars.CLASSIFIERS_APP_CLIENT_ID }}
app-private-key: ${{ secrets.CLASSIFIERS_APP_KEY }}
ai-gateway-api-key: ${{ secrets.AI_GATEWAY_API_KEY }}
```

Pin to a specific commit SHA, never a branch or tag: the job holds secrets on pull requests from forks.

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.

## Rules for the calling workflow

A composite action cannot set a workflow's trigger, permissions or concurrency, so these stay in your workflow file:

- **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.
- **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`.
- **No `workflow_dispatch`** if the result will gate anything: a manual run has no head commit to check against.
- **No condition on drafts.** Drafts are scored.

### Concurrency

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.

## Inputs

| Input | Required | Default | Description |
|-------|----------|---------|-------------|
| `app-client-id` | Yes | | Client ID of the GitHub App that can read `getsentry/classifiers` (the client ID, not the numeric App ID) |
| `app-private-key` | Yes | | Private key of that GitHub App |
| `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 |
| `classifiers-ref` | No | pinned commit | Commit of `getsentry/classifiers` to run |
| `publish-label` | No | `true` | `true` lets the action own the labels it publishes |
| `publish-comment` | No | `false` | `true` keeps one summary comment on the pull request |
| `upload-result` | No | `true` | `true` uploads the public result JSON as the artifact `pr-classifiers-<number>` |

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`.

## Outputs

| Output | Description |
|--------|-------------|
| `status` | How the run ended; `ok` when the pull request was classified |
| `risk-label` | The final label, or empty when the status is not `ok` |
| `model-label` | The model's label before the rules that block low |
| `head-sha`, `base-sha` | The commits that were scored |
| `label-published` | `true` only when the label was added to the unchanged pull request |
| `summary` | Markdown summary of the result |

`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.

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).
103 changes: 103 additions & 0 deletions pr-classifiers/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: 'PR Classifiers'
description: 'Classifies a pull request with the internal getsentry/classifiers action, without checking out or running the pull request'
author: 'Sentry'

inputs:
app-client-id:
description: 'Client ID of the GitHub App that can read getsentry/classifiers (the client ID, not the numeric App ID).'
required: true
app-private-key:
description: 'Private key of that GitHub App.'
required: true
ai-gateway-api-key:
description: 'AI Gateway key for the model call. Use a dedicated key with a spend cap.'
required: true
classifiers-ref:
description: 'Commit of getsentry/classifiers to run. Leave unset to use the commit this action was released with.'
required: false
default: '3f2158c236f47bfb7e918a7350399769a59569ec'
publish-label:
description: "'true' lets the action own the labels it publishes on the pull request. Needs `pull-requests: write`."
required: false
default: 'true'
publish-comment:
description: "'true' keeps one summary comment on the pull request. Needs `pull-requests: write`."
required: false
default: 'false'
upload-result:
description: "'true' uploads the public result JSON as a workflow artifact."
required: false
default: 'true'

outputs:
status:
description: 'How the run ended; `ok` when the pull request was classified.'
value: ${{ steps.classify.outputs.status }}
risk-label:
description: 'The final label (low, medium or high), or empty when the status is not ok.'
value: ${{ steps.classify.outputs.risk-label }}
model-label:
description: "The model's label before the rules that block low; empty unless the status is ok."
value: ${{ steps.classify.outputs.model-label }}
head-sha:
description: 'Head commit that was scored; empty unless the status is ok.'
value: ${{ steps.classify.outputs.head-sha }}
base-sha:
description: 'Base commit that was scored; empty unless the status is ok.'
value: ${{ steps.classify.outputs.base-sha }}
label-published:
description: "'true' only when the label was added to the unchanged pull request."
value: ${{ steps.classify.outputs.label-published }}
summary:
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.'
value: ${{ steps.classify.outputs.summary }}

runs:
using: 'composite'
steps:
# No step here checks out or runs code from the pull request. The only checkout is
# getsentry/classifiers at a pinned commit; the classifier reads the pull request's
# commits itself, into an isolated bare repository.
- name: Get a token for the classifiers repository
id: app
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ inputs.app-client-id }}
private-key: ${{ inputs.app-private-key }}
repositories: classifiers
# The token can read this one repository's contents and nothing else.
permission-contents: read

- name: Fetch the classifiers
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: getsentry/classifiers
ref: ${{ inputs.classifiers-ref }}
token: ${{ steps.app.outputs.token }}
path: .classifiers
persist-credentials: false

- name: Classify the pull request
id: classify
# A local path in a composite action resolves against the workspace, where the
# previous step put the checkout.
uses: ./.classifiers/actions/pr
env:
# On this step only.
AI_GATEWAY_API_KEY: ${{ inputs.ai-gateway-api-key }}
with:
repo: ${{ github.repository }}
pr-number: ${{ github.event.pull_request.number }}
expected-head-sha: ${{ github.event.pull_request.head.sha }}
result-path: pr-classifiers-result.json
publish-label: ${{ inputs.publish-label }}
publish-comment: ${{ inputs.publish-comment }}

- name: Upload the result
if: ${{ always() && inputs.upload-result == 'true' }}
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7
with:
name: pr-classifiers-${{ github.event.pull_request.number }}
path: pr-classifiers-result.json
if-no-files-found: warn
retention-days: 30
Loading