8000 Permit changing the branch protection status. · pythonthings/github3.py@4bd0237 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4bd0237

Browse files
committed
Permit changing the branch protection status.
1 parent 01fd26e commit 4bd0237

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

github3/repos/branch.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
from json import dumps
45
from ..models import GitHubCore
56
from .commit import RepoCommit
67

@@ -30,3 +31,36 @@ def _update_attributes(self, branch):
3031

3132
def _repr(self):
3233
return '<Repository Branch [{0}]>'.format(self.name)
34+
35+
def protect(self, enforcement, status_checks):
36+
"""Enable force push protection and configure status check enforcement.
37+
38+
See: http://git.io/v4Gvu
39+
40+
:param str enforcement: (required), Specifies the enforcement level of
41+
the status checks. Must be one of 'off', 'non_admins', or
42+
'everyone'.
43+
:param list status_checks: (required), An iterable of strings naming
44+
status checks that must pass before merging.
45+
"""
46+
edit = {'protection': {'enabled': True, 'required_status_checks': {
47+
'enforcement_level': enforcement, 'contexts': status_checks}}}
48+
json = self._json(self._patch(self.links['self'], data=dumps(edit),
49+
headers=self.PREVIEW_HEADERS), 200)
50+
51+
# When attempting to clear `contexts`, the reply from github doesn't
52+
# currently reflect the actual value. Let's fix that for now.
53+
cur_contexts = self.protection['required_status_checks']['contexts']
54+
if status_checks == [] != cur_contexts:
55+
json['protection']['required_status_checks']['contexts'] = []
56+
57+
self._update_attributes(json)
58+
return True
59+
60+
def unprotect(self):
61+
"""Disable force push protection on this branch."""
62+
edit = {'protection': {'enabled': False}}
63+
json = self._json(self._patch(self.links['self'], data=dumps(edit),
64+
headers=self.PREVIEW_HEADERS), 200)
65+
self._update_attributes(json)
66+
return True

0 commit comments

Comments
 (0)
0