forked from Kerl13/GHA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
169 lines (122 loc) · 4.48 KB
/
models.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
160
161
162
163
164
165
166
167
168
169
###############################################################################
# #
# GitLabHooks.py #
# by Niols #
# #
# BEERWARE License: #
# <[email protected]> wrote this file. As long as you retain this notice you #
# can do whatever you want with this stuff. If we meet some day, and you #
# think this stuff is worth it, you can buy me a beer in return. #
# –– Poul-Henning Kamp #
# #
###############################################################################
"""
This module describes the internal representations of Gitlab's hooks.
"""
from writer.common import RichTextMixin, RichTextList
# ---
# Base objects
# ---
class User():
def __init__(self, name="unknown", email="unknown"):
self.name = name
self.email = email
def __str__(self):
return self.name
class Project(RichTextMixin):
TEMPLATE = "[{name}]"
def __init__(self, name, url):
self.name = name
self.url = url
class Commit(RichTextMixin):
TEMPLATE = "{id} {author}: {message}"
def __init__(self, id, message, url, author):
assert isinstance(author, User)
self.id = id
self.message = message
self.url = url
self.author = author
def get_context(self):
context = RichTextMixin.get_context(self)
context["id"] = context["id"][:7]
context["message"] = context["message"].split('\n')[0]
return context
class Event():
def __init__(self, user, project):
assert isinstance(user, User)
assert isinstance(project, Project)
self.user = user
self.project = project
# ---
# The available hooks
# ---
class PushEvent(Event, RichTextMixin):
def __init__(self, branch, commits, url, **kwargs):
assert isinstance(commits, list)
assert all(isinstance(commit, Commit) for commit in commits)
Event.__init__(self, **kwargs)
self.branch = branch
self.commits = RichTextList(commits)
self.url = url
def get_context(self):
context = RichTextMixin.get_context(self)
context["commits"] = context["commits"][-5:]
context["nb"] = len(self.commits)
return context
class Push(PushEvent):
TEMPLATE = (
"{project} {user} pushed {nb} commits to {branch}. ({url})\n"
"{commits}"
)
class Creation(PushEvent):
TEMPLATE = (
"{project} {user} created branch {branch} with {nb} commits. ({url})\n"
"{commits}"
)
class Tag(Event, RichTextMixin):
TEMPLATE = "{project} {user} added the tag {tag_name}"
def __init__(self, name, **kwargs):
Event.__init__(self, **kwargs)
self.tag_name = name
class Issue(Event, RichTextMixin):
TEMPLATE = (
"{project} {user} {action} issue #{id}: {title}. ({url})"
)
def __init__(self, id, title, action, url, **kwargs):
Event.__init__(self, **kwargs)
self.id = id
self.title = title
self.action = action
self.url = url
class MergeRequest(Event, RichTextMixin):
TEMPLATE = (
"{project} {user} {action} merge request !{id}: {title}. ({url})"
)
def __init__(self, id, title, action, url, **kwargs):
Event.__init__(self, **kwargs)
self.id = id
self.title = title
self.action = action
self.url = url
class Deletion(Event, RichTextMixin):
TEMPLATE = (
"{project} {user} deleted branch {branch}."
)
def __init__(self, branch, **kwargs):
Event.__init__(self, **kwargs)
self.branch = branch
# ---
# Wiki related models
# ---
class WikiPage(RichTextMixin):
TEMPLATE = "{action} page {page_name}: {title}. ({url})"
def __init__(self, name, title, action, url):
self.page_name = name
self.title = title
self.action = action
self.url = url
class Wiki(Event, RichTextMixin):
TEMPLATE = "{project} {user} updated the wiki\n{wiki_pages}"
def __init__(self, pages, **kwargs):
Event.__init__(self, **kwargs)
self.wiki_pages = RichTextList(pages)