| 31 | |
| 32 | |
| 33 | class AzureDevOpsAPI(API): |
| 34 | paginator = AzurePaginator() |
| 35 | base_url = "https://dev.azure.com/" |
| 36 | |
| 37 | @request_hook |
| 38 | def authenticate(self, request: Request): |
| 39 | token_b64 = base64.b64encode((':' + self.connection.token.get_secret_value()).encode()).decode() |
| 40 | request.headers['Authorization'] = 'Basic ' + token_b64 |
| 41 | |
| 42 | @request_hook |
| 43 | def set_api_version(self, request: Request): |
| 44 | request.query_args['api-version'] = "7.0" |
| 45 | |
| 46 | @response_hook |
| 47 | def change_203_to_401(self, response: Response): |
| 48 | # When the token is invalid, Azure DevOps returns a 302 that resolves to a sign-in page with status 203 |
| 49 | # We want to change that to a 401 and raise an exception |
| 50 | if response.status == 203: |
| 51 | response.status = 401 |
| 52 | raise APIException(response) |
| 53 | |
| 54 | def my_profile(self): |
| 55 | req = Request('https://app.vssps.visualstudio.com/_apis/profile/profiles/me') |
| 56 | return self.send(req) |
| 57 | |
| 58 | def accounts(self, member_id: str): |
| 59 | req = Request('https://app.vssps.visualstudio.com/_apis/accounts', query_args={"memberId": member_id}) |
| 60 | return self.send(req) |
| 61 | |
| 62 | def projects(self, org: str): |
| 63 | return self.get(org, '_apis/projects') |
| 64 | |
| 65 | def git_repos(self, org: str, project: str): |
| 66 | return self.get(org, project, '_apis/git/repositories') |
| 67 | |
| 68 | def git_repo_pull_requests(self, org: str, project: str, repo_id: str): |
| 69 | return self.get(org, project, '_apis/git/repositories', repo_id, 'pullrequests?searchCriteria.status=all') |
| 70 | |
| 71 | def git_repo_pull_request_commits(self, org: str, project: str, repo_id: str, pull_request_id: int): |
| 72 | return self.get(org, project, '_apis/git/repositories', repo_id, 'pullRequests', pull_request_id, 'commits') |
| 73 | |
| 74 | def git_repo_pull_request_comments(self, org: str, project: str, repo_id: str, pull_request_id: int): |
| 75 | return self.get(org, project, '_apis/git/repositories', repo_id, 'pullRequests', pull_request_id, 'threads') |
| 76 | |
| 77 | def commits(self, org: str, project: str, repo_id: str): |
| 78 | return self.get(org, project, '_apis/git/repositories', repo_id, 'commits') |
| 79 | |
| 80 | def builds(self, org: str, project: str, repository_id: str, provider: str): |
| 81 | return self.get(org, project, '_apis/build/builds', repositoryId=repository_id, repositoryType=provider, |
| 82 | deletedFilter='excludeDeleted', |
| 83 | queryOrder="finishTimeDescending") |
| 84 | |
| 85 | def jobs(self, org: str, project: str, build_id: int): |
| 86 | return self.get(org, project, '_apis/build/builds', build_id, 'timeline') |
| 87 | |
| 88 | def endpoints(self, org: str, project: str): |
| 89 | return self.get(org, project, '_apis/serviceendpoint/endpoints') |
| 90 | |