-
Notifications
You must be signed in to change notification settings - Fork 1.3k
ci: add PR auto-labeler and auto-generated release notes #301
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
6 commits
Select commit
Hold shift + click to select a range
714cbb6
ci: add PR auto-labeler and auto-generated release notes
Wibias 3861086
ci(pr-labeler): nudge PR creator when title has no conventional prefix
Wibias a7f67a2
fix(pr-labeler): use pull_request_target to fix 403 on fork PRs
Wibias 26c2dc2
fix(pr-labeler): clear stale type labels when prefix is removed from …
Wibias bd2a0ba
fix: guard Object.prototype leak in prefix lookup; fix gh release edi…
Wibias 266d37e
fix(pr-labeler): paginate comment lookup to avoid duplicate nudge on …
Wibias 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,24 @@ | ||
| # GitHub release notes category config. | ||
| # Maps PR labels (applied by pr-labeler.yml) to release note section headings. | ||
| # Docs: https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes | ||
|
|
||
| changelog: | ||
| exclude: | ||
| labels: | ||
| - skip-changelog | ||
| categories: | ||
| - title: New Features | ||
| labels: | ||
| - enhancement | ||
| - title: Bug Fixes | ||
| labels: | ||
| - bug | ||
| - title: Documentation | ||
| labels: | ||
| - documentation | ||
| - title: Chores | ||
| labels: | ||
| - chore | ||
| - title: Other Changes | ||
| labels: | ||
| - "*" |
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,140 @@ | ||
| name: PR Labeler | ||
|
|
||
| # Automatically applies a single type label to every PR based on its title | ||
| # prefix (conventional-commits style). No secrets or external services needed. | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, edited, synchronize] | ||
|
|
||
| concurrency: | ||
| group: pr-labeler-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| permissions: | ||
| contents: read | ||
| pull-requests: write | ||
| issues: write | ||
|
|
||
| jobs: | ||
| label: | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: Apply type label from PR title | ||
| uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1 | ||
| with: | ||
| script: | | ||
| const title = context.payload.pull_request.title || ''; | ||
| const pr = context.payload.pull_request.number; | ||
|
|
||
| // Map conventional-commit prefix -> GitHub label name. | ||
| const PREFIX_TO_LABEL = { | ||
| 'feat': 'enhancement', | ||
| 'feature': 'enhancement', | ||
| 'fix': 'bug', | ||
| 'bugfix': 'bug', | ||
| 'hotfix': 'bug', | ||
| 'docs': 'documentation', | ||
| 'doc': 'documentation', | ||
| 'chore': 'chore', | ||
| 'refactor': 'chore', | ||
| 'style': 'chore', | ||
| 'test': 'chore', | ||
| 'tests': 'chore', | ||
| 'ci': 'chore', | ||
| 'build': 'chore', | ||
| 'perf': 'enhancement', | ||
| 'revert': 'chore', | ||
| }; | ||
|
|
||
| const TYPE_LABELS = new Set(Object.values(PREFIX_TO_LABEL)); | ||
|
|
||
| // Match "prefix:" or "prefix(scope):" at the start of the title. | ||
| const match = title.match(/^([a-zA-Z]+)(?:\([^)]*\))?[!]?\s*:/); | ||
| const key = match ? match[1].toLowerCase() : null; | ||
| const detected = key && Object.prototype.hasOwnProperty.call(PREFIX_TO_LABEL, key) | ||
| ? PREFIX_TO_LABEL[key] | ||
| : null; | ||
|
|
||
| if (!detected) { | ||
| // Clear any stale type labels left from a previous prefix. | ||
| const { data: staleCheck } = await github.rest.issues.listLabelsOnIssue({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr, | ||
| }); | ||
| for (const label of staleCheck) { | ||
| if (TYPE_LABELS.has(label.name)) { | ||
| await github.rest.issues.removeLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr, | ||
| name: label.name, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| const creator = context.payload.pull_request.user.login; | ||
| const allComments = await github.paginate(github.rest.issues.listComments, { | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr, | ||
| }); | ||
| const alreadyCommented = allComments.some( | ||
| c => c.user.login === 'github-actions[bot]' && | ||
| c.body.includes('<!-- opencodex-label-nudge -->') | ||
| ); | ||
| if (!alreadyCommented) { | ||
| await github.rest.issues.createComment({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr, | ||
| body: [ | ||
| '<!-- opencodex-label-nudge -->', | ||
| `Hey @${creator} — could you add a [conventional-commit](https://www.conventionalcommits.org) prefix to the PR title? This lets the release notes generator categorize it automatically.`, | ||
| '', | ||
| 'Examples:', | ||
| '- `feat: Add Cursor Router optimization levels`', | ||
| '- `fix: Resolve auth token refresh loop`', | ||
| '- `docs: Update provider setup guide`', | ||
| '- `chore: Bump bun to 1.3.15`', | ||
| '', | ||
| 'Once updated, this workflow will re-run and apply the label automatically.', | ||
| ].join('\n'), | ||
| }); | ||
| } | ||
| return; | ||
|
Wibias marked this conversation as resolved.
|
||
| } | ||
|
|
||
| // Read current labels on the PR. | ||
| const { data: currentLabels } = await github.rest.issues.listLabelsOnIssue({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr, | ||
| }); | ||
| const current = new Set(currentLabels.map(l => l.name)); | ||
|
|
||
| // Remove any stale type label that no longer matches. | ||
| for (const label of TYPE_LABELS) { | ||
| if (current.has(label) && label !== detected) { | ||
| await github.rest.issues.removeLabel({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr, | ||
| name: label, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // Add the detected label if not already present. | ||
| if (!current.has(detected)) { | ||
| await github.rest.issues.addLabels({ | ||
| owner: context.repo.owner, | ||
| repo: context.repo.repo, | ||
| issue_number: pr, | ||
| labels: [detected], | ||
| }); | ||
| core.info(`Applied label "${detected}" to PR #${pr}`); | ||
| } else { | ||
| core.info(`Label "${detected}" already present on PR #${pr}`); | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.