|
| 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" |
0 commit comments