Skip to content

Commit 41df6f9

Browse files
committed
Fix Branch.protect with the new protection method
After we added the Branch.protection method, we broke the behaviour of Branch.protect which relied on Branch.protection being a dictionary, not a method. This fixes it to use the correct attribute to try to set the existing values. Closes sigmavirus24gh-837
1 parent 8dc4504 commit 41df6f9

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

github3/repos/branch.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"""Implementation of a branch on a repository."""
33
from __future__ import unicode_literals
44

5-
from json import dumps
5+
import json as jsonlib
66

77
from . import commit
88
from .. import decorators
@@ -95,28 +95,43 @@ def protect(self, enforcement=None, status_checks=None):
9595
bool
9696
"""
9797
previous_values = None
98-
if self.protection:
99-
previous_values = self.protection['required_status_checks']
98+
previous_protection = getattr(self, 'original_protection', {})
99+
if previous_protection:
100+
previous_values = previous_protection.get('required_status_checks',
101+
{})
100102
if enforcement is None and previous_values:
101103
enforcement = previous_values['enforcement_level']
102104
if status_checks is None and previous_values:
103105
status_checks = previous_values['contexts']
104106

105-
edit = {'protection': {'enabled': True, 'required_status_checks': {
106-
'enforcement_level': enforcement, 'contexts': status_checks}}}
107-
json = self._json(self._patch(self._api, data=dumps(edit),
108-
headers=self.PREVIEW_HEADERS), 200)
109-
self._update_attributes(json)
110-
return True
107+
edit = {
108+
'protection': {
109+
'enabled': True,
110+
'required_status_checks': {
111+
'enforcement_level': enforcement,
112+
'contexts': status_checks,
113+
},
114+
},
115+
}
116+
resp = self._patch(self._api, data=jsonlib.dumps(edit),
117+
headers=self.PREVIEW_HEADERS)
118+
json = self._json(resp, 200)
119+
if self._boolean(resp, 200, 404):
120+
self._update_attributes(json)
121+
return True
122+
return False
111123

112124
@decorators.requires_auth
113125
def unprotect(self):
114126
"""Disable force push protection on this branch."""
115127
edit = {'protection': {'enabled': False}}
116-
json = self._json(self._patch(self._api, data=dumps(edit),
117-
headers=self.PREVIEW_HEADERS), 200)
118-
self._update_attributes(json)
119-
return True
128+
resp = self._patch(self._api, data=jsonlib.dumps(edit),
129+
headers=self.PREVIEW_HEADERS)
130+
json = self._json(resp, 200)
131+
if self._boolean(resp, 200, 404):
132+
self._update_attributes(json)
133+
return True
134+
return False
120135

121136

122137
class Branch(_Branch):

0 commit comments

Comments
 (0)