Skip to content

Instantly share code, notes, and snippets.

@dohooo
Created October 31, 2024 07:01
Show Gist options
  • Save dohooo/94e0eb2b12a16acec59b462e512d39e6 to your computer and use it in GitHub Desktop.
Save dohooo/94e0eb2b12a16acec59b462e512d39e6 to your computer and use it in GitHub Desktop.
Auto add the target branch as a label to the PR
name: PR Branch Labeler
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
label-branch:
runs-on: ubuntu-latest
permissions:
pull-requests: write
steps:
- name: Add branch label
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const targetBranch = context.payload.pull_request.base.ref;
const labelName = `branch:${targetBranch}`;
const labels = await github.rest.issues.listLabelsOnIssue({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number
});
for (const label of labels.data) {
if (label.name.startsWith('branch:')) {
await github.rest.issues.removeLabel({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
name: label.name
});
}
}
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.payload.pull_request.number,
labels: [labelName]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment