-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
77 lines (64 loc) · 2.61 KB
/
server.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
# from __future__ import print_function
import json
import os
import sys
from github import Github
from flask import Flask, request
app = Flask(__name__)
# Ensure a github token is set in environment variable
# TODO Retrieve secret from Key Vault or Secret Manager
try:
os.environ["GITHUB_PAT"]
except KeyError:
print("Please set environment variable GITHUB_PAT")
sys.exit(1)
# User or Personal Access Token must have read/write access to repository to add issue to
client = Github(os.environ["GITHUB_PAT"])
# @app.route("/payload", methods=['POST'])
@app.route('/')
def api_root():
# API root level for testing only
return '200'
# Only takes POST
@app.route('/payload', methods=['POST'])
def api_payload():
print(request.get_data())
# print(json.loads(request.get_data()))
api_body = json.loads(request.get_data())
print(api_body)
# determine organisation webhook action type
webhook_action = api_body["action"]
if webhook_action == 'created':
print("New repository name: ", api_body["repository"]["name"])
print("Repository full name: ", api_body["repository"]["full_name"])
# org_repo = client.get_repo(api_body["repository"]["full_name"])
org_repo = client.get_repo(api_body["repository"]["full_name"])
print(org_repo)
# pygithub call to create issue
issue = org_repo.create_issue(
title="A new repository is created",
body="Hooray! A new repository is created. Let's notify @pandaedward and celebrate",
labels=[
org_repo.get_label("good first issue")
]
)
print(issue)
print("First issue created in new repository")
# Identify branch to protect
print("Default branch: ", api_body["repository"]["default_branch"])
default_branch = api_body["repository"]["default_branch"]
# TODO GitHub API V3 issue to be investigated - API response has default_branch master
# Github community support ticket:
# https://github.community/t/github-organization-webhook-respository-created-event-default-branch-field/182245
# pygithub call to get branch
protect_branch = org_repo.get_branch('main')
# Call to edit branch protection
protect_branch.edit_protection(strict=True, required_approving_review_count=2, enforce_admins=True)
print("Edited branch protection rules for: ", default_branch)
else:
# TODO Other actions to be added
print("Not interested in actions other than created")
print("All done")
return 'OK'
if __name__ == '__main__':
app.run(debug=True)