-
Notifications
You must be signed in to change notification settings - Fork 0
/
protect.py
52 lines (41 loc) · 1.38 KB
/
protect.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
#
# Enable/disable GitHub protected branch status
# Usage: ghb protect USER/REPO BRANCH [--disable] [STATUSES...]
#
# Example: ghb protect keith ghb master travis
# This would enable protection on master forcing the travis build to pass
#
# Note: Requiring administrators to go through protection is currently hardcoded
#
import json
import requests
from .helpers import credentials
URL = "https://api.github.com/repos/%s/branches/%s"
def url_from_options(options):
return URL % (options["repo"], options["branch"])
def data_for_options(options):
enabled = not options["disable"]
data = {"protection": {"enabled": enabled}}
statuses = options["statuses"]
if statuses:
data["protection"]["required_status_checks"] = {
"enforcement_level": "everyone",
"contexts": statuses,
}
return data
def main(args):
options = vars(args)
username, password = credentials.credentials()
headers = {"Accept": "application/vnd.github.loki-preview"}
payload = json.dumps(data_for_options(options))
r = requests.patch(
url_from_options(options),
auth=(username, password),
headers=headers,
data=payload,
)
if r.status_code == 200:
print("Success")
else:
# Currently not sure about what errors can be produced
print(f"Error. HTTP Code: {r.status_code} JSON: {r.json()}")