1
0
mirror of https://github.com/Adam-Ant/drone-trigger synced 2024-11-05 05:36:23 +00:00

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.
This commit is contained in:
Adam Dodman 2017-07-17 16:01:00 +01:00
parent 5e7296e76d
commit bde153cfdc

27
main.py
View File

@ -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)