-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrender.py
153 lines (128 loc) · 5.09 KB
/
render.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
149
150
151
152
153
import os
import random
import logging
from datetime import datetime
from dataclasses import dataclass
from urllib.parse import quote
import requests
from jinja2 import Environment, FileSystemLoader
# Configure logging
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
# Constants
GITLAB_API_URL = "https://git.esss.dk/api/v4"
DMSC_NIGHTLY_PROJECT_ID = 301
TOKEN = os.getenv("GITLAB_PRIVATE_TOKEN")
TEAMS = ["ECDC", "SCIPP", "SWAT", "DST", "DONKI", "IDS"]
# Data class for Job
@dataclass
class Job:
job_run_url: str
job_run_status: str
job_name: str
job_stage: str
teams: str
# API Functions
def get_pipelines(project_id):
# TODO: Add pagination or control number of pipelines to fetch
url = f"{GITLAB_API_URL}/projects/{project_id}/pipelines?ref=main&per_page=50"
logging.info(f"Fetching pipelines from URL: {url}")
headers = {"Authorization": f"PRIVATE-TOKEN {TOKEN}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
pipelines = response.json()
latest_pipeline = pipelines[0]
# Get the last 50 pipeline ids
last_50_pipelines = [pipeline["id"] for pipeline in pipelines[:50]]
return latest_pipeline, last_50_pipelines
def get_jobs(project_id, pipeline_id):
url = f"{GITLAB_API_URL}/projects/{project_id}/pipelines/{pipeline_id}/jobs?per_page=100"
headers = {"Authorization": f"PRIVATE-TOKEN {TOKEN}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
return response.json()
def get_test_report(project_id, pipeline_id):
url = f"{GITLAB_API_URL}/projects/{project_id}/pipelines/{pipeline_id}/test_report"
headers = {"Authorization": f"PRIVATE-TOKEN {TOKEN}"}
response = requests.get(url, headers=headers)
response.raise_for_status()
# logging.info(f"Fetching test report from URL: {url}")
return response.json()
def main():
pipeline, last_50 = get_pipelines(DMSC_NIGHTLY_PROJECT_ID)
pipeline_id = pipeline["id"]
jobs = get_jobs(DMSC_NIGHTLY_PROJECT_ID, pipeline_id)
success, failed, others = [], [], []
for job in jobs:
# TODO: Map teams to jobs
temp_teams = ", ".join(random.choices(TEAMS, k=2))
job_obj = Job(
job["web_url"], job["status"], job["name"], job["stage"], temp_teams
)
if job["status"] == "success":
success.append(job_obj)
elif job["status"] == "failed":
failed.append(job_obj)
else:
job_obj.job_run_status = ""
others.append(job_obj)
all_jobs = success + failed + others
for job in all_jobs:
logging.info(
f"Job: {job.job_name}, Status: {job.job_run_status}, URL: {job.job_run_url}, Stage: {job.job_stage}"
)
run_chart = []
for pid in last_50:
test_report = get_test_report(DMSC_NIGHTLY_PROJECT_ID, pid)
total_tests = test_report["total_count"]
failed_tests = test_report["failed_count"]
skipped_tests = test_report["skipped_count"]
failed_job_percentage = (
(failed_tests / total_tests * 100) if total_tests else 0.0
)
run_chart.append(
(pid, total_tests, failed_job_percentage, failed_tests, skipped_tests)
)
run_chart.reverse()
# last_run_skipped_test
last_run_test_report = get_test_report(DMSC_NIGHTLY_PROJECT_ID, pipeline_id)
test_suites = last_run_test_report["test_suites"]
skipped_test_suites = []
for test in test_suites:
job_name = test["name"]
job_name_url = f"https://git.esss.dk/dmsc-nightly/dmsc-nightly/-/pipelines/{pipeline_id}/test_report?job_name={quote(job_name)}"
for test_cases in test["test_cases"]:
if test_cases["status"] == "skipped":
skipped_test_suites.append(
(test_cases["classname"], test_cases["name"], job_name_url)
)
environment = Environment(loader=FileSystemLoader("templates/"))
template = environment.get_template("dashboard.html")
date = datetime.fromisoformat(pipeline["updated_at"].replace("Z", "+00:00"))
formatted_date = date.strftime("%B %d, %Y %I:%M %p") + " UTC"
percentage_data = [i[2] for i in run_chart]
failed_job_percentage = f"{percentage_data[-1]:.2f}%"
pipeline_run_ids = [i[0] for i in run_chart]
number_of_tests = [i[1] for i in run_chart]
failing_test = [i[3] for i in run_chart]
skipped_tests = [i[4] for i in run_chart]
content = template.render(
gitlab_tests=all_jobs,
failed_tests=failed,
failed_job_percentage=failed_job_percentage,
pipeline_end_time=formatted_date,
teams=TEAMS,
failing_test=failing_test,
pipeline_run_ids=pipeline_run_ids,
skipped_tests=skipped_tests,
number_of_tests=number_of_tests,
pipeline_id=pipeline_id,
skipped_test_suites=skipped_test_suites,
)
filename = "render/rendered.html"
with open(filename, mode="w", encoding="utf-8") as message:
message.write(content)
logging.info(f"... wrote {filename}")
if __name__ == "__main__":
main()