markz

One label

A package's release notes are grouped by the label on each pull request, so a pull request without one can't merge. The labels are the ones .github/notes.yml names. The job is named label, a check the main ruleset requires next to ci.

It's a workflow of its own, so a label added or taken off runs only this, not verify again. It reads one file and installs nothing, and a new commit runs it too, since a check belongs to a commit.

It reads the labels from GitHub when it runs, not from the event that started it. gh pr create --label opens the pull request and labels it in two requests, so the opened event carries no label. No run cancels another, since GitHub can count a cancelled run against the check. With the labels read live, every run sees the same ones, and running twice costs a few seconds.

name: Label

on:
  pull_request:
    branches: [main]
    types: [opened, synchronize, reopened, labeled, unlabeled]

permissions:
  contents: read
  pull-requests: read

jobs:
  label:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v7
        with:
          sparse-checkout: .github/notes.yml
          sparse-checkout-cone-mode: false
      - name: Label
        env:
          GH_TOKEN: ${{ github.token }}
          PR: ${{ github.event.pull_request.html_url }}
        run: |
          LABELS=$(gh pr view "$PR" --json labels --jq '[.labels[].name]')
          export LABELS
          node -e '
            const notes = require("node:fs").readFileSync(".github/notes.yml", "utf8");
            const known = [...notes.matchAll(/labels: \[(\w+)\]|^ +- (\w+)$/gm)].map((m) => m[1] ?? m[2]);
            const have = JSON.parse(process.env.LABELS).filter((label) => known.includes(label));
            if (have.length !== 1) {
              const found = have.length ? have.join(", ") : "none";
              console.error(`::error::Label this pull request with one of ${known.join(", ")}. It has ${found}.`);
              process.exit(1);
            }
          '