-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathpull_request.py
More file actions
405 lines (322 loc) · 11.2 KB
/
pull_request.py
File metadata and controls
405 lines (322 loc) · 11.2 KB
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
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT
import datetime
from collections.abc import Iterable
from re import Match
from typing import Any, Optional, Union
from ogr import abstract as _abstract
from ogr.abstract.abstract_class import OgrAbstractClass
from ogr.abstract.commit import CommitLikeChanges
from ogr.abstract.commit_flag import CommitFlag
from ogr.abstract.git_project import GitProject
from ogr.abstract.status import MergeCommitStatus, PRStatus
class PullRequest(OgrAbstractClass):
"""
Attributes:
project (GitProject): Project of the pull request.
"""
def __init__(self, raw_pr: Any, project: "GitProject") -> None:
self._raw_pr = raw_pr
self._target_project = project
@property
def title(self) -> str:
"""Title of the pull request."""
raise NotImplementedError()
@title.setter
def title(self, new_title: str) -> None:
raise NotImplementedError()
@property
def id(self) -> int:
"""ID of the pull request."""
raise NotImplementedError()
@property
def status(self) -> PRStatus:
"""Status of the pull request."""
raise NotImplementedError()
@property
def url(self) -> str:
"""Web URL of the pull request."""
raise NotImplementedError()
@property
def description(self) -> str:
"""Description of the pull request."""
raise NotImplementedError()
@description.setter
def description(self, new_description: str) -> None:
raise NotImplementedError
@property
def author(self) -> str:
"""Login of the author of the pull request."""
raise NotImplementedError()
@property
def source_branch(self) -> str:
"""Name of the source branch (from which the changes are pulled)."""
raise NotImplementedError()
@property
def target_branch(self) -> str:
"""Name of the target branch (where the changes are being merged)."""
raise NotImplementedError()
@property
def created(self) -> datetime.datetime:
"""Datetime of creating the pull request."""
raise NotImplementedError()
@property
def labels(self) -> Union[list["_abstract.PRLabel"], Iterable["_abstract.PRLabel"]]:
"""Labels of the pull request."""
raise NotImplementedError()
@property
def changes(self) -> "PullRequestChanges":
"""Commit-like change information."""
raise NotImplementedError()
@property
def diff_url(self) -> str:
"""Web URL to the diff of the pull request."""
raise NotImplementedError()
@property
def patch(self) -> bytes:
"""Patch of the pull request."""
raise NotImplementedError()
@property
def head_commit(self) -> str:
"""Commit hash of the HEAD commit of the pull request."""
raise NotImplementedError()
@property
def target_branch_head_commit(self) -> str:
"""Commit hash of the HEAD commit of the target branch."""
raise NotImplementedError()
@property
def merge_commit_sha(self) -> str:
"""
Commit hash of the merge commit of the pull request.
Before merging represents test merge commit, if git forge supports it.
"""
raise NotImplementedError()
@property
def merge_commit_status(self) -> MergeCommitStatus:
"""Current status of the test merge commit."""
raise NotImplementedError()
@property
def source_project(self) -> "GitProject":
"""Object that represents source project (from which the changes are pulled)."""
raise NotImplementedError()
@property
def target_project(self) -> "GitProject":
"""Object that represents target project (where changes are merged)."""
return self._target_project
@property
def commits_url(self) -> str:
"""Web URL to the list of commits in the pull request."""
raise NotImplementedError()
@property
def closed_by(self) -> Optional[str]:
"""Login of the account that closed the pull request."""
raise NotImplementedError()
def __str__(self) -> str:
description = (
f"{self.description[:10]}..." if self.description is not None else "None"
)
return (
f"PullRequest("
f"title='{self.title}', "
f"id={self.id}, "
f"status='{self.status.name}', "
f"url='{self.url}', "
f"diff_url='{self.diff_url}', "
f"description='{description}', "
f"author='{self.author}', "
f"source_branch='{self.source_branch}', "
f"target_branch='{self.target_branch}', "
f"created='{self.created}')"
)
@staticmethod
def create(
project: Any,
title: str,
body: str,
target_branch: str,
source_branch: str,
fork_username: Optional[str] = None,
) -> "PullRequest":
"""
Create new pull request.
Args:
project (GitProject): Project where the pull request will be created.
title: Title of the pull request.
body: Description of the pull request.
target_branch: Branch in the project where the changes are being
merged.
source_branch: Branch from which the changes are being pulled.
fork_username: The username/namespace of the forked repository.
Returns:
Object that represents newly created pull request.
"""
raise NotImplementedError()
@staticmethod
def get(project: Any, id: int) -> "PullRequest":
"""
Get pull request.
Args:
project (GitProject): Project where the pull request is located.
id: ID of the pull request.
Returns:
Object that represents pull request.
"""
raise NotImplementedError()
@staticmethod
def get_list(
project: Any,
status: PRStatus = PRStatus.open,
) -> Union[list["PullRequest"], Iterable["PullRequest"]]:
"""
List of pull requests.
Args:
project (GitProject): Project where the pull requests are located.
status: Filters out the pull requests.
Defaults to `PRStatus.open`.
Returns:
List of pull requests with requested status.
"""
raise NotImplementedError()
def update_info(
self,
title: Optional[str] = None,
description: Optional[str] = None,
) -> "PullRequest":
"""
Update pull request information.
Args:
title: The new title of the pull request.
Defaults to `None`, which means no updating.
description: The new description of the pull request.
Defaults to `None`, which means no updating.
Returns:
Pull request itself.
"""
raise NotImplementedError()
def _get_all_comments(
self,
reverse: bool = False,
) -> Union[list["_abstract.PRComment"], Iterable["_abstract.PRComment"]]:
"""
Get list of all pull request comments.
Args:
reverse: Defines whether the comments should be listed in a reversed
order.
Defaults to `False`.
Returns:
List of all comments on the pull request.
"""
raise NotImplementedError()
def get_comments(
self,
filter_regex: Optional[str] = None,
reverse: bool = False,
author: Optional[str] = None,
) -> Union[list["_abstract.PRComment"], Iterable["_abstract.PRComment"]]:
"""
Get list of pull request comments.
Args:
filter_regex: Filter the comments' content with `re.search`.
Defaults to `None`, which means no filtering.
reverse: Whether the comments are to be returned in
reversed order.
Defaults to `False`.
author: Filter the comments by author.
Defaults to `None`, which means no filtering.
Returns:
List of pull request comments.
"""
raise NotImplementedError()
def get_all_commits(self) -> Union[list[str], Iterable[str]]:
"""
Returns:
List of commit hashes of commits in pull request.
"""
raise NotImplementedError()
def search(
self,
filter_regex: str,
reverse: bool = False,
description: bool = True,
) -> Optional[Match[str]]:
"""
Find match in pull request description or comments.
Args:
filter_regex: Regex that is used to filter the comments' content with `re.search`.
reverse: Reverse order of the comments.
Defaults to `False`.
description: Whether description is included in the search.
Defaults to `True`.
Returns:
`re.Match` if found, `None` otherwise.
"""
raise NotImplementedError()
def comment(
self,
body: str,
commit: Optional[str] = None,
filename: Optional[str] = None,
row: Optional[int] = None,
) -> "_abstract.PRComment":
"""
Add new comment to the pull request.
Args:
body: Body of the comment.
commit: Commit hash to which comment is related.
Defaults to generic comment.
filename: Path to the file to which comment is related.
Defaults to no relation to the file.
row: Line number to which the comment is related.
Defaults to no relation to the line.
Returns:
Newly created comment.
"""
raise NotImplementedError()
def close(self) -> "PullRequest":
"""
Close the pull request.
Returns:
Pull request itself.
"""
raise NotImplementedError()
def merge(self) -> "PullRequest":
"""
Merge the pull request.
Returns:
Pull request itself.
"""
raise NotImplementedError()
def add_label(self, *labels: str) -> None:
"""
Add labels to the pull request.
Args:
*labels: Labels to be added.
"""
raise NotImplementedError()
def get_statuses(self) -> Union[list["CommitFlag"], Iterable["CommitFlag"]]:
"""
Returns statuses for latest commit on pull request.
Returns:
List of commit statuses of the latest commit.
"""
raise NotImplementedError()
def get_comment(self, comment_id: int) -> "_abstract.PRComment":
"""
Returns a PR comment.
Args:
comment_id: id of comment
Returns:
Object representing a PR comment.
"""
raise NotImplementedError()
class PullRequestChanges(CommitLikeChanges):
"""
Class representing a PullRequest's changes
Attributes:
pull_request (PullRequest): Parent pull request.
"""
def __init__(self, pull_request: "PullRequest") -> None:
self.pull_request = pull_request
@property
def parent(self) -> "PullRequest":
return self.pull_request