Skip to content

Commit 264ab7a

Browse files
committed
Implement category determination using a dedicated Python script
1 parent 2a42dc1 commit 264ab7a

File tree

2 files changed

+40
-16
lines changed

2 files changed

+40
-16
lines changed

.github/workflows/issue_to_pr.yml

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,18 @@ jobs:
3535
- name: Install dependencies
3636
run: pip install -r requirements.txt
3737

38+
- name: Get issue labels
39+
id: getLabels
40+
uses: snnaplab/get-labels-action@v1
41+
with:
42+
issue-number: ${{ github.event.issue.number }}
43+
3844
- name: Determine category from labels
3945
id: determineCategory
4046
run: |
41-
labels_json="${{ toJson(github.event.issue.labels) }}"
47+
labels_json="${{ steps.getLabels.outputs.labels }}"
4248
echo "Labels: $labels_json"
43-
category=$(
44-
if [[ $(echo "$labels_json" | grep -q 'examples') ]]; then
45-
echo 'examples'
46-
elif [[ $(echo "$labels_json" | grep -q 'mode') ]]; then
47-
echo 'mode'
48-
elif [[ $(echo "$labels_json" | grep -q 'tool') ]]; then
49-
echo 'tool'
50-
elif [[ $(echo "$labels_json" | grep -q 'library') ]]; then
51-
echo 'library'
52-
fi
53-
)
54-
if [ -z "$category" ]; then
55-
echo "Category is empty. Please ensure the issue has a valid label."
56-
exit 1
57-
fi
49+
category=$(python scripts/determine_category.py "$labels_json")
5850
echo "category=$category" >> $GITHUB_OUTPUT
5951
echo "Category found: $category"
6052

scripts/determine_category.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Reads issue labels from a JSON string and determines the category of the issue.
3+
"""
4+
5+
import json
6+
import sys
7+
8+
valid_categories = ["examples", "mode", "tool", "library"]
9+
10+
def determine_category(labels_json):
11+
labels = json.loads(labels_json)
12+
found_categories = [label for label in labels if label in valid_categories]
13+
14+
if len(found_categories) == 1:
15+
return found_categories[0]
16+
elif len(found_categories) > 1:
17+
raise ValueError(f"Multiple valid categories found: ({', '.join(found_categories)}). Please ensure only one valid label is applied.")
18+
else:
19+
return None
20+
21+
if __name__ == "__main__":
22+
labels_json = sys.argv[1]
23+
try:
24+
category = determine_category(labels_json)
25+
if category:
26+
print(f"category={category}")
27+
else:
28+
print(f"Category is empty. Please ensure the issue has a valid label (options: {', '.join(valid_categories)}).")
29+
sys.exit(1)
30+
except ValueError as e:
31+
print(str(e))
32+
sys.exit(1)

0 commit comments

Comments
 (0)