|
1 | 1 | # -*- coding: utf-8 -*-
|
2 | 2 | from __future__ import unicode_literals
|
3 | 3 |
|
| 4 | +from json import dumps |
4 | 5 | from ..models import GitHubCore
|
5 | 6 | from .commit import RepoCommit
|
6 | 7 |
|
@@ -30,3 +31,36 @@ def _update_attributes(self, branch):
|
30 | 31 |
|
31 | 32 | def _repr(self):
|
32 | 33 | 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