|
2 | 2 | """Implementation of a branch on a repository."""
|
3 | 3 | from __future__ import unicode_literals
|
4 | 4 |
|
5 |
| -from json import dumps |
| 5 | +import json as jsonlib |
6 | 6 |
|
7 | 7 | from . import commit
|
8 | 8 | from .. import decorators
|
@@ -95,28 +95,43 @@ def protect(self, enforcement=None, status_checks=None):
|
95 | 95 | bool
|
96 | 96 | """
|
97 | 97 | 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 | + {}) |
100 | 102 | if enforcement is None and previous_values:
|
101 | 103 | enforcement = previous_values['enforcement_level']
|
102 | 104 | if status_checks is None and previous_values:
|
103 | 105 | status_checks = previous_values['contexts']
|
104 | 106 |
|
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 |
111 | 123 |
|
112 | 124 | @decorators.requires_auth
|
113 | 125 | def unprotect(self):
|
114 | 126 | """Disable force push protection on this branch."""
|
115 | 127 | 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 |
120 | 135 |
|
121 | 136 |
|
122 | 137 | class Branch(_Branch):
|
|
0 commit comments