This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dpt-fix-integrations-webhooks.py
98 lines (79 loc) · 3.48 KB
/
dpt-fix-integrations-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
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
# dpt-fix-integrations-webhooks.py
#
# Tool to harmonize webhooks and integrations configurations of DPT repos
import logging
import os
import sys
import gitlab
from debian.deb822 import Deb822
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', stream=sys.stdout, level=logging.INFO)
SALSA_TOKEN = os.environ.get('SALSA_TOKEN', None)
if not SALSA_TOKEN:
print('Set SALSA_TOKEN environment variable, exiting')
sys.exit(1)
# 9360 is the group_id for python-team/packages subgroup, it could be automatically obtained
# from https://salsa.debian.org/api/v4/groups/python-team/subgroups/ but meh
GROUPID = 9360
logging.info("Gather DPT projects from Salsa")
salsa = gitlab.Gitlab('https://salsa.debian.org/', private_token=SALSA_TOKEN)
group = salsa.groups.get(GROUPID)
group_projects = group.projects.list(all=True, order_by='name', sort='asc', as_list=True)
stats = {
'tagpending': 0,
'kgb': 0,
'emails-on-push': 0,
'irker': 0,
'exception': 0,
}
for group_project in group_projects:
project = salsa.projects.get(group_project.id)
logging.info(f'Processing {project.name}')
# gather details
try:
set_tagpending = True
set_kgb = True
for hook in project.hooks.list():
if hook.url.startswith('https://webhook.salsa.debian.org/tagpending/'):
set_tagpending = False
continue
if hook.url.startswith('http://kgb.debian.net:9418'):
# See https://salsa.debian.org/kgb-team/kgb/-/wikis/usage for details on the default arguments
if hook.url == 'http://kgb.debian.net:9418/webhook/?channel=debian-python-changes':
set_kgb = False
else:
project.hooks.delete(id=hook.id)
logging.info(' removed old-format KBG webhook')
set_email = True
for service in project.services.list():
if service.title.lower() == 'emails on push':
set_email = False
continue
if service.title.lower() == 'irker (irc gateway)':
project.services.delete(id=service.slug)
logging.info(' removed integration: irker')
stats['irker'] += 1
# make changes
if set_tagpending:
if any(x['name'] == 'debian' for x in project.repository_tree()):
d_control_id = [d['id'] for d in project.repository_tree(path='debian', all=True) if d['name'] == 'control'][0]
d_control = Deb822(project.repository_raw_blob(d_control_id))
project.hooks.create({'url': f'https://webhook.salsa.debian.org/tagpending/{d_control["Source"]}'})
logging.info(' added webhook: tagpending')
stats['tagpending'] += 1
else:
logging.error(' unable to determine the source package name')
if set_kgb:
project.hooks.create({'url': 'http://kgb.debian.net:9418/webhook/?channel=debian-python-changes'})
logging.info(' added webhook: KGB')
stats['kgb'] += 1
if set_email:
project.services.update('emails-on-push', {'recipients': '[email protected]'})
logging.info(' added integration: email-on-push')
stats['emails-on-push'] += 1
except Exception as e:
logging.exception(e)
stats['exception'] += 1
logging.info('')
logging.info('Execution recap:')
for k, v in stats.items():
logging.info(f' {k}: {v}')