def delete_repo(repo): organisation, project, name = repo.split('/') repo_id = call( f'/{organisation}/{project}/_apis/git/repositories/{name}?api-version=3.2', method='GET', )['id'] return call( f'/{organisation}/{project}/_apis/git/repositories/{repo_id}?api-version=3.2', method='DELETE', only_response_code=True)
def create_repo(repo): organisation, project, name = repo.split('/') return call( f'/{organisation}/{project}/_apis/git/repositories?api-version=3.2', request_data={'name': name}, method='POST', )
def create_build_definition(organisation, project, build_definition): return call( # f'/{organisation}/{project}/_apis/build/definitions?api-version=3.2', f'/{organisation}/{project}/_apis/build/definitions?api-version=3.2', request_data=build_definition, method='POST', only_response_code=True)
def get_repo_branches(organisation, project, repository, ref_filter='refs/heads'): f = f'/{ref_filter}' if ref_filter else '' uri = f'/{organisation}/{project}/_apis/git/repositories' \ f'/{repository}{f}?includeStatuses=true&api-version=3.2' return call(uri)['value']
def get_projects(organisations, project_specs=False, names=False): if not organisations: return [] projects = [(o, p) for o in organisations for p in call(f'/{o}/_apis/projects?api-version=3.2')['value']] if names: return [project[1]['name'] for project in projects] if project_specs: return [f'{project[0]}/{project[1]["name"]}' for project in projects] return projects
def list_build_definitions(organisations=None, projects=None, filter_name=None): if organisations: projects = get_projects(organisations, project_specs=True) if not projects: return [] query = f'&name={filter_name}' if filter_name else '' build_definitions = {build_definition['name']: build_definition for project in projects for build_definition in call(f'/{project}/_apis/build/definitions?api-version=3.2{query}')['value']} return build_definitions
def list_release_definitions(organisations=None, projects=None, filter_name=None): if organisations: projects = get_projects(organisations, project_specs=True) if not projects: return [] release_definitions = {release_definition['name']: release_definition for project in projects for release_definition in call(f'/{project}/_apis/release/definitions')['value']} if not filter_name: return release_definitions else: return {k: v for k, v in release_definitions.items() if fnmatch(k, filter_name)}
def get_repos(organisations=None, projects=None, names=False, repo_specs=False, urls=False): if organisations: projects = get_projects(organisations, project_specs=True) if not projects: return [] repos = [(project, repo) for project in projects for repo in call( f'/{project}/_apis/git/repositories?api-version=3.2')['value']] if names: return [repo[1]['name'] for repo in repos] if repo_specs: return [f'{repo[0]}/{repo[1]["name"]}' for repo in repos] if urls: return [repo[1]['remoteUrl'] for repo in repos] return repos
def delete_build_definition(project, definition_id): organisation, project = project.split('/') return call( f'/{organisation}/{project}/_apis/build/definitions/{definition_id}?api-version=3.2', method='DELETE', only_response_code=True)
def create_release_definition(organisation, project, release_definition): return call( f'/{organisation}/{project}/_apis/release/definitions?api-version=3.2-preview', request_data=release_definition, method='POST', only_response_code=True)