-
Notifications
You must be signed in to change notification settings - Fork 3
/
gitlab_group_editor.py
270 lines (220 loc) · 8.99 KB
/
gitlab_group_editor.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# This is a script that updates projects properties in specific
# group on GitLab.
import argparse
import os
import requests
import yaml
import gitlab
CONFIG_FILE = "python-gitlab.cfg"
DISTROBAKER_URL = "https://gitlab.cee.redhat.com/osci/distrobaker_centos_stream_config/-/raw/rhel9/distrobaker.yaml"
DEFAULT_MR_TEMPLATE = "Merge Request Template.md"
def parse_args():
"""
Parse arguments.
Return:
Argument object.
"""
parser = argparse.ArgumentParser(
description="This app uses GitLab API to edit projects in specific group.")
parser.add_argument("group", type=int, help="Id of the group to edit")
parser.add_argument(
"--visibility",
help="Set visibility of the projects to specific value",
choices=["private", "public"]
)
parser.add_argument(
"--merge_requests_enabled",
help="Enable/disable merge requests for the projects in group",
choices=["True", "False"]
)
parser.add_argument(
"--merge_method",
help="Choose the merge method for MRs. See https://docs.gitlab.com/ee/api/projects.html#project-merge-method",
choices=["merge", "rebase_merge", "ff"]
)
parser.add_argument(
"--only_allow_merge_if_pipeline_succeeds",
help="Merge requests may not be merged until the pipeline passes",
choices=["True", "False"]
)
parser.add_argument(
"--issues_enabled",
help="Enable/disable issues for the projects in group",
choices=["True", "False"]
)
parser.add_argument(
"--emails_enabled",
help="Enable/disable email notifications for the projects in group",
choices=["True", "False"]
)
parser.add_argument(
"--filter",
help="Whether to apply actions only to packages included in the DistroBaker sync",
choices=["synced", "non_synced", "all"],
default="all"
)
parser.add_argument(
"--dry-run",
help="Print the changes that would occur, but do not actually save them",
action=argparse.BooleanOptionalAction
)
parser.add_argument(
"--protect-branch",
help="Branch to protect. Developers can merge, only maintainers and above can push"
)
parser.add_argument(
"--ci_config_path",
help="The path to the Gitlab CI configuration."
)
parser.add_argument(
"--shared_runners_enabled",
help="Enable/disable shared CI runners for the projects in group",
choices=["True", "False"]
)
parser.add_argument(
"--merge-request-template",
help="A markdown file to use as a template for merge requests.",
type=argparse.FileType('r'),
nargs="?",
const=open(DEFAULT_MR_TEMPLATE, 'r'),
default=None,
)
parser.add_argument(
"--c9s_setup",
help="Configure the selected projects with all of the standard CentOS Stream 9 settings. "
"Does not set visibility. Idempotent.",
action=argparse.BooleanOptionalAction
)
return parser.parse_args()
if __name__ == "__main__":
# Main
args = parse_args()
group = args.group
visibility = args.visibility
merge_requests_enabled = None
merge_method = None
issues_enabled = None
emails_disabled = None
protect_branch = None
ci_config_path = None
only_allow_merge_if_pipeline_succeeds = None
shared_runners_enabled = None
mr_template = None
if args.merge_requests_enabled:
merge_requests_enabled = args.merge_requests_enabled == "True"
if args.merge_method:
merge_method = args.merge_method
if args.issues_enabled:
issues_enabled = args.issues_enabled == "True"
if args.emails_enabled:
emails_disabled = args.emails_enabled != "True"
if args.protect_branch:
protect_branch = args.protect_branch
if args.ci_config_path:
ci_config_path = args.ci_config_path
if args.only_allow_merge_if_pipeline_succeeds:
only_allow_merge_if_pipeline_succeeds = args.only_allow_merge_if_pipeline_succeeds
if args.shared_runners_enabled:
shared_runners_enabled = args.shared_runners_enabled == "True"
if args.merge_request_template:
mr_template = args.merge_request_template.read()
args.merge_request_template.close()
if args.c9s_setup:
merge_requests_enabled = True
merge_method = "ff"
issues_enabled = False
emails_disabled = False
protect_branch = "c9s"
ci_config_path = "global-tasks.yml@redhat/centos-stream/ci-cd/dist-git-gating-tests"
only_allow_merge_if_pipeline_succeeds = True
shared_runners_enabled = False
with open(DEFAULT_MR_TEMPLATE, "r") as f:
mr_template = f.read()
config_file = CONFIG_FILE
# Read environment variable with config
try:
config_file = os.environ["PYTHON_GITLAB_CFG"]
except KeyError:
pass
if args.filter != "all":
r = requests.get(DISTROBAKER_URL, timeout=10)
r.raise_for_status()
dbcfg = yaml.load(r.text, Loader=yaml.SafeLoader)
package_filter = dbcfg["configuration"]["control"]["exclude"]["rpms"]
# Create Gitlab object
gl = gitlab.Gitlab.from_config(config_files=[config_file])
# Get the projects in group
group = gl.groups.get(group)
# Update each project in the group
for project in group.projects.list(as_list=False):
if (args.filter == "synced" and project.name in package_filter) or \
(args.filter == "non_synced" and project.name not in package_filter):
continue
print("Project {group_name}/{project_name}".format(
group_name=group.name, project_name=project.name)
)
savable_project = gl.projects.get(project.id)
if visibility:
print("* visibility: {old} -> {new}".format(
old=savable_project.visibility, new=visibility)
)
savable_project.visibility = visibility
if merge_requests_enabled is not None:
print("* merge_requests_enabled: {old} -> {new}".format(
old=savable_project.merge_requests_enabled, new=merge_requests_enabled)
)
savable_project.merge_requests_enabled = merge_requests_enabled
if merge_method is not None:
print("* merge_method: {old} -> {new}".format(
old=savable_project.merge_method, new=merge_method)
)
savable_project.merge_method = merge_method
if issues_enabled is not None:
print("* issues_enabled: {old} -> {new}".format(
old=savable_project.issues_enabled, new=issues_enabled)
)
savable_project.issues_enabled = issues_enabled
if emails_disabled is not None:
print("* emails_disabled: {old} -> {new}".format(
old=savable_project.emails_disabled, new=emails_disabled)
)
savable_project.emails_disabled = emails_disabled
if ci_config_path is not None:
print("* ci_config_path: {old} -> {new}".format(
old=savable_project.ci_config_path, new=ci_config_path)
)
savable_project.ci_config_path = ci_config_path
if only_allow_merge_if_pipeline_succeeds is not None:
print("* only_allow_merge_if_pipeline_succeeds: {old} -> {new}".format(
old=savable_project.only_allow_merge_if_pipeline_succeeds,
new=only_allow_merge_if_pipeline_succeeds)
)
savable_project.only_allow_merge_if_pipeline_succeeds = only_allow_merge_if_pipeline_succeeds
if shared_runners_enabled is not None:
print("* shared_runners_enabled: {old} -> {new}".format(
old=savable_project.shared_runners_enabled, new=shared_runners_enabled)
)
savable_project.shared_runners_enabled = shared_runners_enabled
if mr_template is not None:
print("* merge_request_template: {old} -> {new}".format(
old=savable_project.merge_requests_template, new=mr_template)
)
if protect_branch is not None:
try:
branch_to_protect = savable_project.branches.get(protect_branch)
print("* protected_branches: {old.name} -> {new.name}".format(
old=branch_to_protect, new=branch_to_protect
))
if not args.dry_run:
branch_to_protect.protect(developers_can_push=False, developers_can_merge=True)
except gitlab.exceptions.GitlabGetError as e:
print("WARNING: {group_name}/{project_name} has no '{branch_name}' branch".format(
group_name=group.name, project_name=project.name, branch_name=protect_branch)
)
if not args.dry_run:
while True:
try:
savable_project.save()
break
except requests.exceptions.ReadTimeout:
pass