1
+ import argparse
1
2
from collections import defaultdict
3
+ import logging
2
4
import os
3
5
import sys
4
6
@@ -34,11 +36,14 @@ def get_jobs(session, repo_slug: str, run_id: int) -> list:
34
36
return jobs
35
37
36
38
37
- def main (repo_slug : str , run_id : int ) -> None :
39
+ def main (repo_slug : str , run_id : int , output : str ) -> None :
40
+ logging .info (f"Getting workflow summary for https://github.com/{ repo_slug } /actions/runs/{ run_id } " )
38
41
environ = get_environ ()
39
42
40
43
has_failure = False
41
44
45
+ result : list [str ] = []
46
+
42
47
with requests .Session () as session :
43
48
if "GH_TOKEN" in environ :
44
49
session .headers ["Authorization" ] = environ ["GH_TOKEN" ]
@@ -49,9 +54,11 @@ def main(repo_slug: str, run_id: int) -> None:
49
54
50
55
for job in jobs :
51
56
if job ["name" ] in ("all-jobs-are-green" , "fancy-report" ):
57
+ logging .info (f"Skipping job { job ['name' ]} " )
52
58
continue
53
59
54
60
if job ["conclusion" ] not in ["skipped" , "success" ]:
61
+ logging .info (f"Job { job ['name' ]} conclusion: { job ['conclusion' ]} " )
55
62
has_failure = True
56
63
57
64
if job ["conclusion" ] != "failure" :
@@ -62,16 +69,48 @@ def main(repo_slug: str, run_id: int) -> None:
62
69
failing_steps [step ["name" ]].append ((job , step ))
63
70
64
71
for step_name , items in failing_steps .items ():
65
- print (f"❌ **Failures for `{ step_name } `**\n " )
72
+ result . append (f"❌ **Failures for `{ step_name } `**\n " )
66
73
for job , step in sorted (items , key = lambda x : x [0 ]["name" ]):
67
74
url = job ["html_url" ]
68
- print (f"* [{ job ['name' ]} ]({ url } #step:{ step ['number' ]} )" )
75
+ result .append (f"* [{ job ['name' ]} ]({ url } #step:{ step ['number' ]} )" )
76
+
77
+ result .append ("" )
69
78
79
+ if output :
80
+ logging .info (f"Writing output to { output } " )
81
+ with open (output , "w" ) as f :
82
+ f .write ("\n " .join (result ))
83
+ else :
84
+ logging .info ("Writing output to stdout" )
85
+ print ("\n " .join (result ))
70
86
print ()
71
87
72
88
if has_failure :
73
89
sys .exit (1 )
74
90
75
91
76
92
if __name__ == "__main__" :
77
- main ("DataDog/system-tests" , int (sys .argv [1 ]))
93
+ parser = argparse .ArgumentParser (
94
+ prog = "get-workflow-summary" , description = "List all failing step of a github workflow, and pretty print them"
95
+ )
96
+
97
+ parser .add_argument ("repo_slug" , type = str , help = "Repo slug of the workflow" )
98
+
99
+ parser .add_argument ("run_id" , type = int , help = "Run Id of the workflow" )
100
+
101
+ parser .add_argument (
102
+ "--output" ,
103
+ "-o" ,
104
+ type = str ,
105
+ default = "" ,
106
+ help = "Output file. If not provided, output to stdout" ,
107
+ )
108
+ args = parser .parse_args ()
109
+
110
+ logging .basicConfig (level = logging .INFO , format = "%(levelname)s: %(message)s" , stream = sys .stderr )
111
+
112
+ main (
113
+ repo_slug = args .repo_slug ,
114
+ run_id = args .run_id ,
115
+ output = args .output ,
116
+ )
0 commit comments