Skip to content

Commit dd85416

Browse files
authored
Use wrapper script for label operations in issue triage (#968)
* Use wrapper script for label operations in issue triage Updates /label-issue command and examples to use a dedicated edit-issue-labels.sh script for label operations instead of raw gh issue edit. The script validates labels against the repo's existing labels before applying them. Also tightens gh search permission to gh search issues. * Show multiple --add-label flags in label-issue example
1 parent edd85d6 commit dd85416

File tree

5 files changed

+97
-10
lines changed

5 files changed

+97
-10
lines changed

.claude/commands/label-issue.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
allowed-tools: Bash(gh label list:*),Bash(gh issue view:*),Bash(gh issue edit:*),Bash(gh search:*)
2+
allowed-tools: Bash(gh label list:*),Bash(gh issue view:*),Bash(./scripts/edit-issue-labels.sh:*),Bash(gh search issues:*)
33
description: Apply labels to GitHub issues
44
---
55

@@ -23,8 +23,8 @@ TASK OVERVIEW:
2323
- You have access to these Bash commands:
2424
- Bash(gh label list:\*) - to get available labels
2525
- Bash(gh issue view:\*) - to view issue details
26-
- Bash(gh issue edit:\*) - to apply labels to the issue
27-
- Bash(gh search:\*) - to search for similar issues
26+
- Bash(./scripts/edit-issue-labels.sh:\*) - to apply labels to the issue
27+
- Bash(gh search issues:\*) - to search for similar issues
2828

2929
3. Analyze the issue content, considering:
3030

@@ -44,7 +44,7 @@ TASK OVERVIEW:
4444
- If you find similar issues using gh search, consider using a "duplicate" label if appropriate. Only do so if the issue is a duplicate of another OPEN issue.
4545

4646
5. Apply the selected labels:
47-
- Use `gh issue edit` to apply your selected labels
47+
- Use `./scripts/edit-issue-labels.sh --issue NUMBER --add-label LABEL1 --add-label LABEL2` to apply your selected labels
4848
- DO NOT post any comments explaining your decision
4949
- DO NOT communicate directly with users
5050
- If no labels are clearly applicable, do not apply any labels
@@ -54,7 +54,7 @@ IMPORTANT GUIDELINES:
5454
- Be thorough in your analysis
5555
- Only select labels from the provided list above
5656
- DO NOT post any comments to the issue
57-
- Your ONLY action should be to apply labels using gh issue edit
57+
- Your ONLY action should be to apply labels using ./scripts/edit-issue-labels.sh
5858
- It's okay to not add any labels if none are clearly applicable
5959

6060
---

.github/workflows/issue-triage.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
- name: Run Claude Code for Issue Triage
2222
uses: anthropics/claude-code-action@main
2323
with:
24-
prompt: "/label-issue REPO: ${{ github.repository }} ISSUE_NUMBER${{ github.event.issue.number }}"
24+
prompt: "/label-issue REPO: ${{ github.repository }} ISSUE_NUMBER: ${{ github.event.issue.number }}"
2525
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
2626
allowed_non_write_users: "*" # Required for issue triage workflow, if users without repo write access create issues
2727
github_token: ${{ secrets.GITHUB_TOKEN }}

docs/solutions.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,12 +415,12 @@ jobs:
415415
4. Check if it duplicates existing issues
416416
417417
Based on your analysis, add the appropriate labels using:
418-
`gh issue edit [number] --add-label "label1,label2"`
418+
`./scripts/edit-issue-labels.sh --issue [number] --add-label "label1" --add-label "label2"`
419419

420420
If it appears to be a duplicate, post a comment mentioning the original issue.
421421

422422
claude_args: |
423-
--allowedTools "Bash(gh issue:*),Bash(gh search:*)"
423+
--allowedTools "Bash(gh issue view:*),Bash(gh search issues:*),Bash(./scripts/edit-issue-labels.sh:*)"
424424
```
425425
426426
**Key Configuration:**

examples/issue-triage.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ jobs:
2121
- name: Run Claude Code for Issue Triage
2222
uses: anthropics/claude-code-action@v1
2323
with:
24-
# NOTE: /label-issue here requires a .claude/commands/label-issue.md file in your repo (see this repo's .claude directory for an example)
25-
prompt: "/label-issue REPO: ${{ github.repository }} ISSUE_NUMBER${{ github.event.issue.number }}"
24+
# NOTE: /label-issue requires .claude/commands/label-issue.md and scripts/edit-issue-labels.sh in your repo (see this repo for examples)
25+
prompt: "/label-issue REPO: ${{ github.repository }} ISSUE_NUMBER: ${{ github.event.issue.number }}"
2626

2727
anthropic_api_key: ${{ secrets.ANTHROPIC_API_KEY }}
2828
allowed_non_write_users: "*" # Required for issue triage workflow, if users without repo write access create issues

scripts/edit-issue-labels.sh

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Edits labels on a GitHub issue.
4+
# Usage: ./scripts/edit-issue-labels.sh --issue 123 --add-label bug --add-label needs-triage --remove-label untriaged
5+
#
6+
7+
set -euo pipefail
8+
9+
ISSUE=""
10+
ADD_LABELS=()
11+
REMOVE_LABELS=()
12+
13+
# Parse arguments
14+
while [[ $# -gt 0 ]]; do
15+
case $1 in
16+
--issue)
17+
ISSUE="$2"
18+
shift 2
19+
;;
20+
--add-label)
21+
ADD_LABELS+=("$2")
22+
shift 2
23+
;;
24+
--remove-label)
25+
REMOVE_LABELS+=("$2")
26+
shift 2
27+
;;
28+
*)
29+
exit 1
30+
;;
31+
esac
32+
done
33+
34+
# Validate issue number
35+
if [[ -z "$ISSUE" ]]; then
36+
exit 1
37+
fi
38+
39+
if ! [[ "$ISSUE" =~ ^[0-9]+$ ]]; then
40+
exit 1
41+
fi
42+
43+
if [[ ${#ADD_LABELS[@]} -eq 0 && ${#REMOVE_LABELS[@]} -eq 0 ]]; then
44+
exit 1
45+
fi
46+
47+
# Fetch valid labels from the repo
48+
VALID_LABELS=$(gh label list --limit 500 --json name --jq '.[].name')
49+
50+
# Filter to only labels that exist in the repo
51+
FILTERED_ADD=()
52+
for label in "${ADD_LABELS[@]}"; do
53+
if echo "$VALID_LABELS" | grep -qxF "$label"; then
54+
FILTERED_ADD+=("$label")
55+
fi
56+
done
57+
58+
FILTERED_REMOVE=()
59+
for label in "${REMOVE_LABELS[@]}"; do
60+
if echo "$VALID_LABELS" | grep -qxF "$label"; then
61+
FILTERED_REMOVE+=("$label")
62+
fi
63+
done
64+
65+
if [[ ${#FILTERED_ADD[@]} -eq 0 && ${#FILTERED_REMOVE[@]} -eq 0 ]]; then
66+
exit 0
67+
fi
68+
69+
# Build gh command arguments
70+
GH_ARGS=("issue" "edit" "$ISSUE")
71+
72+
for label in "${FILTERED_ADD[@]}"; do
73+
GH_ARGS+=("--add-label" "$label")
74+
done
75+
76+
for label in "${FILTERED_REMOVE[@]}"; do
77+
GH_ARGS+=("--remove-label" "$label")
78+
done
79+
80+
gh "${GH_ARGS[@]}"
81+
82+
if [[ ${#FILTERED_ADD[@]} -gt 0 ]]; then
83+
echo "Added: ${FILTERED_ADD[*]}"
84+
fi
85+
if [[ ${#FILTERED_REMOVE[@]} -gt 0 ]]; then
86+
echo "Removed: ${FILTERED_REMOVE[*]}"
87+
fi

0 commit comments

Comments
 (0)