forked from spoqa/gh-issues-channel-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
178 lines (148 loc) · 5.64 KB
/
app.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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import os
import json
import requests
from flask import Flask, request, jsonify
from responses import Payload
try:
SLACK_REQUEST_URL = os.environ["SLACK_REQUEST_URL"]
GITHUB_OAUTH_TOKEN = os.environ["GITHUB_OAUTH_TOKEN"]
except KeyError as e:
import sys
print "Please check environment variables : %s" % e
sys.exit(1)
app = Flask(__name__)
def slack_request(payload):
return requests.post(SLACK_REQUEST_URL, data=json.dumps(payload.to_dict()))
class GhEventHandler(object):
_handler_map = {}
def add_event(self, event, **options):
def decorator(handler):
if event not in self._handler_map:
self._handler_map[event] = dict(actions={})
if "actions" in options:
assert len(options["actions"]) != 0, \
"{0} has empty actions".format(handler.__name__)
for action in options["actions"]:
self._handler_map[event]["actions"][action] = handler
else:
self._handler_map[event]["default"] = handler
def decorated_function(*args, **kwargs):
return handler(*args, **kwargs)
return decorated_function
return decorator
def handle(self):
event = request.headers.get("X-GitHub-Event")
if event in self._handler_map:
try:
data = json.loads(request.data)
except ValueError:
data = json.loads(request.values.get("payload"))
try:
action = data["action"]
except KeyError:
action = ""
if action in self._handler_map[event]["actions"]:
self._handler_map[event]["actions"][action](data)
elif "default" in self._handler_map[event]:
self._handler_map[event]["default"](data)
handler = GhEventHandler()
@handler.add_event("issue_comment")
def issue_comment(data):
for label in data["issue"]["labels"]:
slack_request(
Payload(channel=label["name"],
number=data["issue"]["number"],
user=data["comment"]["user"]["login"],
body=data["comment"]["body"],
url=data["comment"]["html_url"],
label="New issue comment"
)
)
@handler.add_event("issues")
def issues(data):
color = "#6cc644"
for label in data["issue"]["labels"]:
slack_request(
Payload(channel=label["name"],
number=data["issue"]["number"],
title=data["issue"]["title"],
user=data["sender"]["login"],
body=data["issue"]["body"],
url=data["issue"]["html_url"],
label="New issue",
color=color,
)
)
@handler.add_event("issues", actions=["closed", "reopened"])
def issues(data):
color = "#bd2c00" if data["action"] == "closed" else "#6cc644"
for label in data["issue"]["labels"]:
slack_request(
Payload(channel=label["name"],
number=data["issue"]["number"],
action=data["action"],
title=data["issue"]["title"],
user=data["sender"]["login"],
body=data["issue"]["body"],
url=data["issue"]["html_url"],
label="Issue status changed",
color=color
)
)
@handler.add_event("pull_request")
def pull_requests(data):
pull_request = data["pull_request"]
issue_url = pull_request["issue_url"]
result = json.loads(requests.get(issue_url, auth=(GITHUB_OAUTH_TOKEN,
"x-oauth-basic")).text)
if pull_request["state"] == "closed":
if pull_request["merged"] == True:
color = "#6e5494"
state = "merged"
else:
color = "#bd2c00"
state = "closed"
else:
color = "#6cc644"
state = "opened"
_label = "New pull request" if pull_request["state"] == "open" else \
"Pull request state changed"
for label in result["labels"]:
slack_request(
Payload(number=result["number"],
channel=label["name"],
action=state,
title=pull_request["title"],
user=data["sender"]["login"],
body=pull_request["body"],
url=pull_request["html_url"],
label=_label,
color=color
)
)
@handler.add_event("pull_request_review_comment")
def pull_request_review_comment(data):
data = data["comment"]
pr_url = data["pull_request_url"]
result = json.loads(requests.get(pr_url, auth=(GITHUB_OAUTH_TOKEN,
"x-oauth-basic")).text)
result = json.loads(requests.get(result["issue_url"],
auth=(GITHUB_OAUTH_TOKEN,
"x-oauth-basic")).text)
for label in result["labels"]:
slack_request(
Payload(channel=label["name"],
title=data["id"],
commit_id=data["commit_id"],
user=data["user"]["login"],
body=data["body"],
url=data["_links"]["html"]["href"],
label="Pull request review comment"
)
)
@app.route("/", methods=["GET", "POST"])
def index():
handler.handle()
return jsonify(result="success")
if __name__ == "__main__":
app.run(debug=True)