1- import os
2- import sys
3- import subprocess
41import json
2+ import subprocess
3+ import sys
54from pathlib import Path
65
6+
77def parse_labels (filepath ):
88 """
99 Parses the simple .github/labels.yml format.
@@ -12,9 +12,9 @@ def parse_labels(filepath):
1212 color: <color>
1313 description: <desc>
1414 """
15- with open (filepath , "r" ) as f :
15+ with open (filepath ) as f :
1616 content = f .read ()
17-
17+
1818 labels = []
1919 # match each block starting with `- name:`
2020 blocks = content .split ("- name:" )[1 :]
@@ -28,15 +28,12 @@ def parse_labels(filepath):
2828 color = line .strip ()[6 :].strip ().strip ("'\" " )
2929 elif line .strip ().startswith ("description:" ):
3030 desc = line .strip ()[12 :].strip ().strip ("'\" " )
31-
31+
3232 if name and name != "*" :
33- labels .append ({
34- "name" : name ,
35- "color" : color ,
36- "description" : desc
37- })
33+ labels .append ({"name" : name , "color" : color , "description" : desc })
3834 return labels
3935
36+
4037def get_existing_labels ():
4138 """
4239 Uses gh cli to get existing labels.
@@ -46,7 +43,7 @@ def get_existing_labels():
4643 ["gh" , "label" , "list" , "--json" , "name,color,description" , "--limit" , "500" ],
4744 capture_output = True ,
4845 text = True ,
49- check = True
46+ check = True ,
5047 )
5148 return json .loads (result .stdout )
5249 except subprocess .CalledProcessError as e :
@@ -56,6 +53,7 @@ def get_existing_labels():
5653 print ("Error: GitHub CLI (gh) is not installed or not in PATH." )
5754 sys .exit (1 )
5855
56+
5957def main ():
6058 root_dir = Path (__file__ ).parent .parent
6159 labels_file = root_dir / ".github" / "labels.yml"
@@ -66,7 +64,7 @@ def main():
6664
6765 desired_labels = parse_labels (labels_file )
6866 existing_labels_list = get_existing_labels ()
69-
67+
7068 existing_labels_map = {lbl ["name" ]: lbl for lbl in existing_labels_list }
7169
7270 print (f"Found { len (desired_labels )} desired labels in taxonomy." )
@@ -78,7 +76,7 @@ def main():
7876 name = desired ["name" ]
7977 color = desired .get ("color" , "ededed" )
8078 desc = desired .get ("description" , "" )
81-
79+
8280 if name in existing_labels_map :
8381 existing = existing_labels_map [name ]
8482 # Check if color or description needs update
@@ -88,7 +86,7 @@ def main():
8886 subprocess .run (
8987 ["gh" , "label" , "edit" , name , "--color" , color , "--description" , desc ],
9088 check = True ,
91- capture_output = True
89+ capture_output = True ,
9290 )
9391 except subprocess .CalledProcessError as e :
9492 print (f"Failed to update label '{ name } ': { e } " )
@@ -101,7 +99,7 @@ def main():
10199 subprocess .run (
102100 ["gh" , "label" , "create" , name , "--color" , color , "--description" , desc ],
103101 check = True ,
104- capture_output = True
102+ capture_output = True ,
105103 )
106104 except subprocess .CalledProcessError as e :
107105 print (f"Failed to create label '{ name } ': { e } " )
@@ -113,5 +111,6 @@ def main():
113111
114112 print ("Label sync complete." )
115113
114+
116115if __name__ == "__main__" :
117116 main ()
0 commit comments