Skip to content

Commit 9a67970

Browse files
committed
Implement for GitCommit and CommitChanges for Github
Signed-off-by: Cristian Le <[email protected]>
1 parent 0a897cb commit 9a67970

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed

ogr/services/github/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# Copyright Contributors to the Packit project.
22
# SPDX-License-Identifier: MIT
33

4+
from ogr.services.github.changes import GithubCommitChanges, GithubPullRequestChanges
45
from ogr.services.github.check_run import GithubCheckRun
56
from ogr.services.github.comments import GithubIssueComment, GithubPRComment
7+
from ogr.services.github.commit import GithubCommit
68
from ogr.services.github.issue import GithubIssue
79
from ogr.services.github.project import GithubProject
810
from ogr.services.github.pull_request import GithubPullRequest
@@ -12,7 +14,10 @@
1214

1315
__all__ = [
1416
GithubCheckRun.__name__,
17+
GithubCommit.__name__,
18+
GithubCommitChanges.__name__,
1519
GithubPullRequest.__name__,
20+
GithubPullRequestChanges.__name__,
1621
GithubIssueComment.__name__,
1722
GithubPRComment.__name__,
1823
GithubIssue.__name__,

ogr/services/github/changes.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
from collections.abc import Iterable
4+
5+
import requests
6+
7+
from ogr.abstract import CommitChanges, PullRequestChanges
8+
from ogr.exceptions import GithubAPIException, OgrNetworkError
9+
from ogr.services import github as ogr_github
10+
11+
12+
class GithubCommitChanges(CommitChanges):
13+
commit: "ogr_github.GithubCommit"
14+
15+
@property
16+
def files(self) -> Iterable[str]:
17+
for file in self.commit._raw_commit.files:
18+
yield file.filename
19+
20+
@property
21+
def patch(self) -> bytes:
22+
patch_url = f"{self.commit._raw_commit.html_url}.patch"
23+
response = requests.get(patch_url)
24+
25+
if not response.ok:
26+
cls = OgrNetworkError if response.status_code >= 500 else GithubAPIException
27+
raise cls(
28+
f"Couldn't get patch from {patch_url} because {response.reason}.",
29+
)
30+
31+
return response.content
32+
33+
@property
34+
def diff_url(self) -> str:
35+
return f"{self.commit._raw_commit.html_url}.diff"
36+
37+
38+
class GithubPullRequestChanges(PullRequestChanges):
39+
pull_request: "ogr_github.GithubPullRequest"
40+
41+
@property
42+
def files(self) -> Iterable[str]:
43+
for file in self.pull_request._raw_pr.get_files():
44+
yield file.filename
45+
46+
@property
47+
def patch(self) -> bytes:
48+
patch_url = self.pull_request._raw_pr.patch_url
49+
response = requests.get(patch_url)
50+
51+
if not response.ok:
52+
cls = OgrNetworkError if response.status_code >= 500 else GithubAPIException
53+
raise cls(
54+
f"Couldn't get patch from {patch_url} because {response.reason}.",
55+
)
56+
57+
return response.content
58+
59+
@property
60+
def diff_url(self) -> str:
61+
return f"{self.pull_request._raw_pr.html_url}.diff"

ogr/services/github/commit.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Copyright Contributors to the Packit project.
2+
# SPDX-License-Identifier: MIT
3+
import github
4+
from github.Commit import Commit as _GitCommit
5+
6+
from ogr.exceptions import GithubAPIException
7+
from ogr.services import github as ogr_github
8+
from ogr.services.base import BaseGitCommit
9+
from ogr.services.github.changes import GithubCommitChanges
10+
11+
12+
class GithubCommit(BaseGitCommit):
13+
_raw_commit: _GitCommit
14+
_changes: GithubCommitChanges = None
15+
16+
@property
17+
def sha(self) -> str:
18+
return self._raw_commit.sha
19+
20+
@property
21+
def changes(self) -> GithubCommitChanges:
22+
if not self._changes:
23+
self._changes = GithubCommitChanges(self)
24+
return self._changes
25+
26+
@staticmethod
27+
def get(project: "ogr_github.GithubProject", sha: str) -> "GithubCommit":
28+
try:
29+
commit = project.github_repo.get_commit(sha=sha)
30+
except github.UnknownObjectException as ex:
31+
raise GithubAPIException(f"No git commit with id {sha} found") from ex
32+
return GithubCommit(commit, project)

ogr/services/github/project.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
CommitComment,
1818
CommitFlag,
1919
CommitStatus,
20+
GitCommit,
2021
GitTag,
2122
Issue,
2223
IssueStatus,
@@ -35,6 +36,7 @@
3536
GithubCheckRunStatus,
3637
)
3738
from ogr.services.github.comments import GithubCommitComment
39+
from ogr.services.github.commit import GithubCommit
3840
from ogr.services.github.flag import GithubCommitFlag
3941
from ogr.services.github.issue import GithubIssue
4042
from ogr.services.github.pull_request import GithubPullRequest
@@ -301,6 +303,10 @@ def get_pr_list(self, status: PRStatus = PRStatus.open) -> list[PullRequest]:
301303
def get_pr(self, pr_id: int) -> PullRequest:
302304
pass
303305

306+
@indirect(GithubCommit.get)
307+
def get_commit(self, sha: str) -> GitCommit:
308+
pass
309+
304310
def get_sha_from_tag(self, tag_name: str) -> str:
305311
# TODO: This is ugly. Can we do it better?
306312
all_tags = self.github_repo.get_tags()

ogr/services/github/pull_request.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from ogr.exceptions import GithubAPIException, OgrNetworkError
1919
from ogr.services import github as ogr_github
2020
from ogr.services.base import BasePullRequest
21+
from ogr.services.github.changes import GithubPullRequestChanges
2122
from ogr.services.github.comments import GithubPRComment
2223
from ogr.services.github.label import GithubPRLabel
2324

@@ -28,6 +29,7 @@ class GithubPullRequest(BasePullRequest):
2829
_raw_pr: _GithubPullRequest
2930
_target_project: "ogr_github.GithubProject"
3031
_source_project: "ogr_github.GithubProject" = None
32+
_changes: GithubPullRequestChanges = None
3133

3234
@property
3335
def title(self) -> str:
@@ -83,6 +85,12 @@ def labels(self) -> list[PRLabel]:
8385
GithubPRLabel(raw_label, self) for raw_label in self._raw_pr.get_labels()
8486
]
8587

88+
@property
89+
def changes(self) -> GithubPullRequestChanges:
90+
if not self._changes:
91+
self._changes = GithubPullRequestChanges(self)
92+
return self._changes
93+
8694
@property
8795
def diff_url(self) -> str:
8896
return f"{self._raw_pr.html_url}/files"

0 commit comments

Comments
 (0)