Skip to content

Commit a28d468

Browse files
committed
add auto-labeler workflow
1 parent fa5ec98 commit a28d468

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

.github/labeler.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
documentation:
2+
- all:
3+
- changed-files:
4+
- any-glob-to-any-file:
5+
- "**/*.md"
6+
- "**/*.rst"
7+
- all-globs-to-all-files:
8+
- "!CHANGELOG.md"
9+
- any:
10+
- head-branch:
11+
- "^docs?"
12+
- "docs?"
13+
bug:
14+
- head-branch:
15+
- "^fix"
16+
- "fix"
17+
enhancement:
18+
- head-branch:
19+
- "^feature"
20+
- "feature"
21+
- "^feat"
22+
- "feat"

.github/workflows/labeler.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Label PR
2+
3+
on:
4+
workflow_call:
5+
6+
jobs:
7+
update-pr-labels:
8+
# Only execute this job when the PR is not from a fork. If the PR is from a fork,
9+
# then the needed permission (pull-requests: write) is not granted.
10+
if: ${{ github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name }}
11+
permissions:
12+
# needed to read info like git refs and changed files
13+
contents: read
14+
# needed to make changes to a PR's labels
15+
pull-requests: write
16+
runs-on: ubuntu-latest
17+
steps:
18+
- name: Checkout cpp-linter/.github (org) repo
19+
# needed for .github/labeler.yml config (org-specific)
20+
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
21+
with:
22+
persist-credentials: false
23+
repository: cpp-linter/.github
24+
path: org-repo
25+
- uses: actions/labeler@634933edcd8ababfe52f92936142cc22ac488b1b # v6.0.1
26+
id: labeler
27+
# This step doesn't require a `git checkout`.
28+
# The above checkout step only pulls in the org config.
29+
with:
30+
# If the config file does not exist, then this action will default to
31+
# trying to read the config in project repo
32+
configuration-path: org-repo/.github/labeler.yml
33+
# allow removing labels to keep them in sync with file changes
34+
sync-labels: true
35+
36+
# The rest of this simply adds labels based on the PR title.
37+
# This isn't really necessary due to how git-cliff parses commits.
38+
# This can be removed once actions/labeler resolves the feature request:
39+
# https://github.com/actions/labeler/issues/809
40+
- name: Setup Nu shell
41+
# because using bash to manipulate arrays is nonsensical
42+
uses: hustcer/setup-nu@985d59ec83ae3e3418f9d36471cda38b9d8b9879 # v3.20
43+
with:
44+
version: ${{ vars.NUSHELL_VERSION || '*' }}
45+
- name: Label from PR title
46+
shell: nu {0}
47+
env:
48+
GH_REPO: ${{ github.repository }}
49+
GH_TOKEN: ${{ github.token }}
50+
PR_NUMBER: ${{ github.event.pull_request.number }}
51+
# includes all existing and added labels from previous step
52+
ALL_LABELS: ${{ steps.labeler.outputs.all-labels }}
53+
run: |-
54+
let title = (
55+
^gh pr view $env.PR_NUMBER --repo $env.GH_REPO --json "title" -q ".title"
56+
)
57+
print $"Analyzing title: ($title)"
58+
let labels = $env.ALL_LABELS | split row ","
59+
mut new_labels = []
60+
if (
61+
($title | str contains --ignore-case "fix")
62+
and not ("bug" in $labels)
63+
) {
64+
$new_labels = $new_labels | append "bug"
65+
}
66+
if (
67+
($title | str contains --ignore-case "feat")
68+
and not ("enhancement" in $labels)
69+
) {
70+
$new_labels = $new_labels | append "enhancement"
71+
}
72+
if ($new_labels | is-not-empty) {
73+
print $"Adding the following labels: ($new_labels | str join ' ')"
74+
gh pr edit $env.PR_NUMBER --add-label ($new_labels | str join ",")
75+
} else {
76+
print "No new labels added based on the PR title"
77+
}

0 commit comments

Comments
 (0)