Repository navigation
Keep PR labels working without pull_request_target
#328
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
+174
−47
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,26 @@ | ||
| // Finds the pull request into `base` a "PR event" run was started for. The run | ||
| // of a fork does not name its pull request, so match its head repository, | ||
| // branch and exact commit. Returns null unless exactly one pull request matches. | ||
| async function findPullRequest({ github, context, core, base, state = 'open' }) { | ||
| const run = context.payload.workflow_run; | ||
| if (!run.head_repository) { | ||
| core.info('The head repository of this run no longer exists; nothing to do.'); | ||
| return null; | ||
| } | ||
| const pulls = await github.paginate(github.rest.pulls.list, { | ||
| ...context.repo, | ||
| state, | ||
| base, | ||
| head: `${run.head_repository.owner.login}:${run.head_branch}`, | ||
| per_page: 100, | ||
| }); | ||
| const matches = pulls.filter(pr => | ||
| pr.head.sha === run.head_sha && pr.head.repo?.id === run.head_repository.id); | ||
| if (matches.length !== 1) { | ||
| core.info(`${matches.length} ${state} pull requests have head ${run.head_sha}; nothing to do.`); | ||
| return null; | ||
| } | ||
| return matches[0]; | ||
| } | ||
|
|
||
| module.exports = { findPullRequest }; | ||
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,47 @@ | ||
| // The release-notes labels a pull request can carry. The pull request | ||
| // template puts each one in backticks at the end of a "Types of changes" | ||
| // checkbox bullet, e.g. - [x] CI / workflow change: `ci` | ||
| const KNOWN = [ | ||
| 'breaking-change', 'bugfix', 'refactor', | ||
| 'new-feature', 'enhancement', 'maintenance', | ||
| 'ci', 'dependencies', 'documentation', | ||
| ]; | ||
|
|
||
| // Returns { label } for a ticked box, { existing } for a known label already on the PR, else { error }. | ||
| function chooseLabel(pr) { | ||
| const re = /^\s*-\s*\[x\][^`\n]*`([^`]+)`/gim; | ||
| const ticked = []; | ||
| for (const m of (pr.body || '').matchAll(re)) { | ||
| const name = m[1].trim(); | ||
| if (KNOWN.includes(name) && !ticked.includes(name)) { | ||
| ticked.push(name); | ||
| } | ||
| } | ||
| if (ticked.length > 1) { | ||
| return { | ||
| error: `Multiple "Types of changes" checkboxes are ticked ` + | ||
| `(${ticked.join(', ')}). Tick exactly one.`, | ||
| }; | ||
| } | ||
| // The description is the source of truth, so a ticked box always wins. | ||
| // That keeps the label in step when someone reconsiders the type after | ||
| // opening. Only with nothing ticked does a label already on the pull | ||
| // request count, covering pull requests opened without the template. | ||
| if (ticked.length === 1) { | ||
| return { label: ticked[0] }; | ||
| } | ||
| // Two or more known labels are ambiguous, so those fail rather than guess. | ||
| const existing = (pr.labels || []) | ||
| .map(l => l.name) | ||
| .filter(n => KNOWN.includes(n)); | ||
| if (existing.length === 1) { | ||
| return { existing: existing[0] }; | ||
| } | ||
| return { | ||
| error: 'No "Types of changes" checkbox is ticked in the PR ' + | ||
| 'description. Edit the description and tick exactly ' + | ||
| 'one box so this PR can be release-noted.', | ||
| }; | ||
| } | ||
|
|
||
| module.exports = { KNOWN, chooseLabel }; |
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,31 @@ | ||
| name: PR event | ||
|
|
||
| # Carries no data and has no permissions. Workflows that need to act on pull | ||
| # requests from forks listen for this one through workflow_run, then read the | ||
| # pull request from the API with their own token. The run name holds the event | ||
| # action and, for a label change, the label, so a listener can pick the events | ||
| # it handles. A pull request controls its own copy of this file, so a listener | ||
| # never trusts the run name for anything else. | ||
| run-name: ${{ github.event.action }} ${{ github.event.label.name }} | ||
|
|
||
| on: | ||
| pull_request: | ||
| types: | ||
| - opened | ||
| - reopened | ||
| - synchronize | ||
| - edited | ||
| - ready_for_review | ||
| - labeled | ||
| - unlabeled | ||
|
|
||
| permissions: {} | ||
|
|
||
| jobs: | ||
| notify: | ||
| name: Notify | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 2 | ||
| steps: | ||
| - name: Nothing to do | ||
| run: "true" |
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
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.