-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlambda_function.py
More file actions
112 lines (101 loc) · 4.21 KB
/
Copy pathlambda_function.py
File metadata and controls
112 lines (101 loc) · 4.21 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import json # pylint: disable=import-error
import os # pylint: disable=import-error
import time # pylint: disable=import-erro
import requests # pylint: disable=import-error
def lambda_handler(event, context):
if event.get('body'):
event=json.loads(event['body'])
# Store incoming json data from webhook
payload = event
user = "bitbucket90"
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" or payload["action"] == "publicized" or payload["ref"] == "main":
# 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": None,
"pull_request_reviews_enforcement_level": "off",
"required_approving_review_count": 1,
"dismiss_stale_reviews_on_push": True,
"require_code_owner_review": True,
"authorized_dismissal_actors_only": False,
"ignore_approvals_from_contributors": False,
"required_status_checks_enforcement_level": "non_admins",
"strict_required_status_checks_policy": False,
"signature_requirement_enforcement_level": "off",
"linear_history_requirement_enforcement_level": "off",
"enforce_admins": False,
"allow_force_pushes_enforcement_level": "off",
"allow_deletions_enforcement_level": "off",
"merge_queue_enforcement_level": "off",
"required_deployments_enforcement_level": "off",
"required_conversation_resolution_level": "off",
"authorized_actors_only": True,
"authorized_actor_names": [
"bitbucket90"
],
"required_pull_request_reviews": None,
"restrictions": None,
}
session = requests.session()
session.auth = (user, cred)
response_1 = session.put(
payload["repository"]["url"] + "/branches/main/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 Branch Protection Added",
"body": "@"
+ user
+ " A new branch protection was added to the main 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,
"No Branch found- Creating one",
)
except KeyError:
# Ignore POST payload since it is not a create action
pass
# TODO implement
return {
'statusCode': 200,
'body': json.dumps('successfull')
}