Skip to content

Derive accepted PR labels from the repository #48

Derive accepted PR labels from the repository

Derive accepted PR labels from the repository #48

Workflow file for this run

name: PR label
on:
pull_request:
types: [opened, reopened, synchronize, labeled, unlabeled]
permissions:
issues: read
pull-requests: read
jobs:
labels:
runs-on: ubuntu-latest
steps:
- name: Require a repository label
uses: actions/github-script@v9
with:
script: |
const pullRequest = context.payload.pull_request;
// Dependabot and friends label their own PRs (dependencies,
// javascript, github_actions) and cannot be asked to pick a
// changelog label, so the gate does not apply to them. Passing
// rather than skipping the job keeps the check reportable if it
// ever becomes a required status check.
if (pullRequest.user.type === "Bot") {
core.info(
`Skipping the changelog label check for bot author ${pullRequest.user.login}.`,
);
return;
}
const repositoryLabels = await github.paginate(
github.rest.issues.listLabelsForRepo,
{ ...context.repo, per_page: 100 },
(response) => response.data.map((label) => label.name),
);
const validLabels = new Set(repositoryLabels);
const matched = pullRequest.labels
.map((label) => label.name)
.filter((label) => validLabels.has(label));
if (matched.length === 0) {
core.setFailed(
`This PR needs at least one repository label. Available labels: ${repositoryLabels.sort().join(", ")}.`,
);
}