This repository has been archived by the owner on Dec 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.py
89 lines (80 loc) · 3.35 KB
/
app.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import json # pylint: disable=import-error
import os # pylint: disable=import-error
import time # pylint: disable=import-error
import requests # pylint: disable=import-error
from flask import Flask, request # pylint: disable=import-error
app = Flask(__name__)
@app.route("/", methods=["POST"])
def webhook():
# Store incoming json data from webhook
payload = request.get_json()
user = "zkoppert"
cred = os.environ["GH_TOKEN"]
if payload is None:
print("POST was not formatted in JSON")
# Verify the repo was created
try:
if payload["action"] == "created":
# Delay needed for server to be create the page, otherwise a 404 returns
time.sleep(1)
# Create branch protection for the master branch of the repo
branch_protection = {
"required_status_checks": {"strict": True, "contexts": ["default"]},
"enforce_admins": False,
"required_pull_request_reviews": None,
"restrictions": None,
}
session = requests.session()
session.auth = (user, cred)
response_1 = session.put(
payload["repository"]["url"] + "/branches/master/protection",
json.dumps(branch_protection),
)
if response_1.status_code == 200:
print(
"Branch protection created successfully. Status code: ",
response_1.status_code,
)
# Create issue in repo notifying user of branch protection
try:
if payload["repository"]["has_issues"]:
issue = {
"title": "New Protection Added",
"body": "@"
+ user
+ " A new branch protection was added to the master branch.",
}
session = requests.session()
session.auth = (user, cred)
response_2 = session.post(
payload["repository"]["url"] + "/issues", json.dumps(issue)
)
if response_2.status_code == 201:
print(
"Issue created successfully. Status code: ",
response_2.status_code,
)
else:
print(
"Unable to create issue. Status code: ",
response_2.status_code,
)
else:
print(
"This repo has no issues so one cannot be created at this time."
)
except KeyError:
# Request did not contain information about if the repository has issues enabled
pass
else:
print(response_1.content)
print(
"Unable to create branch protection. Status code: ",
response_1.status_code,
)
except KeyError:
# Ignore POST payload since it is not a create action
pass
return "OK"
if __name__ == "__main__":
app.run()