From bde153cfdc7e1931fcf8a7ec05701d6403204dbc Mon Sep 17 00:00:00 2001 From: Adam Dodman Date: Mon, 17 Jul 2017 16:01:00 +0100 Subject: [PATCH] Add branch capability Add branches to figure out which build to fork. Useful for repos with seperate triggers needed for each branch. Note this means that branches other than master will not be considered by default from now on. --- main.py | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index e0aa9f3..e545564 100644 --- a/main.py +++ b/main.py @@ -29,6 +29,8 @@ auth_key = eyJEXAMPLE.AUTH.KEY # JSON Tree needed to resolve the value. #structure = object.sha +# Branch to use for deciding which drone build to fork and trigger (Default: master) +#branch = master #[ExampleGitHubRelease] #drone_repo = Example/HelloRelease @@ -37,11 +39,19 @@ auth_key = eyJEXAMPLE.AUTH.KEY ''' -def runbuild(repo: str): +def runbuild(repo: str, branch: str): url = drone_host + '/api/repos/' + repo - latest = jload(requests.get(url + '/builds/latest', - headers={'Authorization': auth}).text)['number'] - buildurl = url + '/builds/' + str(latest) + '?fork=true' + latest = jload(requests.get(url + '/builds/latest', headers={'Authorization': auth}).text) + build_num = False + if (latest['branch'] != branch): + while not build_num: + latest = jload(requests.get(url + '/builds/' + str(latest['number'] - 1), headers={'Authorization': auth}).text) + if (latest['branch'] == branch): + build_num = str(latest['number']) + else: + build_num = str(latest['number']) + + buildurl = url + '/builds/' + build_num + '?fork=true' # [[TODO]] Add functionality for checking if build was triggered successfully? return (requests.post(buildurl, headers={'Authorization': drone_auth_key})) @@ -124,9 +134,12 @@ if __name__ == '__main__': r.raise_for_status() new_value = jsonVal(r.text, config[service].get('structure')) if (new_value != config[service]['current_value']): - print(date.strftime("%d/%m/%Y %H:%M:%S") + - ': Got new build - ' + new_value) - runbuild(config[service].get('drone_repo')) + print(date.strftime("%d/%m/%Y %H:%M:%S") + ': Got new build - ' + new_value) + if not (config[service].get('branch', False)): + branch = 'master' + else: + branch = config[service]['branch'] + runbuild(config[service].get('drone_repo'), branch) config[service]['current_value'] = new_value with open(filepath + '/dronetrigger.cfg', 'w') as configfile: config.write(configfile)