-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (57 loc) · 2.13 KB
/
main.py
File metadata and controls
68 lines (57 loc) · 2.13 KB
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
from flask import escape
import functions_framework
import requests
from requests.auth import HTTPBasicAuth
import json
import os
@functions_framework.http
def hook_root(request):
"""HTTP Cloud Function.
Args:
request (flask.Request): The request object.
<https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
Returns:
The response text, or any set of values that can be turned into a
Response object using `make_response`
<https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
"""
protection = """
{
"required_status_checks":null,
"enforce_admins":null,
"required_pull_request_reviews":{
"dismissal_restrictions":{
},
"dismiss_stale_reviews":false,
"require_code_owner_reviews":true,
"required_approving_review_count":1
},
"restrictions":null
}
"""
issue = """
{
"title":"Automated Branch Protection",
"body":"Automating branch protection enabled @denialofself"
}
"""
PAT = os.environ.get('PAT')
print("Got request")
request_json = request.get_json(silent=True)
print("Logging request body: ")
print(request_json)
if request_json['action'] == 'created':
print("Repository action type was: " + request_json['action'])
print("Automating protection of the default(main) branch")
protection_response = requests.put(url=f"https://api.github.com/repos/ASampleOrg/{request_json['repository']['name']}/branches/main/protection", data=protection, auth=HTTPBasicAuth('denialofself', PAT))
print("Protection response: ")
print(protection_response.status_code)
print(protection_response.text)
print("Opening issue on repository create")
issue_response = requests.post(url=f"https://api.github.com/repos/ASampleOrg/{request_json['repository']['name']}/issues", data=issue, auth=HTTPBasicAuth('denialofself', PAT))
print("Issue response: ")
print(issue_response.status_code)
print(issue_response.text)
else:
print("Action was not created, skipping")
return request_json