2015-04-13 09:46:00 +08:00
|
|
|
#Github pull reqest builder for Jenkins
|
2014-01-16 15:16:12 +08:00
|
|
|
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import sys
|
|
|
|
import traceback
|
2014-04-03 11:24:44 +08:00
|
|
|
from jenkinsapi.jenkins import Jenkins
|
|
|
|
|
2014-08-05 18:01:09 +08:00
|
|
|
access_token = os.environ['GITHUB_ACCESS_TOKEN']
|
2015-04-13 09:46:00 +08:00
|
|
|
Headers = {"Authorization": "token " + access_token}
|
2014-08-05 18:01:09 +08:00
|
|
|
|
2014-05-30 12:51:54 +08:00
|
|
|
http_proxy = ''
|
2015-04-13 09:46:00 +08:00
|
|
|
if('HTTP_PROXY' in os.environ):
|
2014-05-30 12:51:54 +08:00
|
|
|
http_proxy = os.environ['HTTP_PROXY']
|
2015-04-13 09:46:00 +08:00
|
|
|
proxyDict = {'http': http_proxy, 'https': http_proxy}
|
2014-05-19 17:41:09 +08:00
|
|
|
|
2014-04-03 11:24:44 +08:00
|
|
|
def check_queue_build(action, pr_num, statuses_url):
|
|
|
|
username = os.environ['JENKINS_ADMIN']
|
|
|
|
password = os.environ['JENKINS_ADMIN_PW']
|
2015-03-11 00:08:52 +08:00
|
|
|
jenkins_url = os.environ['JENKINS_URL']
|
2015-04-13 09:46:00 +08:00
|
|
|
J = Jenkins(jenkins_url, username, password)
|
2014-04-03 11:24:44 +08:00
|
|
|
queues = J.get_queue()
|
2015-04-13 09:46:00 +08:00
|
|
|
for key, queue in queues.iteritems():
|
|
|
|
q_payload_str = queue.get_parameters()['payload'].decode('utf-8', 'ignore')
|
|
|
|
q_payload = json.loads(q_payload_str)
|
|
|
|
q_pr_num = q_payload['number']
|
|
|
|
q_statuses_url = q_payload['statuses_url']
|
|
|
|
if(q_pr_num == pr_num):
|
|
|
|
if(action == 'closed') or (q_statuses_url != statuses_url):
|
|
|
|
queues.delete_item(queue)
|
|
|
|
target_url = os.environ['JOB_PULL_REQUEST_BUILD_URL']
|
|
|
|
data = {"state": "error", "target_url": target_url}
|
|
|
|
try:
|
|
|
|
requests.post(statuses_url, data=json.dumps(data),
|
|
|
|
headers=Headers, proxies=proxyDict)
|
2015-04-16 15:51:08 +08:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
2014-01-16 15:16:12 +08:00
|
|
|
|
|
|
|
def main():
|
|
|
|
#get payload from os env
|
|
|
|
payload_str = os.environ['payload']
|
|
|
|
#parse to json obj
|
|
|
|
payload = json.loads(payload_str)
|
|
|
|
|
|
|
|
#get pull number
|
|
|
|
pr_num = payload['number']
|
|
|
|
print 'pr_num:' + str(pr_num)
|
2015-04-13 09:46:00 +08:00
|
|
|
payload_forword = {"number": pr_num}
|
|
|
|
|
2014-01-16 15:16:12 +08:00
|
|
|
#build for pull request action 'open' and 'synchronize', skip 'close'
|
|
|
|
action = payload['action']
|
|
|
|
print 'action: ' + action
|
2014-02-07 14:57:07 +08:00
|
|
|
payload_forword['action'] = action
|
2015-04-13 09:46:00 +08:00
|
|
|
|
2014-02-07 14:57:07 +08:00
|
|
|
pr = payload['pull_request']
|
2014-01-16 15:16:12 +08:00
|
|
|
url = pr['html_url']
|
|
|
|
print "url:" + url
|
2014-02-07 14:57:07 +08:00
|
|
|
payload_forword['html_url'] = url
|
2015-04-13 09:46:00 +08:00
|
|
|
|
2014-01-16 15:16:12 +08:00
|
|
|
#get statuses url
|
|
|
|
statuses_url = pr['statuses_url']
|
2014-02-07 14:57:07 +08:00
|
|
|
payload_forword['statuses_url'] = statuses_url
|
2014-01-16 15:16:12 +08:00
|
|
|
|
|
|
|
#get pr target branch
|
|
|
|
branch = pr['base']['ref']
|
2014-02-07 14:57:07 +08:00
|
|
|
payload_forword['branch'] = branch
|
2014-01-16 15:16:12 +08:00
|
|
|
|
|
|
|
#set commit status to pending
|
2014-02-20 17:08:53 +08:00
|
|
|
target_url = os.environ['JOB_PULL_REQUEST_BUILD_URL']
|
2014-04-03 11:42:34 +08:00
|
|
|
|
2015-04-13 09:46:00 +08:00
|
|
|
try:
|
2014-04-28 14:34:34 +08:00
|
|
|
check_queue_build(action, pr_num, statuses_url)
|
|
|
|
except:
|
|
|
|
print 'Can not find build in queue'
|
2015-04-13 09:46:00 +08:00
|
|
|
|
2015-07-09 10:10:37 +08:00
|
|
|
if(action == 'closed' or action == 'labeled' or action == 'unassigned'
|
|
|
|
or action == 'assigned' or action == 'unlabeled'
|
|
|
|
or branch == 'v2' or branch == 'v3-doc'):
|
2015-04-13 09:46:00 +08:00
|
|
|
print 'pull request #' + str(pr_num) + ' is ' + action + ', no build triggered'
|
2014-01-16 15:16:12 +08:00
|
|
|
return(0)
|
2015-04-13 09:46:00 +08:00
|
|
|
|
|
|
|
r = requests.get(pr['url'] + "/commits", headers=Headers, proxies=proxyDict)
|
2014-01-16 15:16:12 +08:00
|
|
|
commits = r.json()
|
2015-04-13 09:46:00 +08:00
|
|
|
last_commit = commits[len(commits) - 1]
|
2014-01-16 15:16:12 +08:00
|
|
|
message = last_commit['commit']['message']
|
|
|
|
|
|
|
|
pattern = re.compile("\[ci(\s+)skip\]", re.I)
|
2014-04-28 14:54:29 +08:00
|
|
|
result_commit_title = pattern.search(message)
|
|
|
|
|
|
|
|
title = pr['title']
|
|
|
|
result_pr_title = pattern.search(title)
|
|
|
|
if result_commit_title is not None or result_pr_title is not None:
|
2014-01-16 15:16:12 +08:00
|
|
|
print 'skip build for pull request #' + str(pr_num)
|
|
|
|
return(0)
|
2015-04-13 09:46:00 +08:00
|
|
|
|
|
|
|
data = {"state": "pending", "target_url": target_url,
|
|
|
|
"context": "Jenkins CI",
|
|
|
|
"description": "Waiting available build machine..."}
|
2014-01-16 15:16:12 +08:00
|
|
|
|
|
|
|
try:
|
2015-04-13 09:46:00 +08:00
|
|
|
requests.post(statuses_url, data=json.dumps(data),
|
|
|
|
headers=Headers, proxies=proxyDict)
|
2014-01-16 15:16:12 +08:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2014-02-08 16:08:50 +08:00
|
|
|
job_trigger_url = os.environ['JOB_TRIGGER_URL']
|
2014-01-16 15:16:12 +08:00
|
|
|
#send trigger and payload
|
2015-04-13 09:46:00 +08:00
|
|
|
post_data = {'payload': ""}
|
|
|
|
post_data['payload'] = json.dumps(payload_forword)
|
|
|
|
requests.post(job_trigger_url, data=post_data, proxies=proxyDict)
|
2014-01-16 15:16:12 +08:00
|
|
|
|
|
|
|
return(0)
|
|
|
|
|
|
|
|
# -------------- main --------------
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys_ret = 0
|
2015-04-13 09:46:00 +08:00
|
|
|
try:
|
2014-01-16 15:16:12 +08:00
|
|
|
sys_ret = main()
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
sys_ret = 1
|
|
|
|
finally:
|
|
|
|
sys.exit(sys_ret)
|