Skip to content

Commit cf14c37

Browse files
committed
add auto-labeler workflow
1 parent 9365c21 commit cf14c37

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

.github/labeler.yml

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

.github/workflows/labeler.yml

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

0 commit comments

Comments
 (0)