Skip to content

Commit 348a2df

Browse files
✨ support custom suffix (#17)
* ✨ update README and action.yml to support customizable suffix for gitmoji labels * ✨ update workflow to use local action for gitmoji label creation * ✅ Test * ✅ Remove test
1 parent 60ef69c commit 348a2df

File tree

3 files changed

+59
-12
lines changed

3 files changed

+59
-12
lines changed

.github/workflows/auto-create-gitmoji-labels.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ jobs:
99
pull-requests: write
1010
repository-projects: read
1111
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v4
14+
1215
- name: Create gitmoji label
13-
uses: georgepstaylor/gitmoji-auto-label@feature/smarter-relabeling
16+
uses: ./
1417
with:
1518
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
19+
# suffix: "okay"
20+
# cleanup_other_gitmoji_labels: true

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
A simple action to automatically label PRs based on the gitmoji used in the title.
44

5-
Note: GitHub _requires_ more than just the gitmoji in the title, so this action will create the labels
5+
Note: GitHub _requires_ more than just the gitmoji in the label, so this action will create the labels
66
as `<gitmoji>_gitmoji` for example `✨_gitmoji` or `🐛_gitmoji` etc.
77

88
## Usage
9+
910
```yaml
1011
name: Create gitmoji label
1112
on:
@@ -26,6 +27,7 @@ jobs:
2627
```
2728
2829
### Permissions
30+
2931
The token requires the `pull-request: write` (to add a label to a PR) and if you are running this in a github-organisation, `repository-projects: read` too. Please see the following reference for the latter: https://github.com/cli/cli/discussions/5307
3032

3133
Using the above example verbatim, you can use the job title `gitmoji-pr-title` as a required check.
@@ -38,7 +40,8 @@ Optionally you can use this action in conjunction with [georgepstaylor/gitmoji-r
3840

3941
Likewise, you can create releases using the GitHub UI and the release notes will be generated based on the gitmoji labels if you use the following configuration:
4042

41-
See:
43+
See:
44+
4245
- https://docs.github.com/en/repositories/releasing-projects-on-github/automatically-generated-release-notes
4346
- https://docs.github.com/en/repositories/releasing-projects-on-github/managing-releases-in-a-repository
4447

action.yml

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ inputs:
66
GITHUB_TOKEN:
77
description: 'GitHub token'
88
required: true
9+
suffix:
10+
description: 'Suffix to append to the gitmoji label'
11+
required: true
12+
default: '_gitmoji'
13+
cleanup_other_gitmoji_labels:
14+
description: 'Remove gitmoji labels with different suffixes from this PR'
15+
required: false
16+
default: 'false'
917

1018
runs:
1119
using: "composite"
@@ -41,42 +49,72 @@ runs:
4149
env:
4250
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
4351
run: |
52+
suffix="${{ inputs.suffix }}"
4453
echo ${{steps.get-gitmoji.outputs.gitmoji}}
4554
4655
# try to create label with gitmoji_clean and if it fails create with gitmoji_clean_nv
4756
48-
(gh label list --json name | jq -er '.[] | select(.name | test("${{steps.get-gitmoji.outputs.gitmoji_clean}}_gitmoji")) | .name' || gh label create ${{steps.get-gitmoji.outputs.gitmoji_clean}}_gitmoji -c "$(openssl rand -hex 3)" -d "PRs with ${{steps.outputs.get-gitmoji.outputs.gitmoji_clean}}_gitmoji" --force) || (gh label create ${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}_gitmoji -c "$(openssl rand -hex 3)" -d "PRs with ${{steps.outputs.get-gitmoji.outputs.gitmoji_clean_nv}}_gitmoji" --force)
57+
(gh label list --json name | jq -er --arg gitmoji "${{steps.get-gitmoji.outputs.gitmoji_clean}}" --arg suffix "$suffix" '.[] | select(.name | test("\($gitmoji)$suffix")) | .name' || gh label create "${{steps.get-gitmoji.outputs.gitmoji_clean}}$suffix" -c "$(openssl rand -hex 3)" -d "PRs with ${{steps.get-gitmoji.outputs.gitmoji_clean}}$suffix" --force) || (gh label create "${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}$suffix" -c "$(openssl rand -hex 3)" -d "PRs with ${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}$suffix" --force)
4958
5059
- name: Check if PR already has the correct label
5160
id: check-existing-label
5261
shell: bash
5362
env:
5463
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
5564
run: |
65+
suffix="${{ inputs.suffix }}"
5666
gh pr checkout ${{ github.event.pull_request.number }}
5767
# Check for primary gitmoji label
58-
if gh pr view --json labels -q '.labels[].name' | grep -q "${{steps.get-gitmoji.outputs.gitmoji_clean}}_gitmoji"; then
59-
echo "PR already has the correct gitmoji label (${{steps.get-gitmoji.outputs.gitmoji_clean}}_gitmoji)"
68+
if gh pr view --json labels -q '.labels[].name' | grep -q "${{steps.get-gitmoji.outputs.gitmoji_clean}}$suffix"; then
69+
echo "PR already has the correct gitmoji label (${{steps.get-gitmoji.outputs.gitmoji_clean}}$suffix)"
6070
echo "has_correct_label=true" >> $GITHUB_OUTPUT
6171
# Check for alternative gitmoji label (no variation selectors)
62-
elif gh pr view --json labels -q '.labels[].name' | grep -q "${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}_gitmoji"; then
63-
echo "PR already has the correct gitmoji label (${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}_gitmoji)"
72+
elif gh pr view --json labels -q '.labels[].name' | grep -q "${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}$suffix"; then
73+
echo "PR already has the correct gitmoji label (${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}$suffix)"
6474
echo "has_correct_label=true" >> $GITHUB_OUTPUT
6575
else
6676
echo "PR does not have the correct gitmoji label"
6777
echo "has_correct_label=false" >> $GITHUB_OUTPUT
6878
fi
6979
70-
- name: remove labels ending in _gitmoji from PR
80+
- name: Clean up gitmoji labels with different suffixes
81+
id: cleanup-old-suffix
82+
if: inputs.cleanup_other_gitmoji_labels == 'true'
83+
shell: bash
84+
env:
85+
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
86+
run: |
87+
suffix="${{ inputs.suffix }}"
88+
current_gitmoji_clean="${{steps.get-gitmoji.outputs.gitmoji_clean}}"
89+
current_gitmoji_clean_nv="${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}"
90+
gh pr checkout ${{ github.event.pull_request.number }}
91+
echo "Cleaning up gitmoji labels with different suffixes..."
92+
gh pr view --json labels -q '.labels[].name' | tr -d '"' | while read -r label; do
93+
# Check if label matches current gitmoji but doesn't have current suffix
94+
if [[ "$label" == "$current_gitmoji_clean"* && "$label" != "$current_gitmoji_clean$suffix" ]] ||
95+
[[ "$label" == "$current_gitmoji_clean_nv"* && "$label" != "$current_gitmoji_clean_nv$suffix" ]]; then
96+
echo "Removing outdated gitmoji label: $label"
97+
gh pr edit --remove-label "$label"
98+
fi
99+
done
100+
101+
- name: remove labels ending in current suffix from PR
71102
id: remove-label
72103
if: steps.check-existing-label.outputs.has_correct_label != 'true'
73104
shell: bash
74105
env:
75106
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
76107
run: |
108+
suffix="${{ inputs.suffix }}"
77109
gh pr checkout ${{ github.event.pull_request.number }}
78-
gh pr view --json labels -q '.labels[] | select(.name | endswith("_gitmoji")) | .name' | tr -d '"' | while read -r label; do
79-
gh pr edit --remove-label $label
110+
# Get all labels ending with suffix and store in array
111+
mapfile -t labels_to_remove < <(gh pr view --json labels -q --arg suffix "$suffix" '.labels[] | select(.name | endswith($suffix)) | .name')
112+
# Remove each label
113+
for label in "${labels_to_remove[@]}"; do
114+
if [ -n "$label" ]; then
115+
echo "Removing label: $label"
116+
gh pr edit --remove-label "$label" || true
117+
fi
80118
done
81119
- name: Add label to PR
82120
id: add-label
@@ -85,8 +123,9 @@ runs:
85123
env:
86124
GITHUB_TOKEN: ${{ inputs.GITHUB_TOKEN }}
87125
run: |
126+
suffix="${{ inputs.suffix }}"
88127
gh pr checkout ${{ github.event.pull_request.number }}
89-
gh pr edit --add-label ${{steps.get-gitmoji.outputs.gitmoji_clean}}_gitmoji || gh pr edit --add-label ${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}_gitmoji
128+
gh pr edit --add-label "${{steps.get-gitmoji.outputs.gitmoji_clean}}$suffix" || gh pr edit --add-label "${{steps.get-gitmoji.outputs.gitmoji_clean_nv}}$suffix"
90129
91130
branding:
92131
icon: 'check-circle'

0 commit comments

Comments
 (0)