Skip to content
Latchkey

How to Add or Remove PR Labels From a Workflow

github-script exposes issues.addLabels and issues.removeLabel so a workflow can adjust PR labels directly.

Call github.rest.issues.addLabels to add and github.rest.issues.removeLabel to drop a single label. Wrap removal in try/catch since removing an absent label returns 404.

Steps

  • Grant issues: write (labels live on the issues API).
  • Call addLabels with an array of names.
  • Call removeLabel for one label, catching the 404 when absent.

Workflow

.github/workflows/ci.yml
permissions:
  issues: write
  pull-requests: write
jobs:
  labels:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/github-script@v7
        with:
          script: |
            const common = {
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: context.issue.number,
            }
            await github.rest.issues.addLabels({ ...common, labels: ['needs-review'] })
            try {
              await github.rest.issues.removeLabel({ ...common, name: 'wip' })
            } catch (e) {
              if (e.status !== 404) throw e
            }

Gotchas

  • The label must already exist in the repo or addLabels creates it with a default color.
  • removeLabel throws 404 when the label is not applied; swallow only that status.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →