-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Branch.py
531 lines (482 loc) · 23.3 KB
/
Branch.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
# -*- coding: utf-8 -*-
############################ Copyrights and license ############################
# #
# Copyright 2012 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2012 Zearin <zearin@gonk.net> #
# Copyright 2013 AKFish <akfish@gmail.com> #
# Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2013 martinqt <m.ki2@laposte.net> #
# Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net> #
# Copyright 2015 Kyle Hornberg <khornberg@users.noreply.github.com> #
# Copyright 2016 Jannis Gebauer <ja.geb@me.com> #
# Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com> #
# Copyright 2018 Steve Kowalik <steven@wedontsleep.org> #
# Copyright 2018 Wan Liuyang <tsfdye@gmail.com> #
# Copyright 2018 sfdye <tsfdye@gmail.com> #
# #
# This file is part of PyGithub. #
# http://pygithub.readthedocs.io/ #
# #
# PyGithub is free software: you can redistribute it and/or modify it under #
# the terms of the GNU Lesser General Public License as published by the Free #
# Software Foundation, either version 3 of the License, or (at your option) #
# any later version. #
# #
# PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY #
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS #
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
# details. #
# #
# You should have received a copy of the GNU Lesser General Public License #
# along with PyGithub. If not, see <http://www.gnu.org/licenses/>. #
# #
################################################################################
import github.BranchProtection
import github.Commit
import github.GithubObject
import github.RequiredPullRequestReviews
import github.RequiredStatusChecks
from . import Consts
class Branch(github.GithubObject.NonCompletableGithubObject):
"""
This class represents Branches. The reference can be found here https://developer.github.com/v3/repos/branches
"""
def __repr__(self):
return self.get__repr__({"name": self._name.value})
@property
def commit(self):
"""
:type: :class:`github.Commit.Commit`
"""
return self._commit.value
@property
def name(self):
"""
:type: string
"""
return self._name.value
@property
def protected(self):
"""
:type: bool
"""
return self._protected.value
@property
def protection_url(self):
"""
:type: string
"""
return self._protection_url.value
def _initAttributes(self):
self._commit = github.GithubObject.NotSet
self._name = github.GithubObject.NotSet
self._protection_url = github.GithubObject.NotSet
self._protected = github.GithubObject.NotSet
def _useAttributes(self, attributes):
if "commit" in attributes: # pragma no branch
self._commit = self._makeClassAttribute(
github.Commit.Commit, attributes["commit"]
)
if "name" in attributes: # pragma no branch
self._name = self._makeStringAttribute(attributes["name"])
if "protection_url" in attributes: # pragma no branch
self._protection_url = self._makeStringAttribute(
attributes["protection_url"]
)
if "protected" in attributes: # pragma no branch
self._protected = self._makeBoolAttribute(attributes["protected"])
def get_protection(self):
"""
:calls: `GET /repos/:owner/:repo/branches/:branch/protection <https://developer.github.com/v3/repos/branches>`_
"""
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.protection_url,
headers={"Accept": Consts.mediaTypeRequireMultipleApprovingReviews},
)
return github.BranchProtection.BranchProtection(
self._requester, headers, data, completed=True
)
def edit_protection(
self,
strict=github.GithubObject.NotSet,
contexts=github.GithubObject.NotSet,
enforce_admins=github.GithubObject.NotSet,
dismissal_users=github.GithubObject.NotSet,
dismissal_teams=github.GithubObject.NotSet,
dismiss_stale_reviews=github.GithubObject.NotSet,
require_code_owner_reviews=github.GithubObject.NotSet,
required_approving_review_count=github.GithubObject.NotSet,
user_push_restrictions=github.GithubObject.NotSet,
team_push_restrictions=github.GithubObject.NotSet,
):
"""
:calls: `PUT /repos/:owner/:repo/branches/:branch/protection <https://developer.github.com/v3/repos/branches>`_
:strict: bool
:contexts: list of strings
:enforce_admins: bool
:dismissal_users: list of strings
:dismissal_teams: list of strings
:dismiss_stale_reviews: bool
:require_code_owner_reviews: bool
:required_approving_review_count: int
:user_push_restrictions: list of strings
:team_push_restrictions: list of strings
NOTE: The GitHub API groups strict and contexts together, both must
be submitted. Take care to pass both as arguments even if only one is
changing. Use edit_required_status_checks() to avoid this.
"""
assert strict is github.GithubObject.NotSet or isinstance(strict, bool), strict
assert contexts is github.GithubObject.NotSet or all(
isinstance(element, str) for element in contexts
), contexts
assert enforce_admins is github.GithubObject.NotSet or isinstance(
enforce_admins, bool
), enforce_admins
assert dismissal_users is github.GithubObject.NotSet or all(
isinstance(element, str) for element in dismissal_users
), dismissal_users
assert dismissal_teams is github.GithubObject.NotSet or all(
isinstance(element, str) for element in dismissal_teams
), dismissal_teams
assert dismiss_stale_reviews is github.GithubObject.NotSet or isinstance(
dismiss_stale_reviews, bool
), dismiss_stale_reviews
assert require_code_owner_reviews is github.GithubObject.NotSet or isinstance(
require_code_owner_reviews, bool
), require_code_owner_reviews
assert (
required_approving_review_count is github.GithubObject.NotSet
or isinstance(required_approving_review_count, int)
), (required_approving_review_count)
post_parameters = {}
if (
strict is not github.GithubObject.NotSet
or contexts is not github.GithubObject.NotSet
):
if strict is github.GithubObject.NotSet:
strict = False
if contexts is github.GithubObject.NotSet:
contexts = []
post_parameters["required_status_checks"] = {
"strict": strict,
"contexts": contexts,
}
else:
post_parameters["required_status_checks"] = None
if enforce_admins is not github.GithubObject.NotSet:
post_parameters["enforce_admins"] = enforce_admins
else:
post_parameters["enforce_admins"] = None
if (
dismissal_users is not github.GithubObject.NotSet
or dismissal_teams is not github.GithubObject.NotSet
or dismiss_stale_reviews is not github.GithubObject.NotSet
or require_code_owner_reviews is not github.GithubObject.NotSet
or required_approving_review_count is not github.GithubObject.NotSet
):
post_parameters["required_pull_request_reviews"] = {}
if dismiss_stale_reviews is not github.GithubObject.NotSet:
post_parameters["required_pull_request_reviews"][
"dismiss_stale_reviews"
] = dismiss_stale_reviews
if require_code_owner_reviews is not github.GithubObject.NotSet:
post_parameters["required_pull_request_reviews"][
"require_code_owner_reviews"
] = require_code_owner_reviews
if required_approving_review_count is not github.GithubObject.NotSet:
post_parameters["required_pull_request_reviews"][
"required_approving_review_count"
] = required_approving_review_count
if dismissal_users is not github.GithubObject.NotSet:
post_parameters["required_pull_request_reviews"][
"dismissal_restrictions"
] = {"users": dismissal_users}
if dismissal_teams is not github.GithubObject.NotSet:
if (
"dismissal_restrictions"
not in post_parameters["required_pull_request_reviews"]
):
post_parameters["required_pull_request_reviews"][
"dismissal_restrictions"
] = {}
post_parameters["required_pull_request_reviews"][
"dismissal_restrictions"
]["teams"] = dismissal_teams
else:
post_parameters["required_pull_request_reviews"] = None
if (
user_push_restrictions is not github.GithubObject.NotSet
or team_push_restrictions is not github.GithubObject.NotSet
):
if user_push_restrictions is github.GithubObject.NotSet:
user_push_restrictions = []
if team_push_restrictions is github.GithubObject.NotSet:
team_push_restrictions = []
post_parameters["restrictions"] = {
"users": user_push_restrictions,
"teams": team_push_restrictions,
}
else:
post_parameters["restrictions"] = None
headers, data = self._requester.requestJsonAndCheck(
"PUT",
self.protection_url,
headers={"Accept": Consts.mediaTypeRequireMultipleApprovingReviews},
input=post_parameters,
)
def remove_protection(self):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection <https://developer.github.com/v3/repos/branches>`_
"""
headers, data = self._requester.requestJsonAndCheck(
"DELETE",
self.protection_url,
)
def get_required_status_checks(self):
"""
:calls: `GET /repos/:owner/:repo/branches/:branch/protection/required_status_checks <https://developer.github.com/v3/repos/branches>`_
:rtype: :class:`github.RequiredStatusChecks.RequiredStatusChecks`
"""
headers, data = self._requester.requestJsonAndCheck(
"GET", self.protection_url + "/required_status_checks"
)
return github.RequiredStatusChecks.RequiredStatusChecks(
self._requester, headers, data, completed=True
)
def edit_required_status_checks(
self, strict=github.GithubObject.NotSet, contexts=github.GithubObject.NotSet
):
"""
:calls: `PATCH /repos/:owner/:repo/branches/:branch/protection/required_status_checks <https://developer.github.com/v3/repos/branches>`_
:strict: bool
:contexts: list of strings
"""
assert strict is github.GithubObject.NotSet or isinstance(strict, bool), strict
assert contexts is github.GithubObject.NotSet or all(
isinstance(element, str) for element in contexts
), contexts
post_parameters = {}
if strict is not github.GithubObject.NotSet:
post_parameters["strict"] = strict
if contexts is not github.GithubObject.NotSet:
post_parameters["contexts"] = contexts
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.protection_url + "/required_status_checks",
input=post_parameters,
)
def remove_required_status_checks(self):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/required_status_checks <https://developer.github.com/v3/repos/branches>`_
"""
headers, data = self._requester.requestJsonAndCheck(
"DELETE", self.protection_url + "/required_status_checks"
)
def get_required_pull_request_reviews(self):
"""
:calls: `GET /repos/:owner/:repo/branches/:branch/protection/required_pull_request_reviews <https://developer.github.com/v3/repos/branches>`_
:rtype: :class:`github.RequiredPullRequestReviews.RequiredPullRequestReviews`
"""
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.protection_url + "/required_pull_request_reviews",
headers={"Accept": Consts.mediaTypeRequireMultipleApprovingReviews},
)
return github.RequiredPullRequestReviews.RequiredPullRequestReviews(
self._requester, headers, data, completed=True
)
def edit_required_pull_request_reviews(
self,
dismissal_users=github.GithubObject.NotSet,
dismissal_teams=github.GithubObject.NotSet,
dismiss_stale_reviews=github.GithubObject.NotSet,
require_code_owner_reviews=github.GithubObject.NotSet,
required_approving_review_count=github.GithubObject.NotSet,
):
"""
:calls: `PATCH /repos/:owner/:repo/branches/:branch/protection/required_pull_request_reviews <https://developer.github.com/v3/repos/branches>`_
:dismissal_users: list of strings
:dismissal_teams: list of strings
:dismiss_stale_reviews: bool
:require_code_owner_reviews: bool
:required_approving_review_count: int
"""
assert dismissal_users is github.GithubObject.NotSet or all(
isinstance(element, str) for element in dismissal_users
), dismissal_users
assert dismissal_teams is github.GithubObject.NotSet or all(
isinstance(element, str) for element in dismissal_teams
), dismissal_teams
assert dismiss_stale_reviews is github.GithubObject.NotSet or isinstance(
dismiss_stale_reviews, bool
), dismiss_stale_reviews
assert require_code_owner_reviews is github.GithubObject.NotSet or isinstance(
require_code_owner_reviews, bool
), require_code_owner_reviews
assert (
required_approving_review_count is github.GithubObject.NotSet
or isinstance(required_approving_review_count, int)
), (required_approving_review_count)
post_parameters = {}
if dismissal_users is not github.GithubObject.NotSet:
post_parameters["dismissal_restrictions"] = {"users": dismissal_users}
if dismissal_teams is not github.GithubObject.NotSet:
if "dismissal_restrictions" not in post_parameters:
post_parameters["dismissal_restrictions"] = {}
post_parameters["dismissal_restrictions"]["teams"] = dismissal_teams
if dismiss_stale_reviews is not github.GithubObject.NotSet:
post_parameters["dismiss_stale_reviews"] = dismiss_stale_reviews
if require_code_owner_reviews is not github.GithubObject.NotSet:
post_parameters["require_code_owner_reviews"] = require_code_owner_reviews
if required_approving_review_count is not github.GithubObject.NotSet:
post_parameters[
"required_approving_review_count"
] = required_approving_review_count
headers, data = self._requester.requestJsonAndCheck(
"PATCH",
self.protection_url + "/required_pull_request_reviews",
headers={"Accept": Consts.mediaTypeRequireMultipleApprovingReviews},
input=post_parameters,
)
def remove_required_pull_request_reviews(self):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/required_pull_request_reviews <https://developer.github.com/v3/repos/branches>`_
"""
headers, data = self._requester.requestJsonAndCheck(
"DELETE", self.protection_url + "/required_pull_request_reviews"
)
def get_admin_enforcement(self):
"""
:calls: `GET /repos/:owner/:repo/branches/:branch/protection/enforce_admins <https://developer.github.com/v3/repos/branches>`_
:rtype: bool
"""
headers, data = self._requester.requestJsonAndCheck(
"GET", self.protection_url + "/enforce_admins"
)
return data["enabled"]
def set_admin_enforcement(self):
"""
:calls: `POST /repos/:owner/:repo/branches/:branch/protection/enforce_admins <https://developer.github.com/v3/repos/branches>`_
"""
headers, data = self._requester.requestJsonAndCheck(
"POST", self.protection_url + "/enforce_admins"
)
def remove_admin_enforcement(self):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/enforce_admins <https://developer.github.com/v3/repos/branches>`_
"""
headers, data = self._requester.requestJsonAndCheck(
"DELETE", self.protection_url + "/enforce_admins"
)
def get_user_push_restrictions(self):
"""
:calls: `GET /repos/:owner/:repo/branches/:branch/protection/restrictions/users <https://developer.github.com/v3/repos/branches>`_
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.NamedUser.NamedUser`
"""
return github.PaginatedList.PaginatedList(
github.NamedUser.NamedUser,
self._requester,
self.protection_url + "/restrictions/users",
None,
)
def get_team_push_restrictions(self):
"""
:calls: `GET /repos/:owner/:repo/branches/:branch/protection/restrictions/teams <https://developer.github.com/v3/repos/branches>`_
:rtype: :class:`github.PaginatedList.PaginatedList` of :class:`github.Team.Team`
"""
return github.PaginatedList.PaginatedList(
github.Team.Team,
self._requester,
self.protection_url + "/restrictions/teams",
None,
)
def add_user_push_restrictions(self, *users):
"""
:calls: `POST /repos/:owner/:repo/branches/:branch/protection/restrictions/users <https://developer.github.com/v3/repos/branches>`_
:users: list of strings (user names)
"""
assert all(isinstance(element, str) for element in users), users
headers, data = self._requester.requestJsonAndCheck(
"POST", self.protection_url + "/restrictions/users", input=users
)
def replace_user_push_restrictions(self, *users):
"""
:calls: `PUT /repos/:owner/:repo/branches/:branch/protection/restrictions/users <https://developer.github.com/v3/repos/branches>`_
:users: list of strings (user names)
"""
assert all(isinstance(element, str) for element in users), users
headers, data = self._requester.requestJsonAndCheck(
"PUT", self.protection_url + "/restrictions/users", input=users
)
def remove_user_push_restrictions(self, *users):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions/users <https://developer.github.com/v3/repos/branches>`_
:users: list of strings (user names)
"""
assert all(isinstance(element, str) for element in users), users
headers, data = self._requester.requestJsonAndCheck(
"DELETE", self.protection_url + "/restrictions/users", input=users
)
def add_team_push_restrictions(self, *teams):
"""
:calls: `POST /repos/:owner/:repo/branches/:branch/protection/restrictions/teams <https://developer.github.com/v3/repos/branches>`_
:teams: list of strings (team slugs)
"""
assert all(isinstance(element, str) for element in teams), teams
headers, data = self._requester.requestJsonAndCheck(
"POST", self.protection_url + "/restrictions/teams", input=teams
)
def replace_team_push_restrictions(self, *teams):
"""
:calls: `PUT /repos/:owner/:repo/branches/:branch/protection/restrictions/teams <https://developer.github.com/v3/repos/branches>`_
:teams: list of strings (team slugs)
"""
assert all(isinstance(element, str) for element in teams), teams
headers, data = self._requester.requestJsonAndCheck(
"PUT", self.protection_url + "/restrictions/teams", input=teams
)
def remove_team_push_restrictions(self, *teams):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions/teams <https://developer.github.com/v3/repos/branches>`_
:teams: list of strings (team slugs)
"""
assert all(isinstance(element, str) for element in teams), teams
headers, data = self._requester.requestJsonAndCheck(
"DELETE", self.protection_url + "/restrictions/teams", input=teams
)
def remove_push_restrictions(self):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/restrictions <https://developer.github.com/v3/repos/branches>`_
"""
headers, data = self._requester.requestJsonAndCheck(
"DELETE", self.protection_url + "/restrictions"
)
def get_required_signatures(self):
"""
:calls: `GET /repos/:owner/:repo/branches/:branch/protection/required_signatures <https://developer.github.com/v3/repos/branches>`
"""
headers, data = self._requester.requestJsonAndCheck(
"GET",
self.protection_url + "/required_signatures",
headers={"Accept": Consts.signaturesProtectedBranchesPreview},
)
return data["enabled"]
def add_required_signatures(self):
"""
:calls: `POST /repos/:owner/:repo/branches/:branch/protection/required_signatures <https://developer.github.com/v3/repos/branches>`
"""
headers, data = self._requester.requestJsonAndCheck(
"POST",
self.protection_url + "/required_signatures",
headers={"Accept": Consts.signaturesProtectedBranchesPreview},
)
def remove_required_signatures(self):
"""
:calls: `DELETE /repos/:owner/:repo/branches/:branch/protection/required_signatures <https://developer.github.com/v3/repos/branches>`
"""
headers, data = self._requester.requestJsonAndCheck(
"DELETE",
self.protection_url + "/required_signatures",
headers={"Accept": Consts.signaturesProtectedBranchesPreview},
)