Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- # IMPORTS
- import json
- import os
- from restful_lib import Connection
- # DECLARES
- username = 'rest'
- password = 'rest'
- project_name = os.getenv('bamboo_planKey', '').split('-')[0]
- short_plan_name = os.getenv('bamboo_shortPlanName', None)
- plan_repository_branch = os.getenv('bamboo_planRepository_branchName', None)
- base_url = 'http://www.example.com:8080/rest/api/latest'
- if project_name == '' or short_plan_name == None:
- raise ValueError('There appears to be a problem with the Environment Variables.')
- if plan_repository_branch == 'dev':
- exit()
- rest_path = 'plan/{0}-GATHER/branch/{1}'.format(project_name,short_plan_name)
- headers = {'content-type' : 'application/json', 'accept' : 'application/json'}
- args = {'os_authType' : 'basic'}
- # SUBS
- def parse_response(resp):
- status = int(resp[u'headers']['status'])
- message = None
- if u'body' in resp:
- try:
- resp_json = json.loads(resp[u'body'])
- print resp_json
- if u'message' in resp_json:
- message = resp_json[u'message']
- except ValueError:
- message = resp[u'body']
- return status,message
- # DO STUFF
- conn = Connection(base_url,username,password)
- resp = conn.request_get(rest_path, headers=headers, args=args)
- status,message = parse_response(resp)
- # If status is 204, it means NO CONTENT, as in, that Plan Branch doesn't exist yet
- # So we create it now
- if status == 204:
- args['enabled'] = 'true'
- resp = conn.request_put(rest_path, headers=headers, args=args)
- status,message = parse_response(resp)
- # If at this point status is anything but 200, there was an error, fail the build.
- if status != 200:
- raise Exception(status, message)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement