projects:stashpr_jenkins_trigger
no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
— | projects:stashpr_jenkins_trigger [2015/12/22 23:38] (current) – created mkucia | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ====== Triggering Bitbucket Server Pull Request builds ====== | ||
+ | The following code can be used within Jenkins job triggered every few minutes. | ||
+ | |||
+ | It will get a list of currently open pull requests. Then get build data for every last commit in pull request. If the commit is not build then trigger Jenkins job. | ||
+ | |||
+ | <code python> | ||
+ | # -*- coding: utf-8 -*- | ||
+ | import requests, re, os, json, datetime, time | ||
+ | from requests.auth import HTTPBasicAuth | ||
+ | JENKINS_URL | ||
+ | STASH_URL | ||
+ | JOB_NAME | ||
+ | JENKINS_TRIG | ||
+ | CREDENTIALS | ||
+ | STASH_PRLIST | ||
+ | STASH_PRLIST += r"? | ||
+ | STASH_BUILDS | ||
+ | JOB_TOKEN | ||
+ | userAuth | ||
+ | pull_requests = [] | ||
+ | trigger | ||
+ | |||
+ | print(' | ||
+ | STASH_PRLIST, | ||
+ | datetime.datetime.utcnow().isoformat(), | ||
+ | ' | ||
+ | |||
+ | def getPullRequestsList(): | ||
+ | isLastPage | ||
+ | nextPageStart = 0 | ||
+ | while not isLastPage: | ||
+ | spage = '' | ||
+ | r = requests.get(STASH_PRLIST + spage, auth=userAuth, | ||
+ | if r.status_code is not requests.codes.ok: | ||
+ | raise Exception(' | ||
+ | [r.status_code, | ||
+ | data = json.loads(r.text) | ||
+ | isLastPage | ||
+ | nextPageStart = data.get(' | ||
+ | pull_requests.extend(data[' | ||
+ | return pull_requests | ||
+ | |||
+ | def checkAndTriggerIfRequired(): | ||
+ | for PR in getPullRequestsList(): | ||
+ | PR_commit = PR[' | ||
+ | PR_title | ||
+ | PR_id = PR[' | ||
+ | r = requests.get(STASH_BUILDS + PR_commit, auth=userAuth, | ||
+ | PR_data | ||
+ | PR_val | ||
+ | PR_lastState = ' | ||
+ | PR_doTrigger = ' | ||
+ | |||
+ | print(' | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | if PR_doTrigger is ' | ||
+ | |||
+ | if len(trigger) is 0: | ||
+ | print(' | ||
+ | return 0 | ||
+ | |||
+ | print() | ||
+ | for trig in trigger: | ||
+ | URL = JENKINS_TRIG+'? | ||
+ | print(' | ||
+ | r = requests.put(URL, | ||
+ | if r.status_code is not requests.codes.ok: | ||
+ | raise Exception(' | ||
+ | return 0 | ||
+ | |||
+ | exitcode = checkAndTriggerIfRequired() | ||
+ | print(' | ||
+ | print(' | ||
+ | time.sleep(10) | ||
+ | exit(exitcode) | ||
+ | |||
+ | </ |
projects/stashpr_jenkins_trigger.txt · Last modified: 2015/12/22 23:38 by mkucia