| 1 |
#!/usr/bin/env python3 |
| 2 |
# Copyright © 2020 - 2024 Collabora Ltd. |
| 3 |
# Authors: |
| 4 |
# David Heidelberg <[email protected]> |
| 5 |
# Sergi Blanch Torne <[email protected]> |
| 6 |
# SPDX-License-Identifier: MIT |
| 7 |
|
| 8 |
""" |
| 9 |
Compare the two latest scheduled pipelines and provide information |
| 10 |
about the jobs you're interested in. |
| 11 |
""" |
| 12 |
|
| 13 |
import argparse |
| 14 |
import csv |
| 15 |
import re |
| 16 |
import requests |
| 17 |
import io |
| 18 |
from tabulate import tabulate |
| 19 |
|
| 20 |
import gitlab |
| 21 |
from gitlab_common import read_token |
| 22 |
from rich import print |
| 23 |
|
| 24 |
|
| 25 |
MARGE_BOT_USER_ID = 9716 |
| 26 |
|
| 27 |
|
| 28 |
def print_failures_csv(id): |
| 29 |
url = "https://gitlab.freedesktop.org/mesa/mesa"\ |
| 30 |
f"/-/jobs/{id}/artifacts/raw/results/failures.csv" |
| 31 |
missing: int = 0 |
| 32 |
MAX_MISS: int = 20 |
| 33 |
try: |
| 34 |
response = requests.get(url) |
| 35 |
response.raise_for_status() |
| 36 |
csv_content = io.StringIO(response.text) |
| 37 |
csv_reader = csv.reader(csv_content) |
| 38 |
data = list(csv_reader) |
| 39 |
|
| 40 |
for line in data[:]: |
| 41 |
if line[1] == "UnexpectedImprovement(Pass)": |
| 42 |
line[1] = f"[green]{line[1]}[/green]" |
| 43 |
elif line[1] == "UnexpectedImprovement(Fail)": |
| 44 |
line[1] = f"[yellow]{line[1]}[/yellow]" |
| 45 |
elif line[1] == "Crash" or line[1] == "Fail": |
| 46 |
line[1] = f" [red]{line[1]}[/red]" |
| 47 |
elif line[1] == "Missing": |
| 48 |
if missing > MAX_MISS: |
| 49 |
data.remove(line) |
| 50 |
continue |
| 51 |
missing += 1 |
| 52 |
line[1] = f"[yellow]{line[1]}[/yellow]" |
| 53 |
elif line[1] == "Fail": |
| 54 |
line[1] = f"[red]{line[1]}[/red]" |
| 55 |
else: |
| 56 |
line[1] = f"[white]{line[1]}[/white]" |
| 57 |
|
| 58 |
if missing > MAX_MISS: |
| 59 |
data.append( |
| 60 |
[ |
| 61 |
f"[red]... more than {MAX_MISS} missing tests, " |
| 62 |
"something crashed?[/red]", |
| 63 |
"[red]Missing[/red]" |
| 64 |
] |
| 65 |
) |
| 66 |
headers = [f"Test{"":<75}", "Result"] |
| 67 |
print(tabulate(data, headers, tablefmt="plain")) |
| 68 |
except Exception: |
| 69 |
pass |
| 70 |
|
| 71 |
|
| 72 |
def job_failed_before(old_jobs, job): |
| 73 |
for old_job in old_jobs: |
| 74 |
if job.name == old_job.name: |
| 75 |
return old_job |
| 76 |
|
| 77 |
|
| 78 |
def parse_args() -> None: |
| 79 |
"""Parse args""" |
| 80 |
parser = argparse.ArgumentParser( |
| 81 |
description="Tool to show merge requests assigned to the marge-bot", |
| 82 |
) |
| 83 |
parser.add_argument( |
| 84 |
"--target", |
| 85 |
metavar="target-job", |
| 86 |
help="Target job regex. For multiple targets, pass multiple values, " |
| 87 |
"eg. `--target foo bar`.", |
| 88 |
required=False, |
| 89 |
nargs=argparse.ONE_OR_MORE, |
| 90 |
) |
| 91 |
parser.add_argument( |
| 92 |
"--token", |
| 93 |
metavar="token", |
| 94 |
help="force GitLab token, " |
| 95 |
"otherwise it's read from ~/.config/gitlab-token", |
| 96 |
) |
| 97 |
return parser.parse_args() |
| 98 |
|
| 99 |
|
| 100 |
if __name__ == "__main__": |
| 101 |
args = parse_args() |
| 102 |
token = read_token(args.token) |
| 103 |
gl = gitlab.Gitlab( |
| 104 |
url="https://gitlab.freedesktop.org", |
| 105 |
private_token=token, |
| 106 |
) |
| 107 |
|
| 108 |
project = gl.projects.get("mesa/mesa") |
| 109 |
|
| 110 |
print( |
| 111 |
"[link=https://gitlab.freedesktop.org/mesa/mesa/-/pipelines?" |
| 112 |
"page=1&scope=all&source=schedule]Scheduled pipelines overview[/link]" |
| 113 |
) |
| 114 |
pipelines = project.pipelines.list( |
| 115 |
source="schedule", |
| 116 |
ordered_by="created_at", |
| 117 |
sort="desc", |
| 118 |
page=1, |
| 119 |
per_page=2, |
| 120 |
) |
| 121 |
print( |
| 122 |
"Old pipeline:" |
| 123 |
f" {pipelines[1].created_at}" |
| 124 |
f"\t[link={pipelines[1].web_url}]{pipelines[1].status}[/link]" |
| 125 |
f"\t{pipelines[1].sha}" |
| 126 |
) |
| 127 |
print( |
| 128 |
"New pipeline:" |
| 129 |
f" {pipelines[0].created_at}" |
| 130 |
f"\t[link={pipelines[0].web_url}]{pipelines[0].status}[/link]" |
| 131 |
f"\t{pipelines[0].sha}" |
| 132 |
) |
| 133 |
print( |
| 134 |
"\nWebUI visual compare: " |
| 135 |
"https://gitlab.freedesktop.org/mesa/mesa/-/compare/" |
| 136 |
f"{pipelines[1].sha}...{pipelines[0].sha}\n" |
| 137 |
) |
| 138 |
|
| 139 |
# regex part |
| 140 |
if args.target: |
| 141 |
target = "|".join(args.target) |
| 142 |
target = target.strip() |
| 143 |
print(f"🞋 jobs: [blue]{target}[/blue]") |
| 144 |
|
| 145 |
target = f"({target})" + r"( \d+/\d+)?" |
| 146 |
else: |
| 147 |
target = ".*" |
| 148 |
|
| 149 |
target_jobs_regex: re.Pattern = re.compile(target) |
| 150 |
|
| 151 |
old_failed_jobs = [] |
| 152 |
for job in pipelines[1].jobs.list(all=True): |
| 153 |
if ( |
| 154 |
job.status != "failed" |
| 155 |
or target_jobs_regex |
| 156 |
and not target_jobs_regex.fullmatch(job.name) |
| 157 |
): |
| 158 |
continue |
| 159 |
old_failed_jobs.append(job) |
| 160 |
|
| 161 |
job_failed = False |
| 162 |
for job in pipelines[0].jobs.list(all=True): |
| 163 |
if ( |
| 164 |
job.status != "failed" |
| 165 |
or target_jobs_regex |
| 166 |
and not target_jobs_regex.fullmatch(job.name) |
| 167 |
): |
| 168 |
continue |
| 169 |
|
| 170 |
job_failed = True |
| 171 |
|
| 172 |
previously_failed_job = job_failed_before(old_failed_jobs, job) |
| 173 |
if previously_failed_job: |
| 174 |
print( |
| 175 |
f"[yellow]" |
| 176 |
f" :: [link={job.web_url}]{job.name}[/link][/yellow]" |
| 177 |
f"[magenta]" |
| 178 |
f" [link={previously_failed_job.web_url}](previous run)[/link]" |
| 179 |
) |
| 180 |
else: |
| 181 |
print( |
| 182 |
f"[red]:: [link={job.web_url}]{job.name}[/link]" |
| 183 |
) |
| 184 |
print_failures_csv(job.id) |
| 185 |
|
| 186 |
if not job_failed: |
| 187 |
exit(0) |
| 188 |
|
| 189 |
print("Commits between nightly pipelines:") |
| 190 |
commit = project.commits.get(pipelines[0].sha) |
| 191 |
while True: |
| 192 |
print( |
| 193 |
f"{commit.id} [link={commit.web_url}]{commit.title}[/link]" |
| 194 |
) |
| 195 |
if commit.id == pipelines[1].sha: |
| 196 |
break |
| 197 |
commit = project.commits.get(commit.parent_ids[0]) |