-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathgitlab_bot.py
More file actions
223 lines (172 loc) · 7.97 KB
/
Copy pathgitlab_bot.py
File metadata and controls
223 lines (172 loc) · 7.97 KB
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Copyright 2023 Lei Zhang
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import warnings
from dotenv import load_dotenv
from src.channels.dispatcher import NotificationDispatcher
from src.channels.feishu import FeishuChannel
from src.channels.log import LogChannel
from src.config import (
bot_gitlab_token,
bot_gitlab_url,
bot_gitlab_username,
bot_host,
bot_port,
feishu_app_id,
feishu_app_secret,
feishu_bot_open_id,
feishu_chat_id,
feishu_enabled,
feishu_request_timeout_seconds,
merge_notification_db_path,
merge_notification_max_attempts,
merge_notification_retry_backoff_seconds,
merge_notification_sending_timeout_seconds,
)
from src.delivery.coordinator import NotificationDelivery
from src.delivery.idempotent_channel import DurableIdempotentChannel
from src.delivery.sqlite import NotificationDeliveryStore
from src.hooks.approval_notification import ApprovalNotificationHooks
from src.hooks.issue import IssueHooks
from src.hooks.merge_notification import MergeRequestNotificationHooks
from src.hooks.merge_request import MergeRequestHooks
from src.hooks.note import NoteHooks
from src.hooks.pipeline_notification import PipelineNotificationHooks
from src.logs import print_event
load_dotenv() # isort:skip
def _load_gitlab_bot():
# gidgetlab 1.1.0 still imports the deprecated pkg_resources API.
with warnings.catch_warnings():
warnings.filterwarnings(
"ignore",
category=UserWarning,
message=r"pkg_resources is deprecated as an API\.",
)
warnings.filterwarnings(
"ignore",
category=DeprecationWarning,
message=r"Deprecated call to `pkg_resources\.declare_namespace",
)
from gidgetlab.aiohttp import GitLabBot
return GitLabBot
GitLabBot = _load_gitlab_bot()
bot = GitLabBot(bot_gitlab_username, url=bot_gitlab_url, access_token=bot_gitlab_token)
issue_hooks = IssueHooks()
merge_request_hooks = MergeRequestHooks()
note_hooks = NoteHooks()
def _build_notification_targets():
targets = {"log": LogChannel()}
if feishu_enabled:
targets["feishu"] = FeishuChannel.from_environment(
app_id=feishu_app_id,
app_secret=feishu_app_secret,
chat_id=feishu_chat_id,
bot_open_id=feishu_bot_open_id,
timeout_seconds=feishu_request_timeout_seconds,
)
return targets
notification_targets = _build_notification_targets()
notification_channel = NotificationDispatcher(notification_targets)
approval_notification_hooks = ApprovalNotificationHooks(notification_channel)
notification_delivery_store = NotificationDeliveryStore(
merge_notification_db_path,
sending_timeout_seconds=merge_notification_sending_timeout_seconds,
max_attempts=merge_notification_max_attempts,
retry_backoff_seconds=merge_notification_retry_backoff_seconds,
)
durable_notification_targets = {
target: DurableIdempotentChannel(channel, notification_delivery_store, delivery_target=target)
for target, channel in notification_targets.items()
}
merge_notification_channel = NotificationDispatcher(durable_notification_targets)
merge_notification_delivery = NotificationDelivery(merge_notification_channel, notification_delivery_store)
pipeline_notification_delivery = NotificationDelivery(merge_notification_channel, notification_delivery_store)
merge_request_notification_hooks = MergeRequestNotificationHooks(
merge_notification_channel,
delivery=merge_notification_delivery,
)
pipeline_notification_hooks = PipelineNotificationHooks(
merge_notification_channel,
delivery=pipeline_notification_delivery,
)
async def recover_merge_notification_deliveries(_app):
recovered_merge_notifications = await merge_request_notification_hooks.recover()
recovered_pipeline_notifications = await pipeline_notification_hooks.recover()
return recovered_merge_notifications + recovered_pipeline_notifications
bot.app.on_startup.append(recover_merge_notification_deliveries)
@bot.router.register("Issue Hook", action="open")
async def issue_opened_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await issue_hooks.issue_opened_event(event, gl, args, kwargs)
@bot.router.register("Issue Hook", action="close")
async def issue_closed_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await issue_hooks.issue_closed_event(event, gl, args, kwargs)
@bot.router.register("Issue Hook", action="update")
async def issue_updated_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await issue_hooks.issue_updated_event(event, gl, args, kwargs)
@bot.router.register("Note Hook", noteable_type="Issue")
async def note_issue_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await issue_hooks.note_issue_event(event, gl, args, kwargs)
@bot.router.register("Merge Request Hook", action="open")
async def merge_request_opened_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await merge_request_hooks.merge_request_opened_event(event, gl, args, kwargs)
@bot.router.register("Merge Request Hook", action="update")
async def merge_request_updated_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await merge_request_hooks.merge_request_updated_event(event, gl, args, kwargs)
@bot.router.register("Merge Request Hook", action="reopen")
async def merge_request_reopen_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await merge_request_hooks.merge_request_reopen_event(event, gl, args, kwargs)
@bot.router.register("Merge Request Hook", action="approved")
@bot.router.register("Merge Request Hook", action="approval")
async def merge_request_approval_event(event, gl, *args, **kwargs):
await approval_notification_hooks.handle(event, gl, *args, **kwargs)
@bot.router.register("Merge Request Hook", action="unapproved")
@bot.router.register("Merge Request Hook", action="unapproval")
async def merge_request_unapproval_event(event, gl, *args, **kwargs):
await approval_notification_hooks.handle(event, gl, *args, **kwargs)
@bot.router.register("Merge Request Hook", action="merge")
async def merge_request_merged_event(event, gl, *args, **kwargs):
await merge_request_notification_hooks.handle(event, gl, *args, **kwargs)
@bot.router.register("Pipeline Hook")
async def pipeline_event(event, gl, *args, **kwargs):
await pipeline_notification_hooks.handle(event, gl, *args, **kwargs)
@bot.router.register("Note Hook", noteable_type="MergeRequest")
async def note_merge_request_event(event, gl, *args, **kwargs):
if not ignore_event(event):
await merge_request_hooks.note_merge_request_event(event, gl, args, kwargs)
@bot.router.register("Note Hook", noteable_type="Commit")
async def note_commit_event(event, gl, *args, **kwargs):
if not ignore_event(event):
note_hooks.note_commit_event(event, gl, args, kwargs)
@bot.router.register("Note Hook", noteable_type="Snippet")
async def note_snippet_event(event, gl, *args, **kwargs):
if not ignore_event(event):
note_hooks.note_snippet_event(event, gl, args, kwargs)
def ignore_event(event) -> bool:
print_event(event)
username = event.data["user"]["username"]
if username != bot_gitlab_username:
return False
else:
logging.info("Ignore event: %s triggered by admin", event.data["event_type"])
return True
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
bot.run(host=bot_host, port=int(bot_port))