Skip to content

Commit 1bffe92

Browse files
feat(workflows): add label management utilities and CLI for workflows
- Introduced `label_manager.py` for consistent label operations across GitHub Issues and PRs - Added `workflow_cli.py` for command-line interface to workflow utilities - Updated `README.md` to document new modules and usage - Enhanced `gen-preview.yml` to specify workflow names - Added comprehensive unit tests for new functionalities
1 parent 4a750a7 commit 1bffe92

15 files changed

Lines changed: 1516 additions & 1 deletion

File tree

.github/workflows/gen-preview.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ run-name: "Preview: ${{ github.event.workflow_run.head_branch }}"
33

44
on:
55
workflow_run:
6-
workflows: ["Plot Tests"]
6+
workflows: ["CI: Plot Tests"]
77
types:
88
- completed
99

automation/scripts/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Workflow Scripts
2+
3+
Reusable Python utilities for GitHub Actions workflows.
4+
5+
## Modules
6+
7+
### `workflow_utils.py`
8+
Branch and issue parsing functions used across multiple workflows.
9+
10+
```python
11+
from automation.scripts.workflow_utils import (
12+
extract_branch_info, # Parse auto/{spec-id}/{library} branches
13+
extract_sub_issue, # Extract sub-issue from PR body
14+
extract_parent_issue, # Extract parent issue with fallback
15+
get_attempt_count, # Count ai-attempt-X labels
16+
parse_plot_path, # Parse plots/{lib}/{type}/{spec}/{variant}.py
17+
is_valid_library, # Validate library name
18+
)
19+
```
20+
21+
### `label_manager.py`
22+
Label operations and status transitions.
23+
24+
```python
25+
from automation.scripts.label_manager import (
26+
get_status_transition, # Calculate label changes for status
27+
get_quality_label, # Get quality:* label for score
28+
get_quality_transition, # Calculate quality label changes
29+
is_approved, # Check for ai-approved label
30+
is_rejected, # Check for ai-rejected label
31+
get_current_status, # Get current status from labels
32+
LabelChange, # Dataclass with to_gh_args() method
33+
)
34+
```
35+
36+
### `workflow_cli.py`
37+
CLI interface for use in GitHub Actions shell steps.
38+
39+
## Usage in Workflows
40+
41+
### Option 1: Direct CLI (Recommended)
42+
43+
```yaml
44+
- name: Extract branch info
45+
id: branch_info
46+
run: |
47+
INFO=$(uv run python -m automation.scripts.workflow_cli extract-branch "$BRANCH")
48+
echo "spec_id=$(echo $INFO | jq -r '.spec_id')" >> $GITHUB_OUTPUT
49+
echo "library=$(echo $INFO | jq -r '.library')" >> $GITHUB_OUTPUT
50+
51+
- name: Get attempt count
52+
id: attempts
53+
run: |
54+
LABELS=$(gh pr view $PR_NUM --json labels -q '.labels[].name' | tr '\n' ',')
55+
COUNT=$(uv run python -m automation.scripts.workflow_cli get-attempt-count "$LABELS")
56+
echo "count=$COUNT" >> $GITHUB_OUTPUT
57+
58+
- name: Update status labels
59+
run: |
60+
LABELS=$(gh pr view $PR_NUM --json labels -q '.labels[].name' | tr '\n' ',')
61+
ARGS=$(uv run python -m automation.scripts.workflow_cli status-transition "$LABELS" "testing")
62+
if [ -n "$ARGS" ]; then
63+
gh pr edit $PR_NUM $ARGS
64+
fi
65+
```
66+
67+
### Option 2: Inline Python
68+
69+
```yaml
70+
- name: Extract sub-issue
71+
id: sub_issue
72+
run: |
73+
SUB_ISSUE=$(uv run python -c "
74+
from automation.scripts.workflow_utils import extract_sub_issue
75+
result = extract_sub_issue('''$PR_BODY''')
76+
print(result if result else '')
77+
")
78+
echo "number=$SUB_ISSUE" >> $GITHUB_OUTPUT
79+
```
80+
81+
## CLI Commands
82+
83+
| Command | Description | Example |
84+
|---------|-------------|---------|
85+
| `extract-branch` | Parse auto branch | `extract-branch auto/scatter-basic/matplotlib` |
86+
| `extract-sub-issue` | Get sub-issue from PR body | `extract-sub-issue "Sub-Issue: #42"` |
87+
| `extract-parent-issue` | Get parent issue | `extract-parent-issue "Parent: #100"` |
88+
| `get-attempt-count` | Count attempts | `get-attempt-count "ai-attempt-1,testing"` |
89+
| `parse-plot-path` | Parse plot file path | `parse-plot-path "plots/matplotlib/..."` |
90+
| `status-transition` | Get label change args | `status-transition "generating" "testing"` |
91+
| `quality-label` | Get quality label | `quality-label 95` |
92+
93+
## Benefits
94+
95+
1. **Testability** - 69 unit tests cover all functions
96+
2. **Consistency** - Same parsing logic across all workflows
97+
3. **Maintainability** - Fix bugs in one place
98+
4. **Documentation** - Clear docstrings and examples

0 commit comments

Comments
 (0)