forked from carlos-jenkins/python-github-webhooks
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathall
More file actions
executable file
·66 lines (50 loc) · 1.78 KB
/
all
File metadata and controls
executable file
·66 lines (50 loc) · 1.78 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python
# Basic webhook handler for starting bash-cmake builder
import sys
import json
import subprocess
import requests
import os
#############################
# Slack webhook publisher
#############################
def send_slack_message(builder,isPre,repo,user,msg):
spayload = {
"text": "",
"username": builder,
"channel": "#notifications",
"mrkdwn": True
}
if isPre:
spayload["text"] = "-->*starting building* on %s\n-->%s push: _%s_\n" % (repo, user, msg)
else:
spayload["text"] = "-->*building results* for %s: %s" % (user,msg)
# recall eveiroment variable (slack webhook post url)
slackwebhook=os.environ['SLACK_INCOMING_WEB_HOOK']
req = requests.post(slackwebhook, json.dumps(spayload), headers={'content-type': 'application/json'})
#######################
####### M A I N #######
with open(sys.argv[1], 'r') as jsf:
payload = json.loads(jsf.read())
builder="MatrixCreatorBuilder"
repo = payload['repository']['name']
git_url = payload['repository']['git_url']
user = payload['head_commit']['author']['name']
msg = payload['head_commit']['message']
commit = payload['head_commit']['id']
commit_url=payload['head_commit']['url']
# notify pre-execution
send_slack_message(builder,True,repo,user,msg)
p=subprocess.Popen(['./hooks/build_cmake_repo',git_url,commit,repo],stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if p.returncode != 0:
print ("ERROR: pipe execution: " "(error code: %s)" % p.returncode)
msg="*failed* ```%s```" % ('\n'.join(stderr.splitlines()[-4:]))
else:
msg="*success*"
msg=msg+"\n[commit](%s)" % (commit_url)
# notify results
send_slack_message(builder,False,repo,user,msg)
outfile = '/tmp/hook-{}.log'.format(repo)
with open(outfile, 'w') as f:
f.write(json.dumps(payload))