forked from Charcoal-SE/SmokeDetector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
continuousintegration.py
77 lines (65 loc) · 3.13 KB
/
continuousintegration.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
import socket
import requests
import re
import os
from globalvars import GlobalVars
import datetime
import time
import random
def watch_ci():
HOST = ''
PORT = 49494
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print "CI Socket Created"
try:
s.bind((HOST, PORT))
except socket.error as msg:
print 'Bind Failed. Error code: ' + str(msg[0])
return
s.listen(10)
print 'listening for CI changes'
while 1:
conn, addr = s.accept()
addr_host = socket.gethostbyaddr(addr[0])[0]
is_circleci = True if re.compile(r"ec2-\d{1,3}-\d{1,3}-\d{1,3}-\d{1,3}.compute-1.amazonaws.com").search(addr_host) else False
print 'Received request from ' + addr[0] + " ; " + "verified as CircleCI" if is_circleci else "NOT verified as CircleCI!"
if not is_circleci:
conn.close()
continue
conn.send('HTTP/1.1 200 OK\n' +
'Content-Type: text/plain\n'
'Content-Length: 2\n' +
'\nOK\n')
conn.close()
r = requests.get('https://api.github.com/repos/Charcoal-SE/SmokeDetector/git/refs/heads/master')
latest_sha = r.json()["object"]["sha"]
print latest_sha
r = requests.get('https://api.github.com/repos/Charcoal-SE/SmokeDetector/commits/' + latest_sha + '/statuses')
for status in r.json():
state = status["state"]
target_url = status["target_url"]
ci_platform = ""
if status["context"] == "ci/circleci":
ci_platform = "Circle"
elif status["context"] == "continuous-integration/travis-ci/push":
ci_platform = "Travis"
else:
ci_platform = "some unrecognized test service"
print state
if state == "success":
if datetime.datetime.strptime(status["updated_at"], '%Y-%m-%dT%H:%M:%SZ') > datetime.datetime.now() - datetime.timedelta(seconds=60):
r = requests.get('https://api.github.com/repos/Charcoal-SE/SmokeDetector/commits/' + latest_sha)
commit_message = r.json()["commit"]["message"]
print commit_message
if "autopull" in commit_message:
GlobalVars.charcoal_hq.send_message("[CI build passed]({}) on {}. Commit message contains 'autopull', pulling...".format(target_url, ci_platform))
time.sleep(2)
os._exit(3)
else:
GlobalVars.charcoal_hq.send_message("[CI build passed]({}) on {}. Ready to pull!".format(target_url, ci_platform))
continue
elif state == "error" or state == "failure":
if datetime.datetime.strptime(status["updated_at"], '%Y-%m-%dT%H:%M:%SZ') > datetime.datetime.now() - datetime.timedelta(seconds=10):
GlobalVars.charcoal_hq.send_message("[CI build failed]({})".format(target_url) + ", *someone* (prolly " + random.choice(["Undo", "Manish", "ProgramFOX", "hichris", "Normal"]) + ") borked something!")
continue
s.close()