-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathado.py
212 lines (174 loc) · 6.37 KB
/
ado.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
from dataclasses import dataclass
import requests
from client import VCSClient, VCSRepoClient
from common import (
CredentialsValidationError,
PullRequestCreationError,
PullRequestInfo,
RepositoryAccessError,
RepositoryInfo,
)
@dataclass
class ADORepositoryInfo(RepositoryInfo):
organization: str
project: str
class ADOClient(VCSClient):
token_prefix = "Basic"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.headers["Accept"] = "application/json;api-version=6.0;"
def validate_credentials(self) -> None:
"""
There is no api that can be used to validate the credentials without organization.
The credentials will be validated in the get_repository_info method when trying to access each of the repositories
"""
pass
def get_repository_info(self, repo_name: str) -> RepositoryInfo:
repo_paths = repo_name.split("/")
if len(repo_paths) != 3:
raise RepositoryAccessError(
"Organization and project names should be included in the repository path: "
"organization/project/repository"
)
organization, project, repository = repo_paths
try:
resp = self.get(
f"{organization}/{project}/_apis/git/repositories/{repository}"
)
resp.json()
if resp.status_code == 401:
raise CredentialsValidationError(
f"Invalid Azure DevOps token for {self.instance_url}"
)
except (
requests.exceptions.ConnectionError,
requests.exceptions.JSONDecodeError,
):
raise CredentialsValidationError(
f"Can't connect to Azure DevOps at {self.instance_url}"
)
if not resp.ok:
raise RepositoryAccessError(
f"Can't get repository info for {repo_name}: {resp.json()['message']}"
)
data = resp.json()
default_branch = data.get("defaultBranch", None)
name = data["name"]
repo_id = data["id"]
if not default_branch:
raise RepositoryAccessError(
f"Repository does not have a default branch: {repo_name}"
)
language = None
resp = self.get(
f"{organization}/{project}/_apis/projectanalysis/languagemetrics"
)
if resp.status_code == 401:
print(
f"Can't get language metrics for {repo_name}. You need to include Analytics scope for your PAT"
)
else:
data = resp.json()
if data and "languageBreakdown" in data:
language_data = data["languageBreakdown"]
if language_data:
language = language_data[0]["name"].lower()
return ADORepositoryInfo(
main_language=language,
default_branch=default_branch,
name=name,
id=repo_id,
project=project,
organization=organization,
)
class ADORepoClient(VCSRepoClient):
token_prefix = "Basic"
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.headers["Accept"] = "application/json;api-version=6.0;"
@property
def common_url_path(self) -> str | None:
return f"{self.repo_info.organization}/{self.repo_info.project}/_apis/git/repositories/{self.repo_info.id}"
def create_new_branch(
self,
branch_name: str,
target_branch: str,
) -> None:
# Branch is created at the same time as the commit is created in create_new_commit
pass
def create_new_commit(
self, pr_info: PullRequestInfo, repo_info: RepositoryInfo
) -> None:
target_branch = repo_info.default_branch
new_branch = pr_info.branch
resp = self.get(target_branch)
if not resp.ok:
raise PullRequestCreationError(
f"Failed to fetch the last commit on branch {target_branch}: {resp.json()['message']}"
)
latest_commit = resp.json()["value"][0]["objectId"]
resp = self.post(
"pushes",
json={
"refUpdates": [
{"name": f"refs/heads/{new_branch}", "oldObjectId": latest_commit}
],
"commits": [
{
"comment": pr_info.commit_message,
"changes": [
{
"changeType": "add",
"item": {"path": f"/{pr_info.filename}"},
"newContent": {
"content": pr_info.content,
"contentType": "rawtext",
},
}
],
}
],
},
)
if not resp.ok:
raise PullRequestCreationError(
f"Can't create new branch: {resp.json()['message']}"
)
def create_pull_request(
self, branch_name: str, target_branch: str, title: str
) -> str:
resp = self.post(
"pullRequests",
json={
"sourceRefName": f"refs/heads/{branch_name}",
"targetRefName": target_branch,
"title": title,
"description": "",
"reviewers": [],
},
)
if not resp.ok:
raise PullRequestCreationError(
f"Failed to create pull request: {resp.json()['message']}"
)
data = resp.json()
return f"{data['repository']['webUrl']}/pullrequest/{data['pullRequestId']}"
def delete_branch(self, branch_name: str) -> None:
"""
Delete a branch
"""
resp = self.get(f"refs?filter=heads/{branch_name}")
refs = resp.json()["value"]
if not refs:
raise PullRequestCreationError(f"Failed to clean up {branch_name}.")
ref = refs[0]
self.post(
"refs",
json=[
{
"name": ref["name"],
"oldObjectId": ref["objectId"],
"newObjectId": "0000000000000000000000000000000000000000",
}
],
)