diff --git a/README.md b/README.md index 343a631..7047e73 100644 --- a/README.md +++ b/README.md @@ -244,3 +244,75 @@ Add a task and look at the POST parameters. You don't have to specify all of the myproj_plan_keys = filter(lambda s: re.match(project,s) != None, plan_keys) print 'PLAN KEYS IN PROJECT %s = %s' % (project, myproj_plan_keys) + + +####################################################### +#### Create Connection Either use username and password + +logging.basicConfig(level=logging.DEBUG) +host='http://localhost:6990' +user_name='admin' +user_pwd='admin' +conn = authenticate(host, user_name, user_pwd) + + + +#### enable/disable plans and jobs +plan_id = 'SWRD-CM' +disable_plan(conn, plan_id) +enable_plan(conn, plan_id) + +job_id = 'SWRD-CM-JOB2' +disable_job(conn, job_id) +enable_job(conn, job_id) + + +#### add a new job to each plan +plan_id = 'SWRD-CM' +job_name = 'job6' +job_description = 'job6_description' +res = add_one_job(conn, plan_id, job_name, job_description) + + +#### Modify environment variables for each job +job_id = 'SWRD-CM-JOB4' +task_id = 2 +environment_variables = "JAVA_OPTS=\"-Xmx256m -Xms128m\"" +res = update_environment_variables(conn, job_id, task_id, environment_variables) + + + +#### Add a repository to each job +job_id = 'SWRD-CM-JOB01' +task_id = 1 +repository_id = "2343563" +checkout_dir = "community/poky12" +res = add_repository(conn, job_id, task_id, repository_id, checkout_dir) + + + +#### Modify permissions on each job +plan_id = 'SWRD-CM' +#usertype, username, permissiontype, value +change_plan_permission(conn, plan_id, ('user', 'username', 'clone', False)) + + +#### Add a task about "script" type +job_id = "DNSWRD-CIM-JOB5" +task_description = "1234" +is_task_disabled = False +is_run_with_power_shell = True +script_location = "INLINE" +script = "pwd" +argument = "" +environment_variables = "" +working_sub_directory = "" +add_job_task_script(conn, job_id, is_task_disabled, is_run_with_power_shell, task_description, script_location, script, + argument, environment_variables, working_sub_directory) + + +#### Add agents to each job +job_id="DNSWRD-CIM-JOB11" +req_key="agent.name" +req_value="agent" +res=add_job_requirement(conn, job_id, req_key, req_value) \ No newline at end of file diff --git a/lib/bamboo/jobs.py b/lib/bamboo/jobs.py index 7395a79..f0dd6da 100644 --- a/lib/bamboo/jobs.py +++ b/lib/bamboo/jobs.py @@ -3,63 +3,121 @@ from collections import OrderedDict + def get_jobs(conn, plan_id, sort_by_title=False): - params = { - "buildKey": plan_id - } - res = requests.get_ui_return_html( - conn, - conn.baseurl+'/chain/admin/config/editChainDetails.action', - params) - - root = res #.getroot() - - jobs = OrderedDict() - - li_jobkeys = root.findall('.//li[@data-job-key]') - for li in li_jobkeys: - key = li.attrib['data-job-key'] - edit_link = li.find('.//a').attrib['href'] - del_link = None - title = li.find('.//a').text - description = None - try: - description = li.attrib['title'] - except: - pass - - if sort_by_title: - jobs[title] = (key, description, edit_link, del_link,) - else: - jobs[key] = (title, description, edit_link, del_link,) - - return jobs - -def disable_job(conn, job_id, job_title): - params = { - "buildKey": job_id, - "buildName": job_title, - "checkBoxFields": "enabled", - "save": "Save" - } - res = requests.get_ui_return_html( - conn, - conn.baseurl+'/build/admin/edit/updateBuildDetails.action', - params) - - return res - -def enable_job(conn, job_id, job_title): - params = { - "buildKey": job_id, - "buildName": job_title, - "checkBoxFields": "enabled", - "enabled": "true", - "save": "Save" - } - res = requests.get_ui_return_html( - conn, - conn.baseurl+'/build/admin/edit/updateBuildDetails.action', - params) - - return res; + params = { + "buildKey": plan_id + } + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/chain/admin/config/editChainDetails.action', + params) + + root = res # .getroot() + + jobs = OrderedDict() + + li_jobkeys = root.findall('.//li[@data-job-key]') + for li in li_jobkeys: + key = li.attrib['data-job-key'] + edit_link = li.find('.//a').attrib['href'] + del_link = None + title = li.find('.//a').text + description = None + try: + description = li.attrib['title'] + except: + pass + + if sort_by_title: + jobs[title] = (key, description, edit_link, del_link,) + else: + jobs[key] = (title, description, edit_link, del_link,) + + return jobs + + +def get_job_params(conn, job_id): + params = { + "buildKey": job_id + } + + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/build/admin/edit/editBuildDetails.action', + params) + html_root = res + job_params = OrderedDict() + form = html_root.find('.//form[@id="updateBuildDetails"]') + li_inputs = form.findall('.//input') + for input in li_inputs: + name = input.attrib.get('name') + type = input.attrib.get('type') + if type == "checkbox": + is_checked = input.attrib.get('checked') + if is_checked and is_checked == "checked": + value = "true" + else: + value = "false" + else: + value = input.attrib.get('value') + job_params[name] = value + + return job_params + + +def disable_job(conn, job_id): + job_params = get_job_params(conn, job_id) + params = { + "buildKey": job_params.get('buildKey'), + "buildName": job_params.get('buildName'), + 'buildDescription': job_params.get('buildDescription'), + "checkBoxFields": "enabled", + "save": "Save" + } + + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/build/admin/edit/updateBuildDetails.action', + params) + + return res + + +def enable_job(conn, job_id): + job_params = get_job_params(conn, job_id) + params = { + "buildKey": job_params.get('buildKey'), + "buildName": job_params.get('buildName'), + 'buildDescription': job_params.get('buildDescription'), + "checkBoxFields": "enabled", + "enabled": "true", + "save": "Save" + } + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/build/admin/edit/updateBuildDetails.action', + params) + + return res + + +def add_one_job(conn, plan_id, job_name, job_description, is_enabled=True, job_key=None): + params = { + "bamboo.successReturnMode": "json", + "buildKey": plan_id, + "buildName": job_name, + "subBuildKey": job_key.upper() if job_key else job_name.upper(), + "buildDescription": job_description, + "existingStage": "Default Stage", + "checkBoxFields": "tmp.createAsEnabled", + "tmp.createAsEnabled": "true" if is_enabled else "false", + "save": "Create job" + } + + res = requests.post_ui_return_json( + conn, + conn.baseurl + '/chain/admin/createJob.action', + params) + + return res \ No newline at end of file diff --git a/lib/bamboo/permissions.py b/lib/bamboo/permissions.py index 51aa589..d1503b2 100644 --- a/lib/bamboo/permissions.py +++ b/lib/bamboo/permissions.py @@ -8,9 +8,9 @@ def _check_permission(html_root, usertype, username, permission): username = 'ROLE_USER' elif username == 'Anonymous Users': username = 'ROLE_ANONYMOUS' - permission_input_field_name = 'bambooPermission_'+usertype+'_'+username+'_'+permission.upper() - permission_cell_name = permission_input_field_name+'_cell' - permission_xpath = './/td[@id="'+permission_cell_name+'"]/input[@name="'+permission_input_field_name+'"]' + permission_input_field_name = 'bambooPermission_'+usertype+'_'+username+'_'+permission.upper()#bambooPermission_user_B19537_READ + #permission_cell_name = permission_input_field_name+'_cell' + permission_xpath = './/input[@name="'+permission_input_field_name+'"]' logging.debug('xpath to search for permission checkbox = %s' % permission_xpath) el = html_root.find(permission_xpath) if el == None: @@ -69,16 +69,19 @@ def get_plan_permissions(conn, plan_id): def mod_plan_permissions(conn, plan_id, permission_params): + params = { + "bamboo.successReturnMode": "json", "buildKey": plan_id, "newGroup": None, "newUser": None, "principalType": "User", "save": "Save", - "selectFields": "principalType" + "selectFields": "principalType", + } params.update(permission_params) - res = requests.post_ui_return_html( + res = requests.post_ui_return_json( conn, conn.baseurl+'/chain/admin/config/updateChainPermissions.action', params) diff --git a/lib/bamboo/plans.py b/lib/bamboo/plans.py index ff4f872..30058ba 100644 --- a/lib/bamboo/plans.py +++ b/lib/bamboo/plans.py @@ -1,44 +1,117 @@ import logging from .. import requests +from collections import OrderedDict + + def _iterate_json_entity_results(request, conn, entity, path, params): - start_index = 0 - params.update({ - "start-index": start_index - }) - entities = entity+'s' - res = request(conn, path, params) - logging.debug('%s', res[entities]['max-result']) - part_result_size = res[entities]['max-result'] - result_size = res[entities]['size'] - part_res = res - while start_index <= result_size: - logging.debug('size = %s max-result = %s', res[entities]['size'], res[entities]['max-result']) - logging.debug('start_index = %s', start_index) - start_index = start_index + part_result_size + start_index = 0 params.update({ - "start-index": start_index - }) - part_res = request(conn, path, params) - res[entities][entity].extend(part_res[entities][entity]) + "start-index": start_index + }) + entities = entity + 's' + res = request(conn, path, params) + logging.debug('%s', res[entities]['max-result']) + part_result_size = res[entities]['max-result'] + result_size = res[entities]['size'] + part_res = res + while start_index <= result_size: + logging.debug('size = %s max-result = %s', res[entities]['size'], res[entities]['max-result']) + logging.debug('start_index = %s', start_index) + start_index = start_index + part_result_size + params.update({ + "start-index": start_index + }) + part_res = request(conn, path, params) + res[entities][entity].extend(part_res[entities][entity]) + + return res - return res def _get_entity(conn, entity, expand): - params = { - "expand": expand - } - res = _iterate_json_entity_results( - requests.get_rest_return_json, - conn, - entity, - conn.baseurl+'/rest/api/latest/'+entity, - params) - - return res + params = { + "expand": expand + } + res = _iterate_json_entity_results( + requests.get_rest_return_json, + conn, + entity, + conn.baseurl + '/rest/api/latest/' + entity, + params) + + return res + def get_plans(conn, expand=''): - return _get_entity(conn, 'plan', expand) + return _get_entity(conn, 'plan', expand) + def get_projects(conn, expand=''): - return _get_entity(conn, 'project', expand) + return _get_entity(conn, 'project', expand) + + +def get_plan_params(conn, plan_id): + params = { + "buildKey": plan_id + } + + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/chain/admin/config/editChainDetails.action', + params) + html_root = res + plan_params = OrderedDict() + form = html_root.find('.//form[@id="saveChainDetails"]') + li_inputs = form.findall('.//input') + for input in li_inputs: + name = input.attrib.get('name') + type = input.attrib.get('type') + if type == "checkbox": + is_checked = input.attrib.get('checked') + if is_checked and is_checked == "checked": + value = "true" + else: + value = "false" + else: + value = input.attrib.get('value') + plan_params[name] = value + + return plan_params + + +def disable_plan(conn, plan_id): + plan_params = get_plan_params(conn, plan_id) + params = { + "buildKey": plan_params.get('buildKey'), + "projectName": plan_params.get('projectName'), + 'chainName': plan_params.get('chainName'), + 'chainDescription': plan_params.get('chainDescription'), + "checkBoxFields": "enabled", + "save": "Save" + } + + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/chain/admin/config/saveChainDetails.action', + params) + + return res + + +def enable_plan(conn, plan_id): + plan_params = get_plan_params(conn, plan_id) + params = { + "buildKey": plan_params.get('buildKey'), + "projectName": plan_params.get('projectName'), + 'chainName': plan_params.get('chainName'), + 'chainDescription': plan_params.get('chainDescription'), + "checkBoxFields": "enabled", + "enabled": "true", + "save": "Save" + } + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/chain/admin/config/saveChainDetails.action', + params) + + return res \ No newline at end of file diff --git a/lib/bamboo/requirements.py b/lib/bamboo/requirements.py index 87d072f..bd628f4 100644 --- a/lib/bamboo/requirements.py +++ b/lib/bamboo/requirements.py @@ -2,83 +2,103 @@ from .. import requests import re + def _get_requirements(conn, job_id): - params = { - "buildKey": job_id - } - res = requests.get_ui_return_html( - conn, - conn.baseurl+'/build/admin/edit/defaultBuildRequirement.action', - params) - - root = res #.getroot() - - requirements = {} - - td_labels = root.find_class('labelCell') - for td in td_labels: - key = None - req_id = None - edit_link = None - del_link = None - tr = td.getparent() - links = tr.findall('.//a') - for l in links: - href = l.attrib['href'] - match = re.search('capabilityKey=(.*)', href) - if match: - key = match.group(1) - match = re.search('editBuildRequirement.*requirementId=(\d+)', href) - if match: - edit_link = href - req_id = match.group(1) - match = re.search('deleteBuildRequirement.*requirementId=(\d+)', href) - if match: - del_link = href - req_id = match.group(1) - - if not key: - key = td.text.strip() - - requirements[key] = (req_id, edit_link, del_link,) - - return requirements + params = { + "buildKey": job_id + } + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/build/admin/edit/defaultBuildRequirement.action', + params) + + root = res # .getroot() + + requirements = {} + + td_labels = root.find_class('labelCell') + for td in td_labels: + key = None + req_id = None + edit_link = None + del_link = None + tr = td.getparent() + links = tr.findall('.//a') + for l in links: + href = l.attrib['href'] + match = re.search('capabilityKey=(.*)', href) + if match: + key = match.group(1) + match = re.search('editBuildRequirement.*requirementId=(\d+)', href) + if match: + edit_link = href + req_id = match.group(1) + match = re.search('deleteBuildRequirement.*requirementId=(\d+)', href) + if match: + del_link = href + req_id = match.group(1) + + if not key: + key = td.text.strip() + + requirements[key] = (req_id, edit_link, del_link,) + + return requirements + def delete_job_requirement(conn, job_id, req_key): - requirements = _get_requirements(conn, job_id) - logging.debug('%s', requirements) - res = None - req_id, _, del_link = requirements[req_key] - if req_id != None: - res = requests.post_ui_no_return(conn, del_link, {}) + requirements = _get_requirements(conn, job_id) + logging.debug('%s', requirements) + res = None + req_id, _, del_link = requirements[req_key] + if req_id != None: + res = requests.post_ui_no_return(conn, del_link, {}) + + return res - return res def delete_job_all_requirements(conn, job_id): - requirements = _get_requirements(conn, job_id) - res = None - for req_id, _, del_link in requirements.itervalues(): - if req_id != None: - res = requests.post_ui_no_return(conn, del_link, {}) - - return res - -def add_job_requirement(conn, job_id, req_key, req_value, req_exists=False): - params = { - "Add": "Add", - "buildKey": job_id, - "existingRequirement": req_key if req_exists else None, - "regexMatchValue": None, - "requirementKey": None if req_exists else req_key, - "requirementMatchType": "equal", - "requirementMatchValue": req_value, - "selectFields": "existingRequirement", - "selectFields": "requirementMatchType" - } - logging.debug(params) - res = requests.post_ui_return_html( - conn, - conn.baseurl+'/build/admin/edit/addBuildRequirement.action', - params) - - return res + requirements = _get_requirements(conn, job_id) + res = None + for req_id, _, del_link in requirements.itervalues(): + if req_id != None: + res = requests.post_ui_no_return(conn, del_link, {}) + + return res + + +def get_job_requirement(conn, job_id): + requirements = {} + res = requests.get_ui_return_json( + conn, + conn.baseurl + '/rest/api/latest/config/job/' + job_id + '/requirement', None) + if not res: + return requirements + for re in res: + key = re.get('key') + if key: + requirements[key] = { + 'key': key, + 'matchType': re.get('matchType'), + 'matchValue': re.get('matchValue') + } + return requirements + + +def add_job_requirement(conn, job_id, req_key, req_value): + requirements = get_job_requirement(conn, job_id) + if requirements and req_key in requirements.keys(): + return None + params = { + "key": req_key, + "matchType": "EQUALS", + "matchValue": req_value + } + logging.debug(params) + res = requests.post_ui_return_json( + conn, + conn.baseurl + '/rest/api/latest/config/job/' + job_id + '/requirement', + params, content_type='application/json') + + return res + diff --git a/lib/bamboo/tasks.py b/lib/bamboo/tasks.py index 6fc233f..a804453 100644 --- a/lib/bamboo/tasks.py +++ b/lib/bamboo/tasks.py @@ -4,110 +4,249 @@ from collections import OrderedDict + def get_tasks(conn, job_id, sort_by_title=False): - params = { - "buildKey": job_id - } - res = requests.get_ui_return_html( - conn, - conn.baseurl+'/build/admin/edit/editBuildTasks.action', - params) - - root = res #.getroot() - - tasks = OrderedDict() - - li_items = root.find_class('item') - for order_id, li in enumerate(li_items, start=1): - key = int(li.attrib['data-item-id']) - edit_link = None - del_link = None - title = li.find('.//h3').text - description = None - try: - description = li.find('.//div').text - except: - pass - links = li.findall('.//a') - for l in links: - href = l.attrib['href'] - match = re.search('editTask', href) - if match: - edit_link = href - match = re.search('confirmDeleteTask', href) - if match: - del_link = href - req_id = href - - if sort_by_title: - title_desc = (title, description) - tasks[title_desc] = (key, (title, description), edit_link, del_link, order_id,) - else: - tasks[key] = (title, description, edit_link, del_link, order_id,) - - return tasks - -def add_job_task(conn, job_id, task_id, task_params): - params = { - "bamboo.successReturnMode": "json", - "planKey": job_id, - "checkBoxFields": "taskDisabled", - "confirm": "true", - "createTaskKey": task_id, - "decorator": "nothing", - "taskId": 0, - "finalising": "true", - "userDescription": None - } - params.update(task_params) - res = requests.post_ui_return_json( - conn, - conn.baseurl+'/build/admin/edit/createTask.action', - params) - - return res + params = { + "buildKey": job_id + } + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/build/admin/edit/editBuildTasks.action', + params) + + root = res # .getroot() + + tasks = OrderedDict() + + li_items = root.find_class('item') + for order_id, li in enumerate(li_items, start=1): + key = int(li.attrib['data-item-id']) + edit_link = None + del_link = None + title = li.find('.//h3').text + description = None + try: + description = li.find('.//div').text + except: + pass + links = li.findall('.//a') + for l in links: + href = l.attrib['href'] + match = re.search('editTask', href) + if match: + edit_link = href + match = re.search('confirmDeleteTask', href) + if match: + del_link = href + req_id = href + + if sort_by_title: + title_desc = (title, description) + tasks[title_desc] = { + 'task_id': key, + 'title': title, + 'description': description, + 'edit_link': edit_link, + 'del_link': del_link, + 'order_id': order_id + } + + else: + tasks[key] = { + 'task_id': key, + 'title': title, + 'description': description, + 'edit_link': edit_link, + 'del_link': del_link, + 'order_id': order_id + } + + return tasks + + +def get_task_params(conn, job_id, task_id): + # '/build/admin/edit/editTask.action?planKey=DNSWRD-CIM-JOB0&taskId=1' + params = { + "planKey": job_id, + "taskId": task_id + } + + res = requests.get_ui_return_html( + conn, + conn.baseurl + '/build/admin/edit/editTask.action', + params) + + html_root = res + task_params = OrderedDict() + form = html_root.find('.//form[@id="updateTask"]') + li_inputs = form.findall('.//input') + selects = form.findall('.//select') + for input in li_inputs: + name = input.attrib.get('name') + type = input.attrib.get('type') + if type == "checkbox": + is_checked = input.attrib.get('checked') + if is_checked and is_checked == "checked": + value = "true" + else: + value = "false" + else: + value = input.attrib.get('value') + + task_params[name] = value + + for select in selects: + name = select.attrib.get('name') + value = select.value + if not value: + value = select.value_options[0] + task_params[name] = value + + return task_params + + +def update_task(conn, job_id, task_id, task_params): + params = { + "bamboo.successReturnMode": "json", + "planKey": job_id, + "checkBoxFields": "taskDisabled", + "confirm": "true", + "createTaskKey": task_id, + "decorator": "nothing", + "taskId": 0, + "finalising": "true", + "userDescription": None + + } + + params.update(task_params) + + res = requests.post_ui_return_json( + conn, + conn.baseurl + '/build/admin/edit/updateTask.action', + params) + + return res + + +def add_job_task(conn, job_id, task_params): + params = { + "bamboo.successReturnMode": "json", + "planKey": job_id, + "checkBoxFields": "taskDisabled", + "confirm": "true", + "createTaskKey": "", + "decorator": "nothing", + "taskId": 0, + "finalising": "true", + "userDescription": None + } + params.update(task_params) + res = requests.post_ui_return_json( + conn, + conn.baseurl + '/build/admin/edit/createTask.action', + params) + + return res + def delete_job_task(conn, job_id, task_id): - params = { - "bamboo.successReturnMode": "json", - "planKey": job_id, - "confirm": "true", - "createTaskKey": None, - "decorator": "nothing", - "taskId": task_id - } - res = requests.post_ui_return_json( - conn, - conn.baseurl+'/build/admin/edit/deleteTask.action', - params) - - return res + params = { + "bamboo.successReturnMode": "json", + "planKey": job_id, + "confirm": "true", + "createTaskKey": None, + "decorator": "nothing", + "taskId": task_id + } + res = requests.post_ui_return_json( + conn, + conn.baseurl + '/build/admin/edit/deleteTask.action', + params) + + return res + def move_job_task(conn, job_id, task_id, finalising=False, beforeId=None, afterId=None): - """ Move a task in the runtime order. - - Arguments: - conn -- the connection object - job_id -- the id of the job - task_id -- the id of the task to move - finalising -- true, if task should be a final task - beforeId -- id of the task which should be before this task - afterId -- id of the task which should be after this taks - - """ - params = { - "planKey": job_id, - "finalising": "true" if finalising else "false", - "taskId": task_id - } - if beforeId: - params.update({"beforeId": beforeId}) - if afterId: - params.update({"afterId": afterId}) - res = requests.post_ui_return_json( - conn, - conn.baseurl+'/build/admin/ajax/moveTask.action', - params) - logging.debug(params) - - return res + """ Move a task in the runtime order. + + Arguments: + conn -- the connection object + job_id -- the id of the job + task_id -- the id of the task to move + finalising -- true, if task should be a final task + beforeId -- id of the task which should be before this task + afterId -- id of the task which should be after this taks + + """ + params = { + "planKey": job_id, + "finalising": "true" if finalising else "false", + "taskId": task_id + } + if beforeId: + params.update({"beforeId": beforeId}) + if afterId: + params.update({"afterId": afterId}) + res = requests.post_ui_return_json( + conn, + conn.baseurl + '/build/admin/ajax/moveTask.action', + params) + logging.debug(params) + + return res + + +def update_environment_variables(conn, job_id, task_id, environment_variables): + task_params = get_task_params(conn, job_id, task_id) + if task_params.get('environmentVariables') is None: + return + task_params['environmentVariables'] = environment_variables + + return update_task(conn, job_id, task_id, task_params) + + +def add_repository(conn, job_id, task_id, repository_id, checkout_dir): + task_params = get_task_params(conn, job_id, task_id) + checkout_dirs = OrderedDict() + for param_name, param_value in task_params.items(): + res = re.findall('checkoutDir_(\d)', param_name) + if res: + checkout_dirs[int(res[0])] = param_value + if len(checkout_dirs) == 0 or checkout_dir in checkout_dirs.values(): + return + + selcted_index = max(checkout_dirs.keys()) + 1 + task_params['selectedRepository_%s' % selcted_index] = repository_id + task_params['checkoutDir_%s' % selcted_index] = checkout_dir + + return update_task(conn, job_id, task_id, task_params) + + +def add_job_task_script(conn, job_id, is_task_disabled=False, is_run_with_power_shell=False, task_description="", + script_location="INLINE", script="pwd", argument="", environment_variables="", + working_sub_directory=""): + script_params = { + "userDescription": task_description, + "checkBoxFields": "taskDisabled", + "scriptLocation": script_location, + "selectFields": "scriptLocation", + "checkBoxFields": "runWithPowershell", + "script": script if script_location == "FILE" else "", + "scriptBody": script if script_location == "INLINE" else "", + "argument": argument, + "environmentVariables": environment_variables, + "workingSubDirectory": working_sub_directory, + "createTaskKey": "com.atlassian.bamboo.plugins.scripttask:task.builder.script", + "taskId": "0", + "planKey": job_id, + "bamboo.successReturnMode": "json", + "decorator": "nothing", + "confirm": "true" + } + if is_task_disabled: + script_params['taskDisabled'] = 'true' + if is_run_with_power_shell: + script_params['runWithPowershell'] = 'true' + + return add_job_task(conn, job_id, script_params) diff --git a/lib/bamboo_automate.py b/lib/bamboo_automate.py index bacbce2..95ce524 100644 --- a/lib/bamboo_automate.py +++ b/lib/bamboo_automate.py @@ -1,16 +1,156 @@ -from high_level_functions import * -from manipulate_bamboo_json import * -from prettyprint_functions import * - -from bamboo.agents import * -from bamboo.authenticate import * -from bamboo.branches import * -from bamboo.jobs import * -from bamboo.permissions import * -from bamboo.plans import * -from bamboo.requirements import * -from bamboo.results import * -from bamboo.tasks import * -from bamboo.variables import * +from lib.high_level_functions import * +from lib.bamboo.authenticate import * +from lib.bamboo.variables import * + + +createTaskKey = ["com.atlassian.bamboo.plugins.ant:task.builder.ant", + "com.atlassian.bamboo.plugins.bamboo-artifact-downloader-plugin:artifactdownloadertask", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.bower", + "com.atlassian.bamboo.plugins.scripttask:task.builder.command", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:deployAppTask", + "com.atlassian.bamboo.plugins.bamboo-variable-inject-plugin:dump", + "net.gejza.bamboo.plugins.bamboo-gnutools-tasks:task.gnutools.make", + "com.atlassian.bamboo.plugins.bamboo-grails-plugin:grailsBuilderTaskType", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.grunt", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.gulp", + "com.heroku.bamboo.heroku-bamboo-plugin:com.heroku.bamboo.WarDeploymentTask", + "com.atlassian.bamboo.plugins.bamboo-variable-inject-plugin:inject", + "com.atlassian.bamboo.plugins.testresultparser:task.testresultparser.junit", + "com.atlassian.bamboo.plugins.maven:task.builder.maven", + "com.atlassian.bamboo.plugins.maven:task.builder.mvn2", + "com.atlassian.bamboo.plugins.maven:task.builder.mvn3", + "com.atlassian.bamboo.plugins.maven:task.mvn.dependencies.processor", + "com.atlassian.bamboo.plugin.dotnet:mbunit", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.reporter.mocha", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.mocha", + "com.atlassian.bamboo.plugin.dotnet:msbuild", + "com.atlassian.bamboo.plugin.dotnet:mstest", + "com.atlassian.bamboo.plugin.dotnet:mstestRunner", + "com.atlassian.bamboo.plugin.dotnet:nant", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.node", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.nodeunit", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.npm", + "com.atlassian.bamboo.plugin.dotnet:nunit", + "com.atlassian.bamboo.plugin.dotnet:nunitRunner", + "com.atlassian.bamboo.plugins.php:task.builder.phpunit", + "com.atlassian.bamboo.plugins.php:task.builder.phpunit33X", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:reloadAppTask", + "com.atlassian.bamboo.plugins.bamboo-scp-plugin:scptask", + "com.atlassian.bamboo.plugins.scripttask:task.builder.script", + "com.atlassian.bamboo.plugins.vcs:task.vcs.checkout", + "com.atlassian.bamboo.plugins.bamboo-scp-plugin:sshtask", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:startAppTask", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:stopAppTask", + "com.atlassian.bamboo.plugins.testresultparser:task.testresultparser.testng", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:undeployAppTask", + "com.atlassian.bamboo.plugins.vcs:task.vcs.branching", + "com.atlassian.bamboo.plugins.vcs:task.vcs.tagging", + "com.atlassian.bamboo.plugin.dotnet:devenv", + "com.atlassian.bamboo.plugins.ant:task.builder.ant", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.bower", + "com.atlassian.bamboo.plugins.scripttask:task.builder.command", + "com.atlassian.bamboo.plugins.bamboo-grails-plugin:grailsBuilderTaskType", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.grunt", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.gulp", + "com.atlassian.bamboo.plugins.maven:task.builder.maven", + "com.atlassian.bamboo.plugins.maven:task.builder.mvn2", + "com.atlassian.bamboo.plugins.maven:task.builder.mvn3", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.mocha", + "com.atlassian.bamboo.plugin.dotnet:msbuild", + "com.atlassian.bamboo.plugin.dotnet:nant", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.node", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.nodeunit", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.npm", + "com.atlassian.bamboo.plugins.scripttask:task.builder.script", + "com.atlassian.bamboo.plugin.dotnet:devenv", + "com.atlassian.bamboo.plugins.testresultparser:task.testresultparser.junit", + "com.atlassian.bamboo.plugin.dotnet:mbunit", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.reporter.mocha", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.mocha", + "com.atlassian.bamboo.plugin.dotnet:mstest", + "com.atlassian.bamboo.plugin.dotnet:mstestRunner", + "com.atlassian.bamboo.plugins.bamboo-nodejs-plugin:task.builder.nodeunit", + "com.atlassian.bamboo.plugin.dotnet:nunit", + "com.atlassian.bamboo.plugin.dotnet:nunitRunner", + "com.atlassian.bamboo.plugins.php:task.builder.phpunit", + "com.atlassian.bamboo.plugins.php:task.builder.phpunit33X", + "com.atlassian.bamboo.plugins.testresultparser:task.testresultparser.testng", + "com.atlassian.bamboo.plugins.bamboo-artifact-downloader-plugin:artifactdownloadertask", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:deployAppTask", + "com.heroku.bamboo.heroku-bamboo-plugin:com.heroku.bamboo.WarDeploymentTask", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:reloadAppTask", + "com.atlassian.bamboo.plugins.bamboo-scp-plugin:scptask", + "com.atlassian.bamboo.plugins.bamboo-scp-plugin:sshtask", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:startAppTask", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:stopAppTask", + "com.atlassian.bamboo.plugins.tomcat.bamboo-tomcat-plugin:undeployAppTask", + "com.atlassian.bamboo.plugins.vcs:task.vcs.checkout", + "com.atlassian.bamboo.plugins.vcs:task.vcs.branching", + "com.atlassian.bamboo.plugins.vcs:task.vcs.tagging", + "com.atlassian.bamboo.plugins.bamboo-variable-inject-plugin:dump", + "com.atlassian.bamboo.plugins.bamboo-variable-inject-plugin:inject" +] logging.basicConfig(level=logging.DEBUG) +host='http://localhost:6990' +user_name='admin' +user_pwd='admin' +conn = authenticate(host, user_name, user_pwd) + +###enable/disable plans and jobs +plan_id = 'SWRD-CM' +disable_plan(conn, plan_id) +enable_plan(conn, plan_id) + +job_id = 'SWRD-CM-JOB2' +disable_job(conn, job_id) +enable_job(conn, job_id) + +##add a new job to each plan +plan_id = 'SWRD-CM' +job_name = 'job6' +job_description = 'job6_description' +res = add_one_job(conn, plan_id, job_name, job_description) + + +####Modify environment variables for each job +job_id = 'SWRD-CM-JOB4' +task_id = 2 +environment_variables = "JAVA_OPTS=\"-Xmx256m -Xms128m\"" +res = update_environment_variables(conn, job_id, task_id, environment_variables) + + + +####### Add a repository to each job +job_id = 'SWRD-CM-JOB01' +task_id = 1 +repository_id = "2343563" +checkout_dir = "community/poky12" +res = add_repository(conn, job_id, task_id, repository_id, checkout_dir) + + + +# modify permissions on each job +plan_id = 'SWRD-CM' +#usertype, username, permissiontype, value +change_plan_permission(conn, plan_id, ('user', 'username', 'clone', False)) + + +#Add a task about "script" type +job_id = "DNSWRD-CIM-JOB5" +task_description = "1234" +is_task_disabled = False +is_run_with_power_shell = True +script_location = "INLINE" +script = "pwd" +argument = "" +environment_variables = "" +working_sub_directory = "" +add_job_task_script(conn, job_id, is_task_disabled, is_run_with_power_shell, task_description, script_location, script, + argument, environment_variables, working_sub_directory) + +#add agents to each job +job_id="DNSWRD-CIM-JOB11" +req_key="agent.name" +req_value="agent" +res=add_job_requirement(conn, job_id, req_key, req_value) \ No newline at end of file diff --git a/lib/high_level_functions.py b/lib/high_level_functions.py index ccd8c00..dbedeb6 100644 --- a/lib/high_level_functions.py +++ b/lib/high_level_functions.py @@ -43,6 +43,8 @@ def change_plan_permission(conn, plan_key, permission): usertype, username, permissiontype, value = permission permissions = get_plan_permissions(conn, plan_key) + if username not in permissions[usertype].iterkeys(): + permissions[usertype][username]={'read': False, 'write': False, 'clone': False, 'admin': False, 'build': False} try: if permissiontype == 'all': for key in permissions[usertype][username].iterkeys(): diff --git a/lib/requests.py b/lib/requests.py index d8e90fa..ff95eef 100644 --- a/lib/requests.py +++ b/lib/requests.py @@ -43,8 +43,10 @@ def post_ui_return_html(conn, path, params): res, _, _ = _request(conn, "POST", path, params, [], urllib.urlencode, html.fromstring) return res -def post_ui_return_json(conn, path, params): +def post_ui_return_json(conn, path, params,content_type=None): headers = [('Accept', 'application/json')] + if content_type: + headers.append(('Content-Type', content_type)) try: res, _, _ = _request(conn, "POST", path, params, headers, urllib.urlencode, json.loads) except: @@ -108,13 +110,17 @@ def _request(conn, method, path, params, headers, param_parse_func, response_par if len(cookies) > 0: req.add_header('Cookie', cookies) + content_type=req.headers.get('Content-type') retries = 0 req_success = False while not req_success: logging.debug('%s', req.get_full_url()) try: if method == "POST": - response = conn.opener.open(req, param_parse_func(params)) + if content_type and content_type=='application/json': + response= conn.opener.open(req,json.dumps(params)) + else: + response = conn.opener.open(req, param_parse_func(params)) elif method == "GET": response = conn.opener.open(req) req_success = True