-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate-labels.sh
More file actions
executable file
·222 lines (192 loc) · 6.69 KB
/
migrate-labels.sh
File metadata and controls
executable file
·222 lines (192 loc) · 6.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env bash
# migrate-labels.sh — Phase 2: Migrate old labels to new taxonomy on ocl_issues.
#
# Usage:
# ./migrate-labels.sh --dry-run # preview changes (default)
# ./migrate-labels.sh --execute # apply changes
#
# Uses REST API PATCH (single call per issue) for speed.
# Fetches all issues once, processes in memory via Python.
#
# Requires: gh CLI (authenticated), python3
set -euo pipefail
REPO="OpenConceptLab/ocl_issues"
DRY_RUN=true
for arg in "$@"; do
case "$arg" in
--execute) DRY_RUN=false ;;
--dry-run) DRY_RUN=true ;;
esac
done
if $DRY_RUN; then
echo "=== DRY RUN === (use --execute to apply changes)"
else
echo "=== EXECUTING === (changes will be applied)"
fi
echo ""
# ─── Fetch all issues with their labels ─────────────────────────────
echo "Fetching all issues from $REPO..."
ISSUES_FILE=$(mktemp)
gh api "repos/$REPO/issues" --paginate \
--jq '.[] | {number, state, labels: [.labels[].name]}' \
-X GET -f state=all -f per_page=100 > "$ISSUES_FILE"
TOTAL=$(wc -l < "$ISSUES_FILE" | tr -d ' ')
echo "Fetched $TOTAL issues."
echo ""
# ─── Compute migration plan and optionally execute via Python ───────
PYTHONUNBUFFERED=1 python3 -u - "$ISSUES_FILE" "$DRY_RUN" "$REPO" <<'PYEOF'
import json, sys, subprocess, urllib.parse
sys.stdout.reconfigure(line_buffering=True)
issues_file = sys.argv[1]
dry_run = sys.argv[2] == "true"
repo = sys.argv[3]
# Label mapping: old → new
MAPPING = {
"bug": "type/bug",
"Feature": "type/feature",
"enhancement": "type/feature",
"Design": "type/feature",
"documentation": "type/docs",
"documentation-needed": "type/docs",
"tech debt": "type/refactor",
"performance": "type/feature",
"breaking-change": "type/feature",
"infra": "type/infra",
"V3": "component/web",
"web3": "component/web",
"v3-foundation": "component/web",
"web2": "component/web",
"ui": "component/web",
"UX / UI": "component/web",
"DS": "component/web",
"api2": "component/api",
"fhir": "component/fhir",
"For FHIR testing": "component/fhir",
"openmrs": "component/omrs",
"affects-ocl-client": "component/omrs",
"analytics": "component/analytics",
"ocldev": "component/api",
"ES": "component/api",
"bulk-import": "component/api",
"content": "component/content",
"community site": "component/docs",
"Logging": "component/infra",
"errbit": "component/infra",
"Priority: High": "priority/high",
"Priority: Medium": "priority/medium",
"Priority: Low": "priority/low",
"top-priority": "priority/critical",
"blocker": "priority/critical",
}
REMOVE_ONLY = {
"Epic", "Project-level Epic", "CIEL Implementer", "OHIE Must-Haves", "PM",
"Review before Dev", "demo", "demo-needed", "duplicate", "invalid", "question",
"wontfix", "intro", "medium-difficulty", "could-improve", "data-issue",
"environment-specific", "requires-es-index", "report", "post-launch",
"parking-lot", "production", "qa", "staging", "scheduled", "reviewed/keep",
}
ALL_OLD = set(MAPPING.keys()) | REMOVE_ONLY | {"discussion-needed"}
# Load issues
issues = []
with open(issues_file) as f:
for line in f:
line = line.strip()
if line:
issues.append(json.loads(line))
# Compute per-issue plan
plans = []
label_stats = {} # old_label -> count of issues affected
for issue in issues:
number = issue["number"]
state = issue["state"]
current = set(issue["labels"])
adds = set()
removes = set()
for label in current:
if label in MAPPING:
adds.add(MAPPING[label])
removes.add(label)
label_stats.setdefault(label, 0)
label_stats[label] += 1
elif label in REMOVE_ONLY:
removes.add(label)
label_stats.setdefault(label, 0)
label_stats[label] += 1
elif label == "discussion-needed":
removes.add(label)
label_stats.setdefault(label, 0)
label_stats[label] += 1
if state == "open":
adds.add("signal/needs-spec")
if not adds and not removes:
continue
new_labels = sorted((current - removes) | adds)
plans.append({
"number": number,
"state": state,
"adds": sorted(adds),
"removes": sorted(removes),
"new_labels": new_labels,
})
# Print summary
print("── Migration summary by label ──\n")
for old_label in sorted(MAPPING.keys(), key=str.lower):
if old_label in label_stats:
print(f" {old_label} ({label_stats[old_label]} issues) → {MAPPING[old_label]}")
for old_label in sorted(REMOVE_ONLY):
if old_label in label_stats:
print(f" {old_label} ({label_stats[old_label]} issues) → REMOVE")
dn_count = label_stats.get("discussion-needed", 0)
if dn_count:
dn_open = sum(1 for p in plans if "signal/needs-spec" in p["adds"])
print(f"\n discussion-needed: {dn_open} open → +signal/needs-spec, {dn_count - dn_open} closed → remove only")
print(f"\nTotal issues to update: {len(plans)}\n")
# Apply or dry-run
print("── Applying label changes ──\n")
done = 0
errors = 0
for plan in plans:
number = plan["number"]
state = plan["state"]
adds_str = ", ".join(plan["adds"])
removes_str = ", ".join(plan["removes"])
if dry_run:
print(f" #{number} ({state}): +[{adds_str}] -[{removes_str}]")
done += 1
else:
payload = json.dumps({"labels": plan["new_labels"]})
result = subprocess.run(
["gh", "api", f"repos/{repo}/issues/{number}",
"--method", "PATCH", "--input", "-"],
input=payload, capture_output=True, text=True
)
if result.returncode == 0:
done += 1
if done % 50 == 0:
print(f" ... {done} / {len(plans)} issues updated")
else:
print(f" ERROR #{number}: {result.stderr.strip()}")
errors += 1
print(f"\n Updated: {done} issues (errors: {errors})\n")
# Delete old labels
print("── Deleting old labels ──\n")
deleted = 0
for label in sorted(ALL_OLD):
encoded = urllib.parse.quote(label, safe="")
if dry_run:
print(f" DELETE: {label}")
deleted += 1
else:
result = subprocess.run(
["gh", "api", f"repos/{repo}/labels/{encoded}", "--method", "DELETE"],
capture_output=True, text=True
)
if result.returncode == 0:
print(f" DELETED: {label}")
deleted += 1
else:
print(f" SKIP (not found): {label}")
print(f"\n{'='*35}")
print(f"Done. Issues updated: {done} | Labels deleted: {deleted} | Errors: {errors}")
PYEOF
rm -f "$ISSUES_FILE"