-
Notifications
You must be signed in to change notification settings - Fork 9
/
scm.py
468 lines (372 loc) · 15.2 KB
/
scm.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import functools
import hashlib
from dataclasses import dataclass
from itertools import chain
import gitlab.const
from github import Auth, Consts, Github, GithubException, PullRequest
from gitlab import Gitlab, GitlabAuthenticationError, GitlabError
from gitlab.v4.objects import ProjectMergeRequest
from typing_extensions import Protocol
from patchwork.logger import logger
@dataclass(slots=True)
class Comment:
path: str
body: str
start_line: int | None
end_line: int
_COMMENT_MARKER = "<!-- PatchWork comment marker -->"
class PullRequestProtocol(Protocol):
def url(self) -> str:
...
def set_pr_description(self, body: str) -> None:
...
def create_comment(
self, body: str, path: str | None = None, start_line: int | None = None, end_line: int | None = None
) -> str | None:
...
def reset_comments(self) -> None:
...
def file_diffs(self) -> dict[str, str]:
...
@staticmethod
def _get_template_indexes(template: str) -> tuple[int | None, int | None]:
start_idx = template.find("{{")
if start_idx == -1:
return None, None
end_idx = template.find("}}", start_idx)
if end_idx == -1:
return None, None
return start_idx, end_idx
@staticmethod
def _apply_pr_template(pr: "PullRequestProtocol", body: str) -> str:
if isinstance(pr, GithubPullRequest):
backup_link_format = "{url}/files"
file_link_format = backup_link_format + "#diff-{diff_anchor}"
chunk_link_format = file_link_format + "L{start_line}-L{end_line}"
anchor_hash = hashlib.sha256
elif isinstance(pr, GitlabMergeRequest):
backup_link_format = "{url}/diffs"
file_link_format = backup_link_format + "#{diff_anchor}"
# TODO: deal with gitlab line links
# chunk_link_format = file_link_format + "_{start_line}_{end_line}"
chunk_link_format = file_link_format + ""
anchor_hash = hashlib.sha1
else:
return pr.url()
template = body
start_idx, end_idx = PullRequestProtocol._get_template_indexes(template)
while start_idx is not None and end_idx is not None:
template_content = template[start_idx + 2 : end_idx]
split_parts = template_content.split(":")
split_parts_iter = iter(split_parts)
path = next(split_parts_iter, None)
start = next(split_parts_iter, None)
end = next(split_parts_iter, None)
diff_anchor = ""
format_to_use = backup_link_format
if path is not None:
diff_anchor = anchor_hash(path.encode()).hexdigest()
format_to_use = file_link_format
if start is not None and end is not None:
format_to_use = chunk_link_format
replacement_value = format_to_use.format(
url=pr.url(), diff_anchor=diff_anchor, start_line=start, end_line=end
)
template = template[:start_idx] + replacement_value + template[end_idx + 2 :]
start_idx, end_idx = PullRequestProtocol._get_template_indexes(template)
return template
class ScmPlatformClientProtocol(Protocol):
def test(self) -> bool:
...
def set_url(self, url: str) -> None:
...
def get_slug_and_id_from_url(self, url: str) -> tuple[str, int] | None:
...
def find_issue_by_url(self, url: str) -> list[str] | None:
...
def find_issue_by_id(self, slug: str, issue_id: int) -> list[str] | None:
...
def get_pr_by_url(self, url: str) -> PullRequestProtocol | None:
...
def find_pr_by_id(self, slug: str, pr_id: int) -> PullRequestProtocol | None:
...
def find_pr(self, slug: str, original_branch: str, feature_branch: str) -> PullRequestProtocol | None:
...
def create_pr(
self,
slug: str,
title: str,
body: str,
original_branch: str,
feature_branch: str,
) -> PullRequestProtocol:
...
def create_issue_comment(
self, slug: str, issue_text: str, title: str | None = None, issue_id: int | None = None
) -> str:
...
class GitlabMergeRequest(PullRequestProtocol):
def __init__(self, mr: ProjectMergeRequest):
self._mr = mr
def url(self) -> str:
return self._mr.web_url
def set_pr_description(self, body: str) -> None:
self._mr.description = PullRequestProtocol._apply_pr_template(self, body)
self._mr.save()
def create_comment(
self, body: str, path: str | None = None, start_line: int | None = None, end_line: int | None = None
) -> str | None:
final_body = f"{_COMMENT_MARKER} \n{PullRequestProtocol._apply_pr_template(self, body)}"
if path is None:
note = self._mr.notes.create({"body": final_body})
return f"#note_{note.get_id()}"
while True:
try:
commit = self._mr.commits().next()
except StopIteration:
continue
break
while True:
try:
iterator = self._mr.diffs.list(iterator=True)
diff = iterator.next() # type: ignore
except StopIteration:
continue
if diff.head_commit_sha == commit.get_id():
break
base_commit = diff.base_commit_sha
head_commit = diff.head_commit_sha
try:
discussion = self._mr.discussions.create(
{
"body": final_body,
"position": {
"base_sha": base_commit,
"start_sha": base_commit,
"head_sha": head_commit,
"position_type": "text",
"old_path": path,
"new_path": path,
"old_line": end_line,
"new_line": end_line,
},
}
)
except Exception as e:
logger.error(e)
return None
for note in discussion.attributes["notes"]:
return f"#note_{note['id']}"
return None
def reset_comments(self) -> None:
for discussion in self._mr.discussions.list(iterator=True):
for note in discussion.attributes["notes"]:
if note["body"].startswith(_COMMENT_MARKER):
discussion.notes.delete(note["id"])
def file_diffs(self) -> dict[str, str]:
diffs = self._mr.diffs.list()
latest_diff = max(diffs, key=lambda diff: diff.created_at, default=None)
if latest_diff is None:
return {}
files = self._mr.diffs.get(latest_diff.id).diffs
return {file["new_path"]: file["diff"] for file in files}
class GithubPullRequest(PullRequestProtocol):
def __init__(self, pr: PullRequest):
self._pr = pr
def url(self) -> str:
return self._pr.html_url
def set_pr_description(self, body: str) -> None:
final_body = PullRequestProtocol._apply_pr_template(self, body)
self._pr.edit(body=final_body)
def create_comment(
self, body: str, path: str | None = None, start_line: int | None = None, end_line: int | None = None
) -> str | None:
final_body = f"{_COMMENT_MARKER} \n{PullRequestProtocol._apply_pr_template(self, body)}"
if path is None:
return self._pr.create_issue_comment(body=final_body).html_url
kwargs = dict(body=final_body, path=path)
if start_line is not None:
kwargs["start_line"] = start_line
kwargs["start_side"] = "LEFT"
if end_line is not None:
kwargs["line"] = end_line
kwargs["side"] = "LEFT"
return self._pr.create_review_comment(commit=self._pr.get_commits()[0], **kwargs).html_url # type: ignore
def reset_comments(self) -> None:
for comment in chain(self._pr.get_review_comments(), self._pr.get_issue_comments()):
if comment.body.startswith(_COMMENT_MARKER):
comment.delete()
def file_diffs(self) -> dict[str, str]:
files = self._pr.get_files()
return {file.filename: file.patch for file in files}
class GithubClient(ScmPlatformClientProtocol):
DEFAULT_URL = Consts.DEFAULT_BASE_URL
def __init__(self, access_token: str, url: str = DEFAULT_URL):
self._access_token = access_token
self._url = url
@functools.cached_property
def github(self) -> Github:
auth = Auth.Token(self._access_token)
return Github(base_url=self._url, auth=auth)
def test(self) -> bool:
return True
def set_url(self, url: str) -> None:
self._url = url
def get_slug_and_id_from_url(self, url: str) -> tuple[str, int] | None:
url_parts = url.split("/")
if len(url_parts) < 5:
logger.error(f"Invalid issue URL: {url}")
return None
try:
resource_id = int(url_parts[-1])
except ValueError:
logger.error(f"Invalid issue URL: {url}")
return None
slug = "/".join(url_parts[-4:-2])
return slug, resource_id
def find_issue_by_url(self, url: str) -> list[str] | None:
slug, issue_id = self.get_slug_and_id_from_url(url)
return self.find_issue_by_id(slug, issue_id)
def find_issue_by_id(self, slug: str, issue_id: int) -> list[str] | None:
repo = self.github.get_repo(slug)
try:
issue = repo.get_issue(issue_id)
body = issue.body
comments = [issue_comment.body for issue_comment in issue.get_comments()]
return [body] + comments
except GithubException as e:
logger.warn(f"Failed to get issue: {e}")
return None
def get_pr_by_url(self, url: str) -> PullRequestProtocol | None:
slug, pr_id = self.get_slug_and_id_from_url(url)
return self.find_pr_by_id(slug, pr_id)
def find_pr_by_id(self, slug: str, pr_id: int) -> PullRequestProtocol | None:
repo = self.github.get_repo(slug)
try:
pr = repo.get_pull(pr_id)
return GithubPullRequest(pr)
except GithubException as e:
logger.warn(f"Failed to get PR: {e}")
return None
def find_pr(self, slug, original_branch: str, feature_branch: str) -> PullRequestProtocol | None:
repo = self.github.get_repo(slug)
pages = repo.get_pulls(base=original_branch, head=feature_branch)
for pr in iter(pages):
if pr.base.ref == original_branch and pr.head.ref == feature_branch:
return GithubPullRequest(pr)
return None
def create_pr(
self,
slug: str,
title: str,
body: str,
original_branch: str,
feature_branch: str,
) -> PullRequestProtocol:
# before creating a PR, check if one already exists
repo = self.github.get_repo(slug)
gh_pr = repo.create_pull(title=title, body=body, base=original_branch, head=feature_branch)
pr = GithubPullRequest(gh_pr)
return pr
def create_issue_comment(
self, slug: str, issue_text: str, title: str | None = None, issue_id: int | None = None
) -> str:
repo = self.github.get_repo(slug)
if issue_id is not None:
return repo.get_issue(issue_id).create_comment(issue_text).html_url
else:
return repo.create_issue(title, issue_text).html_url
class GitlabClient(ScmPlatformClientProtocol):
DEFAULT_URL = gitlab.const.DEFAULT_URL
def __init__(self, access_token: str, url: str = DEFAULT_URL):
self._access_token = access_token
self._url = url
@functools.cached_property
def gitlab(self) -> Gitlab:
return Gitlab(self._url, private_token=self._access_token)
def set_url(self, url: str) -> None:
self._url = url
def test(self) -> bool:
try:
self.gitlab.auth()
except GitlabAuthenticationError:
return False
return self.gitlab.user is not None
def get_slug_and_id_from_url(self, url: str) -> tuple[str, int] | None:
url_parts = url.split("/")
if len(url_parts) < 5:
logger.error(f"Invalid issue URL: {url}")
return None
try:
resource_id = int(url_parts[-1])
except ValueError:
logger.error(f"Invalid issue URL: {url}")
return None
slug = "/".join(url_parts[-5:-3])
return slug, resource_id
def find_issue_by_url(self, url: str) -> list[str] | None:
slug, issue_id = self.get_slug_and_id_from_url(url)
return self.find_issue_by_id(slug, issue_id)
def find_issue_by_id(self, slug: str, issue_id: int) -> list[str] | None:
project = self.gitlab.projects.get(slug)
try:
issue = project.issues.get(issue_id)
body = issue["description"]
comments = [note["body"] for note in issue.notes.list()]
return [body] + comments
except GitlabError as e:
logger.warn(f"Failed to get issue: {e}")
return None
def get_pr_by_url(self, url: str) -> PullRequestProtocol | None:
slug, pr_id = self.get_slug_and_id_from_url(url)
return self.find_pr_by_id(slug, pr_id)
def find_pr_by_id(self, slug: str, pr_id: int) -> PullRequestProtocol | None:
project = self.gitlab.projects.get(slug)
try:
mr = project.mergerequests.get(pr_id)
return GitlabMergeRequest(mr)
except GitlabError as e:
logger.warn(f"Failed to get MR: {e}")
return None
def find_pr(self, slug, original_branch: str, feature_branch: str) -> PullRequestProtocol | None:
project = self.gitlab.projects.get(slug)
mrs = project.mergerequests.list(
iterator=True,
state="opened",
source_branch=feature_branch,
target_branch=original_branch,
)
mr: ProjectMergeRequest
try:
mr = mrs.next() # type: ignore
except StopIteration:
return None
return GitlabMergeRequest(mr)
def create_pr(
self,
slug: str,
title: str,
body: str,
original_branch: str,
feature_branch: str,
) -> PullRequestProtocol:
# before creating a PR, check if one already exists
project = self.gitlab.projects.get(slug)
gl_mr = project.mergerequests.create(
{
"source_branch": feature_branch,
"target_branch": original_branch,
"title": title,
"description": body,
}
)
mr = GitlabMergeRequest(gl_mr) # type: ignore
return mr
def create_issue_comment(
self, slug: str, issue_text: str, title: str | None = None, issue_id: int | None = None
) -> str:
if issue_id is not None:
obj = self.gitlab.projects.get(slug).issues.get(issue_id).notes.create({"body": issue_text})
return obj["web_url"]
obj = self.gitlab.projects.get(slug).issues.create({"title": title, "description": issue_text})
return obj["web_url"]