-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
43 lines (37 loc) · 1.25 KB
/
bot.py
File metadata and controls
43 lines (37 loc) · 1.25 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
from flask import Flask
from flask import request as req
from flask import render_template
from githubbot.utils import PULL_REQUEST_COMMENT_URL
import requests
import json
app = Flask(__name__)
@app.route('/', methods=['GET'])
def home():
return "Welcome"
@app.route('/recv', methods=['POST'])
def recv_webhook_event():
event_type = req.headers['X-GitHub-Event']
if event_type == 'pull_request':
payload = json.loads(req.data)
if payload['action'] == 'opened':
user, repo = payload['repository']['full_name'].split('/')
create_comment_on_pr(user, repo, payload['number'])
return "Success"
def make_gh_request(endpoint, req_type='GET', payload=None):
headers = {
'Authorization': 'Basic SmF5amVldEF0R2l0aHViOkpheWplZXRAMTk5OQ=='
}
if req_type == 'POST':
r = requests.post(endpoint, json=payload,headers=headers)
elif req_type == 'GET':
r = requests.get(endpoint,headers=headers)
return r.status_code
def create_comment_on_pr(org, repo, pull):
payload = {
'body':'Thanks for opening this PR. Please feel free to request a review.'
}
return make_gh_request(
PULL_REQUEST_COMMENT_URL.format(org, repo, pull),
'POST',
payload
)