2014-01-23 00:21:58 +08:00
|
|
|
import os
|
|
|
|
import json
|
|
|
|
import requests
|
|
|
|
import jenkinsapi
|
|
|
|
from jenkinsapi.jenkins import Jenkins
|
2014-01-23 12:54:35 +08:00
|
|
|
from jenkinsapi.constants import STATUS_SUCCESS
|
2014-06-23 10:26:36 +08:00
|
|
|
from email.mime.text import MIMEText
|
|
|
|
import smtplib
|
|
|
|
def send_mail(sub,title,content):
|
|
|
|
#to_list = os.environ['EMAIL_LIST'].split(' ')
|
|
|
|
mail_user = os.environ['EMAIL_USER']
|
|
|
|
mail_pass = os.environ['EMAIL_PWD']
|
|
|
|
to_list = os.environ['EMAIL_LIST'].split(' ')
|
|
|
|
mail_postfix = 'gmail.com'
|
|
|
|
me = mail_user + "<" + mail_user + "@" + mail_postfix + ">"
|
|
|
|
msg = MIMEText(content, _subtype='plain', _charset='gb2312')
|
|
|
|
msg['Subject'] = sub
|
|
|
|
msg['From'] = me
|
|
|
|
msg['To'] = " ".join(to_list)
|
|
|
|
print 'to users:', msg['To']
|
|
|
|
msg['Content'] = 'test'
|
|
|
|
try:
|
|
|
|
s = smtplib.SMTP('smtp.gmail.com', 587)
|
|
|
|
s.ehlo()
|
|
|
|
s.starttls()
|
|
|
|
s.login(mail_user,mail_pass)
|
|
|
|
s.sendmail(me, to_list, str(msg))
|
|
|
|
print 'info:', me, to_list, str(msg)
|
|
|
|
s.close()
|
|
|
|
return True
|
|
|
|
except Exception, e:
|
|
|
|
print str(e)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def sendEmail(pr, html_url, target_url):
|
2015-03-11 00:08:52 +08:00
|
|
|
sub = "Pull request #" + str(pr) + " build is failed!"
|
|
|
|
title = "build failed!"
|
|
|
|
content = 'to view build result, check:\r\n'+target_url + '\r\nYou can view the failed pr at:\r\n'+html_url
|
|
|
|
send_mail(sub, title, content)
|
2014-01-23 00:21:58 +08:00
|
|
|
|
2014-06-03 11:48:52 +08:00
|
|
|
payload_str = os.environ['payload']
|
2014-01-23 00:21:58 +08:00
|
|
|
payload_str = payload_str.decode('utf-8','ignore')
|
|
|
|
#parse to json obj
|
|
|
|
payload = json.loads(payload_str)
|
2014-02-07 14:57:07 +08:00
|
|
|
#pr = payload['pull_request']
|
|
|
|
url = payload['html_url']
|
2014-01-23 12:54:35 +08:00
|
|
|
print "build pr:" + url
|
2014-06-23 10:26:36 +08:00
|
|
|
pr_num = payload['number']
|
2014-01-23 00:21:58 +08:00
|
|
|
#get statuses url
|
2014-02-07 14:57:07 +08:00
|
|
|
statuses_url = payload['statuses_url']
|
2014-01-23 00:21:58 +08:00
|
|
|
|
|
|
|
J = Jenkins(os.environ['JENKINS_URL'])
|
|
|
|
target_url = os.environ['BUILD_URL']
|
2014-01-23 12:54:35 +08:00
|
|
|
build_number = int(os.environ['BUILD_NUMBER'])
|
2014-06-12 11:30:37 +08:00
|
|
|
data = {"state":"pending", "target_url":target_url, "context":"Jenkins CI", "description":"Build finished!"}
|
2014-01-23 00:21:58 +08:00
|
|
|
access_token = os.environ['GITHUB_ACCESS_TOKEN']
|
|
|
|
Headers = {"Authorization":"token " + access_token}
|
|
|
|
|
2014-01-23 12:54:35 +08:00
|
|
|
result = J[os.environ['JOB_NAME']].get_build(build_number).get_status()
|
2014-01-23 00:21:58 +08:00
|
|
|
|
2014-01-23 12:54:35 +08:00
|
|
|
if(result == STATUS_SUCCESS):
|
2014-01-23 00:21:58 +08:00
|
|
|
data['state'] = "success"
|
2014-06-12 11:30:37 +08:00
|
|
|
data['description'] = "Build successfully!"
|
2014-01-23 00:21:58 +08:00
|
|
|
else:
|
2014-01-23 12:57:18 +08:00
|
|
|
data['state'] = "failure"
|
2014-06-12 11:30:37 +08:00
|
|
|
data['description'] = "Build failed!"
|
2014-06-23 10:26:36 +08:00
|
|
|
sendEmail(pr_num, url, target_url)
|
2014-06-03 11:48:52 +08:00
|
|
|
http_proxy = ''
|
|
|
|
if(os.environ.has_key('HTTP_PROXY')):
|
|
|
|
http_proxy = os.environ['HTTP_PROXY']
|
2014-05-19 17:41:09 +08:00
|
|
|
proxyDict = {'http':http_proxy,'https':http_proxy}
|
|
|
|
requests.post(statuses_url, data=json.dumps(data), headers=Headers, proxies = proxyDict)
|