1010import click
1111from jinja2 import Template
1212from tqdm import tqdm
13+ from collections import OrderedDict
1314
14- __version__ = "0.4.1 "
15+ __version__ = "0.5.0 "
1516
1617try :
1718 ROOT = sys ._MEIPASS
@@ -61,73 +62,9 @@ def update(name, token, org):
6162
6263def run_update (name , token , org ):
6364 click .echo ("Generating.." )
64- gh = Github (token )
65- if org :
66- repo = gh .get_organization (org ).get_repo (name = name )
67- else :
68- repo = gh .get_user ().get_repo (name = name )
69-
70- # get a list of collaborators for this repo
71- collaborators = [col .login for col in repo .get_collaborators ()]
72-
73- systems , incidents , panels = {}, [], {}
74-
75- # get all systems and mark them as operational
76- for name in iter_systems (labels = repo .get_labels ()):
77- systems [name ] = {
78- "status" : "operational" ,
79- }
80-
81- # loop over all issues in the past 90 days to get current and past incidents
82- for issue in repo .get_issues (state = "all" , since = datetime .now () - timedelta (days = 90 )):
83- labels = issue .get_labels ()
84- affected_systems = list (iter_systems (labels ))
85- severity = get_severity (labels )
86-
87- # make sure that non-labeled issues are not displayed
88- if not affected_systems or severity is None :
89- continue
9065
91- # make sure that the user that created the issue is a collaborator
92- if issue .user .login not in collaborators :
93- continue
94-
95- if issue .state == "open" :
96- # shit is hitting the fan RIGHT NOW. Mark all affected systems
97- for affected_system in affected_systems :
98- systems [affected_system ]["status" ] = severity
99-
100- # create an incident
101- incident = {
102- "created" : issue .created_at ,
103- "title" : issue .title ,
104- "systems" : affected_systems ,
105- "severity" : severity ,
106- "closed" : issue .state == "closed" ,
107- "body" : issue .body ,
108- "updates" : []
109- }
110-
111- for comment in issue .get_comments ():
112- # add comments by collaborators only
113- if comment .user .login in collaborators :
114- incident ["updates" ].append ({
115- "created" : comment .created_at ,
116- "body" : comment .body
117- })
118-
119- incidents .append (incident )
120-
121- # sort incidents by date
122- incidents = sorted (incidents , key = lambda i : i ["created" ], reverse = True )
123-
124- # initialize and fill the panels with affected systems
125- for system , data in systems .items ():
126- if data ["status" ] != "operational" :
127- if data ["status" ] in panels :
128- panels [data ["status" ]].append (system )
129- else :
130- panels [data ["status" ]] = [system , ]
66+ repo = get_repo (token = token , name = name , org = org )
67+ issues = get_issues (repo )
13168
13269 # get the SHA of the current HEAD
13370 sha = repo .get_git_ref ("heads/gh-pages" ).object .sha
@@ -138,6 +75,10 @@ def run_update(name, token, org):
13875 ref = sha
13976 )
14077
78+ systems = get_systems (repo , issues )
79+ incidents = get_incidents (repo , issues )
80+ panels = get_panels (systems )
81+
14182 # render the template
14283 template = Template (template_file .decoded_content .decode ("utf-8" ))
14384 content = template .render ({
@@ -153,8 +94,8 @@ def run_update(name, token, org):
15394 )
15495
15596 if is_same_content (content , base64 .b64decode (index .content )):
156- click .echo ("Local status matches remote status, no need to commit." )
157- return False
97+ click .echo ("Local status matches remote status, no need to commit." )
98+ return False
15899
159100 repo .update_file (
160101 path = "/index.html" ,
@@ -172,15 +113,6 @@ def run_update(name, token, org):
172113 branch = "gh-pages" ,
173114 )
174115
175- def is_same_content (c1 , c2 ):
176- def sha1 (c ):
177- if PY3 :
178- if isinstance (c , str ):
179- c = bytes (c , "utf-8" )
180- else :
181- c = c .encode ()
182- return hashlib .sha1 (c )
183- return sha1 (c1 ).hexdigest () == sha1 (c2 ).hexdigest ()
184116
185117def run_create (name , token , systems , org ):
186118 gh = Github (token )
@@ -269,5 +201,104 @@ def get_severity(labels):
269201 return None
270202
271203
204+ def get_panels (systems ):
205+ # initialize and fill the panels with affected systems
206+ panels = OrderedDict ()
207+ for system , data in systems .items ():
208+ if data ["status" ] != "operational" :
209+ if data ["status" ] in panels :
210+ panels [data ["status" ]].append (system )
211+ else :
212+ panels [data ["status" ]] = [system , ]
213+ return panels
214+
215+
216+ def get_repo (token , name , org ):
217+ gh = Github (token )
218+ if org :
219+ return gh .get_organization (org ).get_repo (name = name )
220+ return gh .get_user ().get_repo (name = name )
221+
222+
223+ def get_collaborators (repo ):
224+ return [col .login for col in repo .get_collaborators ()]
225+
226+
227+ def get_systems (repo , issues ):
228+ systems = OrderedDict ()
229+ # get all systems and mark them as operational
230+ for name in sorted (iter_systems (labels = repo .get_labels ())):
231+ systems [name ] = {
232+ "status" : "operational" ,
233+ }
234+
235+ for issue in issues :
236+ if issue .state == "open" :
237+ labels = issue .get_labels ()
238+ severity = get_severity (labels )
239+ affected_systems = list (iter_systems (labels ))
240+ # shit is hitting the fan RIGHT NOW. Mark all affected systems
241+ for affected_system in affected_systems :
242+ systems [affected_system ]["status" ] = severity
243+ return systems
244+
245+
246+ def get_incidents (repo , issues ):
247+ # loop over all issues in the past 90 days to get current and past incidents
248+ incidents = []
249+ collaborators = get_collaborators (repo = repo )
250+ for issue in issues :
251+ labels = issue .get_labels ()
252+ affected_systems = sorted (iter_systems (labels ))
253+ severity = get_severity (labels )
254+
255+ # make sure that non-labeled issues are not displayed
256+ if not affected_systems or severity is None :
257+ continue
258+
259+ # make sure that the user that created the issue is a collaborator
260+ if issue .user .login not in collaborators :
261+ continue
262+
263+ # create an incident
264+ incident = {
265+ "created" : issue .created_at ,
266+ "title" : issue .title ,
267+ "systems" : affected_systems ,
268+ "severity" : severity ,
269+ "closed" : issue .state == "closed" ,
270+ "body" : issue .body ,
271+ "updates" : []
272+ }
273+
274+ for comment in issue .get_comments ():
275+ # add comments by collaborators only
276+ if comment .user .login in collaborators :
277+ incident ["updates" ].append ({
278+ "created" : comment .created_at ,
279+ "body" : comment .body
280+ })
281+
282+ incidents .append (incident )
283+
284+ # sort incidents by date
285+ return sorted (incidents , key = lambda i : i ["created" ], reverse = True )
286+
287+
288+ def get_issues (repo ):
289+ return repo .get_issues (state = "all" , since = datetime .now () - timedelta (days = 90 ))
290+
291+
292+ def is_same_content (c1 , c2 ):
293+ def sha1 (c ):
294+ if PY3 :
295+ if isinstance (c , str ):
296+ c = bytes (c , "utf-8" )
297+ else :
298+ c = c .encode ()
299+ return hashlib .sha1 (c )
300+ return sha1 (c1 ).hexdigest () == sha1 (c2 ).hexdigest ()
301+
302+
272303if __name__ == '__main__' : # pragma: no cover
273304 cli ()
0 commit comments