forked from openfaas/workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
61 lines (45 loc) · 1.92 KB
/
handler.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
import requests, json, os, sys
from github import Github
def handle(req):
event_header = os.getenv("Http_X_Github_Event")
if not event_header == "issues":
print("Unable to handle X-GitHub-Event: " + event_header)
sys.exit(1)
return
gateway_hostname = os.getenv("gateway_hostname", "gateway")
payload = json.loads(req)
if not payload["action"] == "opened":
print("Action not supported: " + payload["action"])
sys.exit(1)
return
# Call sentimentanalysis
res = requests.post('http://' + gateway_hostname + ':8080/function/sentimentanalysis',
data= payload["issue"]["title"]+" "+payload["issue"]["body"])
if res.status_code != 200:
print("Error with sentimentanalysis, expected: %d, got: %d\n" % (200, res.status_code))
sys.exit(1)
# Read the positive_threshold from configuration
positive_threshold = float(os.getenv("positive_threshold", "0.2"))
polarity = res.json()['polarity']
# Call back to GitHub to apply a label
apply_label(polarity,
payload["issue"]["number"],
payload["repository"]["full_name"],
positive_threshold)
return "Repo: %s, issue: %s, polarity: %f" % (payload["repository"]["full_name"], payload["issue"]["number"], polarity)
def apply_label(polarity, issue_number, repo, positive_threshold):
with open("/var/openfaas/secrets/auth-token","r") as authToken:
g = Github(authToken.read())
repo = g.get_repo(repo)
issue = repo.get_issue(issue_number)
has_label_positive = False
has_label_review = False
for label in issue.labels:
if label == "positive":
has_label_positive = True
if label == "review":
has_label_review = True
if polarity > positive_threshold and not has_label_positive:
issue.set_labels("positive")
elif not has_label_review:
issue.set_labels("review")