-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwebHook.py
77 lines (60 loc) · 2.8 KB
/
webHook.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 contributors import getContributorList
from logging.handlers import RotatingFileHandler
from Config import Config
from Constants import *
from flask import Flask
from flask import request
import json
import logging
import os
import requests
app = Flask(__name__)
config = Config('app.properties')
username = config.getProperty('DEFAULT', 'username')
password = config.getProperty('DEFAULT', 'password')
message = config.getProperty('comments', 'message')
newUsersOnly = config.getBoolean('DEFAULT', 'new-users')
postPREndPoint = config.getProperty('DEFAULT', 'end-point')
@app.route(postPREndPoint, methods=["POST"])
def postPRCommentToNewContributor():
pullRequestEvent = json.loads(request.data)
# Only comment on PRs that have just been opened
if(pullRequestEvent.has_key(ACTION) and pullRequestEvent[ACTION] == OPENED):
pullRequest = pullRequestEvent[PULL_REQUEST]
githubLoginName = pullRequest[USER][LOGIN]
githubUserId = pullRequest[USER][ID]
ownerLoginName = pullRequest[BASE][REPO][OWNER][LOGIN]
repoName = pullRequest[BASE][REPO][NAME]
url = pullRequest[LINKS][COMMENTS][HREF]
comment = message.format(githubLoginName)
data = BODY_FORMAT.format(comment)
# Only comment on PRs by new contributors
contributor_list = getContributorList(ownerLoginName, repoName)
try:
# If the flag has been set to only send the message to new users
if newUsersOnly:
# Then check if the user is a past contributor
if githubUserId not in contributor_list:
response = requests.post(url, auth=(username, password), data=data)
app.logger.info(COMMENT_MADE_MESSAGE.format(
githubLoginName, githubUserId, url, contributor_list))
else:
app.logger.info(COMMENT_NOT_MADE_MESSAGE.format(
githubLoginName, githubUserId, url, contributor_list))
else:
response = requests.post(url, auth=(username, password), data=data)
app.logger.info(COMMENT_MADE_MESSAGE.format(
githubLoginName, githubUserId, url, contributor_list))
except requests.exceptions.RequestException as e:
app.logger.warning(COMMENT_NOT_MADE_MESSAGE.format(
githubLoginName, githubUserId, url, contributor_list))
return DONE
if __name__ == MAIN:
# Add a stream handler as well as a file handler
fileHandler = RotatingFileHandler('foo.log', maxBytes=10000, backupCount=1)
app.logger.addHandler(fileHandler)
streamHandler = logging.StreamHandler()
app.logger.addHandler(streamHandler)
app.logger.setLevel(logging.INFO)
port = int(os.environ.get(PORT, 5000))
app.run(host='0.0.0.0', port=port)