-
Notifications
You must be signed in to change notification settings - Fork 0
/
prank.py
85 lines (72 loc) · 2.71 KB
/
prank.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
import argparse
from gitlab import Gitlab, GitlabCreateError, GitlabGetError
def read_file(path: str) -> str:
f = open(path, 'r', encoding='iso8859-1')
content = f.read()
f.close()
return content
accessToken = read_file('access_token').strip()
host = read_file('host').strip()
gl = Gitlab(url=host, private_token=accessToken)
parser = argparse.ArgumentParser()
parser.add_argument('-group',
help='The top level GitLab group you want to be removed from',
nargs='+',
default=[],
required=False)
parser.add_argument('-user',
help='For testing: the user name to test branch and mr creation on',
nargs='+',
default=[],
required=False)
parser.add_argument('-name',
help='The name to be added in the I was here message',
nargs='+',
default=['I'],
required=False)
args = parser.parse_args()
name = args.name[0]
branch = name+'_was_here'
if args.group:
group = gl.groups.get(args.group[0])
elif args.user:
group = gl.users.list(username=args.user[0])[0]
else:
raise RuntimeError('Either specify a group or user')
created = 0
for project in group.projects.list(include_subgroups=True):
print(project.name)
manageable_project = gl.projects.get(project.id, lazy=True)
main_or_master = "main"
try:
manageable_project.branches.get('main')
except GitlabGetError:
main_or_master = "master"
try:
manageable_project.branches.create({'branch': branch,
'ref': main_or_master})
except GitlabCreateError:
print(f'{project.name} has neither master nor main branch, spare it as folks use git in a funny way :)')
continue
#commit my message
content = open('I_was_here.md').read()
content = content.replace('<group>', group.name)
content = content.replace('<name>', name)
data = {
'branch': branch,
'commit_message': name + ' was here.',
'actions': [
{
'action': 'create',
'file_path': name+'_was_here.md',
'content': content,
}
]
}
commit = manageable_project.commits.create(data)
#create the merge request
mr = manageable_project.mergerequests.create({'source_branch': branch,
'target_branch': main_or_master,
'title': name+' was here! Please remove me from the '+group.name+' group!'})
created += 1
print(f'{created} branches and merge requests were created')