Skip to content

Commit a76280b

Browse files
authored
ci: Add auto-label GH action (#5163)
### Description Add a GH action that auto-labels PRs based on title. If a PR is opened or edited, the action: - reads the existing labels - if there already is a Changelog label, it terminates early to not overwrite whatever the developer chose - if there is no Changelog label, it reads the PR title and matches it against a map of common patterns - if it matches, the label is added - if there is no match, it doesn't add a label #### Issues <!-- * resolves: #1234 * resolves: LIN-1234 --> #### Reminders - Please add tests to validate your changes, and lint your code using `tox -e linters`. - Add GH Issue ID _&_ Linear ID (if applicable) - PR title should use [conventional commit](https://develop.sentry.dev/engineering-practices/commit-messages/#type) style (`feat:`, `fix:`, `ref:`, `meta:`) - For external contributors: [CONTRIBUTING.md](https://github.com/getsentry/sentry-python/blob/master/CONTRIBUTING.md), [Sentry SDK development docs](https://develop.sentry.dev/sdk/), [Discord community](https://discord.gg/Ww9hbqr)
1 parent b528f32 commit a76280b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

.github/release.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# This configuration is used by Craft to categorize changelog entries based on
2+
# PR labels. To avoid some manual work, there is a PR labeling GitHub action in
3+
# .github/workflows/pr-labeler.yml that adds a changelog label to PRs based on
4+
# the title.
5+
16
changelog:
27
exclude:
38
labels:

.github/workflows/pr-labeler.yml

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# This action adds changelog labels to PRs that are then used by the release
2+
# notes generator in Craft. The configuration for which labels map to what
3+
# changelog categories can be found in .github/release.yml.
4+
5+
name: Label PR for Changelog
6+
7+
on:
8+
pull_request:
9+
types: [opened, edited]
10+
11+
permissions:
12+
pull-requests: write
13+
14+
jobs:
15+
label:
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Add changelog label
19+
uses: actions/github-script@v7
20+
with:
21+
script: |
22+
const title = context.payload.pull_request.title.toLowerCase();
23+
const prNumber = context.payload.pull_request.number;
24+
25+
// Get current labels
26+
const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({
27+
owner: context.repo.owner,
28+
repo: context.repo.repo,
29+
issue_number: prNumber,
30+
});
31+
32+
// Check if a Changelog label already exists
33+
const hasChangelogLabel = currentLabels.some(label =>
34+
label.name.startsWith('Changelog:') || label.name === 'skip-changelog'
35+
);
36+
37+
if (hasChangelogLabel) {
38+
console.log('PR already has a Changelog label, skipping');
39+
return;
40+
}
41+
42+
// Determine which label to apply
43+
let newLabel = null;
44+
45+
if (title.includes('deprecate')) {
46+
newLabel = 'Changelog: Deprecation';
47+
} else if (title.startsWith('feat')) {
48+
newLabel = 'Changelog: Feature';
49+
} else if (title.startsWith('fix') || title.startsWith('bugfix')) {
50+
newLabel = 'Changelog: Bugfix';
51+
} else if (title.startsWith('docs')) {
52+
newLabel = 'Changelog: Docs';
53+
} else if (title.startsWith('ref') || title.startsWith('test')) {
54+
newLabel = 'Changelog: Internal';
55+
} else if (title.startsWith('ci') || title.startsWith('build')) {
56+
newLabel = 'skip-changelog';
57+
}
58+
59+
// Apply the new label if one was determined
60+
if (newLabel) {
61+
await github.rest.issues.addLabels({
62+
owner: context.repo.owner,
63+
repo: context.repo.repo,
64+
issue_number: prNumber,
65+
labels: [newLabel],
66+
});
67+
68+
console.log(`Applied label: ${newLabel}`);
69+
} else {
70+
console.log('No matching label pattern found in PR title, please add manually');
71+
}
72+

0 commit comments

Comments
 (0)