forked from elninotech/mergeminion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_webhooks.py
56 lines (46 loc) · 1.73 KB
/
update_webhooks.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
from util import get_gitlab, get_gitlab_requests, get_webhook_token
from requests import Response
from app import config
def edit_hook(project, hook_id, url, token) -> Response:
if not config.ACCESS_GITLAB:
return Response()
data = {
"url": url,
"token": token,
"merge_requests_events": True,
"push_events": False
}
return get_gitlab_requests(type="PUT", path=f"/projects/{project.id}/hooks/{hook_id}", data=data)
def add_hook(project, url, token) -> Response:
if not config.ACCESS_GITLAB:
return Response()
data = {
"url": url,
"token": token,
"merge_requests_events": True,
"push_events": False
}
return get_gitlab_requests(type="POST", path=f"/projects/{project.id}/hooks", data=data)
def get_hooks(project):
return get_gitlab_requests(type="GET", path=f"/projects/{project.id}/hooks")
def main():
gl = get_gitlab()
squad = config.GITLAB_GROUP_ID_MAPPING["group_name"] # enter your gitlab group name
group = gl.groups.get(squad)
projects = group.projects.list(archived=False)
url = config.BOT_URL + squad.name.lower()
token = get_webhook_token()
for project in projects:
res = get_hooks(project)
hooks = res.json()
project_has_hook = False
for hook in hooks:
if config.BOT_NAME in hook["url"]:
project_has_hook = True
res = edit_hook(project, hook["id"], url, token)
print(res.status_code, project.id, project.name, "edited hook")
if not project_has_hook:
res = add_hook(project, url, token)
print(res.status_code, project.id, project.name, "add hook")
if __name__ == "__main__":
main()