2014-03-03 17:32:29 +08:00
|
|
|
#Github pull reqest builder for Jenkins
|
|
|
|
|
|
|
|
import json
|
|
|
|
import re
|
|
|
|
import os
|
|
|
|
import requests
|
|
|
|
import sys
|
|
|
|
import traceback
|
|
|
|
import urllib2
|
|
|
|
|
|
|
|
def main():
|
|
|
|
#get payload from os env
|
|
|
|
payload_str = os.environ['payload']
|
|
|
|
#parse to json obj
|
|
|
|
payload = json.loads(payload_str)
|
|
|
|
|
2014-03-10 16:35:55 +08:00
|
|
|
issue = payload['issue']
|
|
|
|
#get pull number
|
|
|
|
pr_num = issue['number']
|
|
|
|
print 'pr_num:' + str(pr_num)
|
|
|
|
payload_forword = {"number":pr_num}
|
|
|
|
|
2014-03-03 17:32:29 +08:00
|
|
|
comment = payload['comment']
|
|
|
|
#get comment body
|
|
|
|
comment_body = comment['body']
|
|
|
|
print comment_body
|
2014-04-16 11:05:27 +08:00
|
|
|
#will check 'ci' comment
|
|
|
|
searchCI = re.search("\[ci.*\]", comment_body)
|
2014-03-19 17:57:19 +08:00
|
|
|
|
2014-04-16 11:05:27 +08:00
|
|
|
#will check console/console create
|
2014-03-20 10:24:10 +08:00
|
|
|
searchConsole = re.search('\[console.*\]', comment_body)
|
2014-03-19 17:57:19 +08:00
|
|
|
|
2014-04-16 11:05:27 +08:00
|
|
|
if searchCI is None and searchConsole is None:
|
2014-03-03 18:36:32 +08:00
|
|
|
print 'skip build for pull request #' + str(pr_num)
|
2014-03-03 17:32:29 +08:00
|
|
|
return(0)
|
|
|
|
|
|
|
|
#build for pull request action 'open' and 'synchronize', skip 'close'
|
|
|
|
action = issue['state']
|
|
|
|
print 'action: ' + action
|
|
|
|
payload_forword['action'] = action
|
|
|
|
|
|
|
|
pr = issue['pull_request']
|
|
|
|
url = pr['html_url']
|
|
|
|
print "url:" + url
|
|
|
|
payload_forword['html_url'] = url
|
|
|
|
|
|
|
|
#get pull request info
|
2014-03-04 11:50:46 +08:00
|
|
|
req = 'https://api.github.com/repos/cocos2d/cocos2d-x/pulls/' + str(pr_num)
|
2014-03-03 17:32:29 +08:00
|
|
|
pr_payload = ''
|
|
|
|
try:
|
|
|
|
pr_payload = urllib2.urlopen(req).read()
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
|
|
|
repository = json.loads(pr_payload)
|
|
|
|
#get statuses url
|
|
|
|
statuses_url = repository['statuses_url']
|
|
|
|
payload_forword['statuses_url'] = statuses_url
|
|
|
|
print 'statuses_url: ' + statuses_url
|
2014-04-16 18:36:05 +08:00
|
|
|
#get comments url
|
|
|
|
comments_url = repository['comments_url']
|
|
|
|
payload_forword['comments_url'] = comments_url
|
|
|
|
print 'comments_url: ' + comments_url
|
2014-03-03 17:32:29 +08:00
|
|
|
|
|
|
|
#get pr target branch
|
|
|
|
branch = repository['base']['ref']
|
|
|
|
payload_forword['branch'] = branch
|
|
|
|
print 'branch: ' + branch
|
|
|
|
|
|
|
|
#set commit status to pending
|
|
|
|
target_url = os.environ['JOB_PULL_REQUEST_BUILD_URL']
|
|
|
|
|
|
|
|
if(action == 'closed'):
|
|
|
|
print 'pull request #' + str(pr_num) + ' is '+action+', no build triggered'
|
|
|
|
return(0)
|
|
|
|
|
|
|
|
data = {"state":"pending", "target_url":target_url}
|
|
|
|
access_token = os.environ['GITHUB_ACCESS_TOKEN']
|
|
|
|
Headers = {"Authorization":"token " + access_token}
|
|
|
|
|
|
|
|
try:
|
2014-04-16 18:36:05 +08:00
|
|
|
if searchCI:
|
|
|
|
ciOper = searchCI.group()
|
|
|
|
if('rebuild' in ciOper):
|
|
|
|
requests.post(statuses_url, data=json.dumps(data), headers=Headers)
|
2014-03-03 17:32:29 +08:00
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
|
2014-03-19 17:57:19 +08:00
|
|
|
job_trigger_url = ''
|
2014-04-16 11:05:27 +08:00
|
|
|
if searchCI:
|
|
|
|
ciOper = searchCI.group()
|
2014-04-16 18:36:05 +08:00
|
|
|
if('rebuild' in ciOper):
|
2014-04-16 11:05:27 +08:00
|
|
|
job_trigger_url = os.environ['JOB_PULL_REQUEST_BUILD_TRIGGER_URL']
|
|
|
|
if('emptytest' in ciOper):
|
|
|
|
job_trigger_url = os.environ['JOB_EMPTYTEST_TRIGGER_URL']
|
2014-04-22 11:41:14 +08:00
|
|
|
if('release' in ciOper):
|
|
|
|
searchTag = re.search('\[ci release (.*)\]', ciOper)
|
|
|
|
if searchTag:
|
|
|
|
ci_tag = searchTag.group(1)
|
|
|
|
payload_forword['tag'] = ci_tag
|
|
|
|
job_trigger_url = os.environ['JOB_RELEASE_TRIGGER_URL']
|
2014-03-20 10:24:10 +08:00
|
|
|
if searchConsole:
|
|
|
|
consoleOper = searchConsole.group()
|
2014-04-16 11:05:27 +08:00
|
|
|
job_trigger_url = os.environ['JOB_CONSOLE_TEST_TRIGGER_URL']
|
2014-03-20 10:24:10 +08:00
|
|
|
payload_forword['console'] = consoleOper
|
2014-03-19 17:57:19 +08:00
|
|
|
print 'job_trigger_url is: ', job_trigger_url
|
|
|
|
|
2014-03-03 17:32:29 +08:00
|
|
|
#send trigger and payload
|
2014-04-23 18:10:39 +08:00
|
|
|
if('tag' in payload_forword):
|
|
|
|
post_data = {'tag':""}
|
|
|
|
post_data['tag'] = payload_forword['tag']
|
|
|
|
else:
|
|
|
|
post_data = {'payload':""}
|
|
|
|
post_data['payload']= json.dumps(payload_forword)
|
2014-03-03 17:32:29 +08:00
|
|
|
requests.post(job_trigger_url, data=post_data)
|
|
|
|
|
|
|
|
return(0)
|
|
|
|
|
|
|
|
# -------------- main --------------
|
|
|
|
if __name__ == '__main__':
|
|
|
|
sys_ret = 0
|
|
|
|
try:
|
|
|
|
sys_ret = main()
|
|
|
|
except:
|
|
|
|
traceback.print_exc()
|
|
|
|
sys_ret = 1
|
|
|
|
finally:
|
|
|
|
sys.exit(sys_ret)
|