generated from rochacbruno/python-project-template
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpush.py
92 lines (81 loc) · 3.04 KB
/
push.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
import re
import urllib.parse
from typing import List
import airspeed
from pydantic import BaseModel
# @todo #/DEV Worksection task id in the middle (or end) might be in message not
# only at the begining. It could be in the middle or end. It would be good to
# support such cases as from app perspective we need Worksection task id only
# and doesn't matter where it in message exactly...
commit_msg_pattern = re.compile(r"^#WS-(\d+):.+$")
"""
Push event details from Gitlab.
"""
class Push(BaseModel):
ref: str
user_name: str
user_username: str
user_email: str
total_commits_count: int
project: dict = {}
commits: List[dict] = []
object_kind: str
event_name: str
before: str
after: str
checkout_sha: str
"""
Gitlab push commit SHA url
"""
def push_sha(self) -> str:
return self.project["homepage"] + "/-/commit/" + self.checkout_sha
def quantity(self) -> str:
return "commits" if self.total_commits_count > 1 else "commit"
"""
Allows to transform Gitlab push event about multiple commits into HTML
comment for worksection.
"""
def comment(self, author) -> str:
t = """<a href="$push_url" target="_blank">$commits_count new $quantity</a> pushed to <b style="background: rgb(196, 255, 166)"><a href="$branch_url" target="_blank">$branch_name</a></b> by <span class="invite invite_old" rel="$user_id"><img src="$user_logo" class="av_sm av_i" width="24" height="24" alt="">$user_name</span>
<br>
<ul>
#foreach ($commit in $commits)
<li><a href="$commit_url$commit.id" target="_blank">$commit.id</a> - $commit.message</li>
#end
</ul>
""" # noqa: E501
return urllib.parse.quote_plus(
airspeed.Template(t).merge(
{
"push_url": self.push_sha(),
"commits_count": self.total_commits_count,
"quantity": self.quantity(),
"branch_url": self.ref,
"branch_name": self.ref,
"user_id": author["id"],
"user_logo": author["avatar"],
"user_name": author["name"],
"commits": self.commits,
"commit_url": self.project["homepage"] + "/-/commit/",
}
)
)
"""
Extract worksection task id from Gitlab commit message
"""
def tasks(self) -> List[int]:
return list(
filter(
lambda ticket_id: ticket_id > 0,
map(
lambda m: 0 if m is None else int(m.group(1)),
filter(
lambda m: m is not None and int(m.group(1)) > 0,
map(
lambda c: commit_msg_pattern.search(c["message"]),
self.commits,
),
),
),
)
)