-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmrmonitor.py
executable file
·159 lines (123 loc) · 4.86 KB
/
mrmonitor.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
154
155
156
157
158
159
#!/usr/bin/env python3
import os
import re
import sys
import arrow
import click
import dotenv
import gitlab
import rich
from rich.console import Console
dotenv.load_dotenv()
gl = gitlab.Gitlab(os.getenv("GITLAB_URI"), private_token=os.getenv("PRIVATE_TOKEN"))
JIRA_URL = os.getenv("JIRA_URL", "").removesuffix("/")
JIRA_REGEX = [re.compile(regex) for regex in os.getenv("JIRA_REGEX", "").split(",")]
def _get_jira_link(mr) -> str | None:
if JIRA_URL and JIRA_REGEX:
for pattern in JIRA_REGEX:
if m := pattern.search(mr.title):
issue_id = m.group(1)
return issue_id
return None
def _get_colored_pipeline_status(pipeline) -> str:
match pipeline.status:
case "failed":
color = "red"
case "pending":
color = "yellow"
case "running":
color = "cyan"
case "success":
color = "green"
case _:
color = "grey"
return f"[{color}]{pipeline.status}[/{color}]"
@click.group()
def cli() -> None:
pass
@cli.command(help="List MRs from given projects (IDs separated by comma)")
@click.argument("project_ids", required=True)
@click.option("--user", help="Filter for username")
def show(project_ids: str, user: str) -> None:
"""Show MRs for a single user across projects."""
projects = project_ids.split(",")
first = True
console = Console(highlight=False)
for project_id in projects:
project = gl.projects.get(project_id)
mrs = project.mergerequests.list(state="opened", author_username=user)
if not first:
print()
first = False
rich.print(f"[bold blue]{project.path_with_namespace}[/bold blue]")
if not mrs:
print(" No merge requests matching the criteria")
print()
continue
for mr in mrs:
# MR info
iid_str = f"[bold red]!{mr.iid}[/bold red]"
title_str = f"[yellow]{mr.title}[/yellow]"
upvotes_str = downvotes_str = "0"
if mr.upvotes:
upvotes_str = f"[bold green]+{mr.upvotes}[/bold green]"
if mr.downvotes:
downvotes_str = f"[bold red]-{mr.downvotes}[/bold red]"
approvals = mr.approvals.get()
approved = (
"[bold green]Yes[/bold green]"
if approvals.approved_by
else "[bold red]No[/bold red]"
)
approvers = (
"by " + ", ".join([ap["user"]["username"] for ap in approvals.approved_by])
if approvals.approved_by
else ""
)
console.print(f" {iid_str} {title_str} [{upvotes_str} {downvotes_str}]")
console.print(f" Author: {mr.author['username']}")
console.print(f" Approved: {approved} {approvers}")
console.print(f" URL: [link={mr.web_url}]MR {mr.iid}[/link]")
if issue_id := _get_jira_link(mr):
console.print(f" JIRA: [link={JIRA_URL}/{issue_id}]{issue_id}[/link]")
pipelines = mr.pipelines.list(get_all=True)
if pipelines:
last_pipeline = sorted(pipelines, key=lambda x: x.id)[-1]
console.print(f" Pipeline: {_get_colored_pipeline_status(last_pipeline)}")
print()
# Dates
created = arrow.get(mr.created_at)
updated = arrow.get(mr.updated_at)
age = arrow.now() - created
since_update = arrow.now() - updated
infos = [
f"[cyan]Created:[/cyan] {created.humanize()}",
]
if age.days >= 7:
infos.append(f"[cyan]Age:[/cyan] [bold red]{age.days} days[/bold red]")
elif age.days >= 3:
infos.append(f"[cyan]Age:[/cyan] [bold yellow]{age.days} days[/bold yellow]")
else:
infos.append(f"[cyan]Age:[/cyan] [bold green]{age.days} days[/bold green]")
if since_update.days >= 3:
infos.append(
f"[cyan]Since update:[/cyan] [bold red]{since_update.days} days[/bold red]"
)
elif since_update.days >= 1:
infos.append(
f"[cyan]Since update:[/cyan] [bold yellow]{since_update.days} days[/bold yellow]"
)
else:
infos.append(
f"[cyan]Since update:[/cyan] [bold green]{since_update.days} days[/bold green]"
)
console.print(" " + " | ".join(infos))
print()
if __name__ == "__main__":
if not os.getenv("GITLAB_URI"):
rich.print("[bold red]GITLAB_URI not configured[/bold red]")
sys.exit(1)
if not os.getenv("PRIVATE_TOKEN"):
rich.print("[bold red]PRIVATE_TOKEN not configured[/bold red]")
sys.exit(1)
cli()