-
Notifications
You must be signed in to change notification settings - Fork 23
/
main.py
148 lines (133 loc) · 4.88 KB
/
main.py
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
import os
import requests
import io
import logging
import sys
import signal
import json
# don't remove, it loads the configuration
import logger
def main():
# User provided variables
github_repo = os.environ.get("INPUT_GITHUB_REPOSITORY")
try:
assert github_repo not in (None, '')
except:
output = "The input github repository is not set"
print(f"Error: {output}")
sys.exit(-1)
github_run_id = os.environ.get("INPUT_GITHUB_RUN_ID")
try:
assert github_run_id not in (None, '')
except:
output = "The input github run id is not set"
print(f"Error: {output}")
sys.exit(-1)
github_token = os.environ.get("INPUT_GITHUB_TOKEN")
try:
assert github_token not in (None, '')
except:
output = "The input github token is not set"
print(f"Error: {output}")
sys.exit(-1)
github_org = os.environ.get("INPUT_GITHUB_ORG")
try:
assert github_org not in (None, '')
except:
output = "The input github org is not set"
print(f"Error: {output}")
sys.exit(-1)
elastic_logger = logging.getLogger("elastic")
metadata_url = f"https://api.github.com/repos/{github_org}/{github_repo}/actions/runs/{github_run_id}"
try:
r = requests.get(metadata_url, stream=True, headers={
"Authorization": f"token {github_token}"
})
metadata = json.loads(r.content)
jobs_url = metadata.get('jobs_url')
metadata.pop('repository')
metadata.pop('head_repository')
metadata = {
"metadata_" + k: v for k,v in metadata.items()
}
except Exception as exc:
output = "Failed to get run metadata" + str(exc)
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
sys.exit(-1)
# extract all done jobs
jobs = {}
try:
jobs_response = requests.get(jobs_url, headers={
"Authorization": f"token {github_token}"
})
if not jobs_response.ok:
raise Exception("Failed to get run jobs")
_jobs = json.loads(jobs_response.content)
for job in _jobs.get('jobs'):
job_id = job.get('id')
# no logs for jobs that weren't completed
if not job.get('status') == 'completed':
continue
jobs[job_id] = {
"job_id": job_id,
"job_name": job.get('name'),
"job_status": job.get('status'),
"job_conclusion": job.get('conclusion'),
"job_steps": job.get('steps')
}
# log this metadata to elastic
elastic_logger.info("Job metadata", extra={
**jobs.get(job_id)
})
except Exception as exc:
output = "Failed to get run jobs" + str(exc)
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
sys.exit(-1)
for job_id in jobs:
try:
job_logs_url = f"https://api.github.com/repos/{github_org}/{github_repo}/actions/jobs/{job_id}/logs"
r = requests.get(job_logs_url, stream=True, headers={
"Authorization": f"token {github_token}"
})
if not r.ok:
output = "Failed to download logs"
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
sys.exit(-1)
logs = io.BytesIO(r.content)
for log in logs:
elastic_logger.info(log.strip().decode(), extra={
"job_id": job_id,
"job_name": jobs.get(job_id).get('job_name'),
"repo": github_repo,
"run_id": github_run_id,
**metadata
})
except requests.exceptions.HTTPError as errh:
output = "GITHUB API Http Error:" + str(errh)
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
sys.exit(-1)
except requests.exceptions.ConnectionError as errc:
output = "GITHUB API Error Connecting:" + str(errc)
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
sys.exit(-1)
except requests.exceptions.Timeout as errt:
output = "Timeout Error:" + str(errt)
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
sys.exit(-1)
except requests.exceptions.RequestException as err:
output = "GITHUB API Non catched error connecting:" + str(err)
print(f"Error: {output}")
print(f"::set-output name=result::{output}")
sys.exit(-1)
def keyboard_interrupt_bug(signal, frame):
print('keyboard interrupt')
pass
signal.signal(signal.SIGINT, keyboard_interrupt_bug)
if __name__ == "__main__":
main()