diff --git a/src/lando/utils/github.py b/src/lando/utils/github.py index 1e1a4d129..298ed9dc2 100644 --- a/src/lando/utils/github.py +++ b/src/lando/utils/github.py @@ -62,7 +62,7 @@ def get(self, path: str, *args, **kwargs) -> dict: def post(self, path: str, *args, **kwargs) -> dict: """Send a POST request to the GitHub API with given args and kwargs.""" - url = f"self.GITHUB_BASE_URL/{path}" + url = f"{self.GITHUB_BASE_URL}/{path}" return self.session.post(url, *args, **kwargs) @@ -88,6 +88,10 @@ def _get(self, path: str, *args, **kwargs) -> dict: elif content_type == "application/vnd.github.diff; charset=utf-8": return result.text + def _post(self, path: str, *args, **kwargs): + result = self.client.post(path, *args, **kwargs) + return result.json() + def list_pull_requests(self) -> list: """List all pull requests in the repo.""" return self._get(f"{self.repo_base_url}/pulls") @@ -110,6 +114,25 @@ def get_patch(self, pull_number: int) -> str: headers={"Accept": "application/vnd.github.patch"}, ) + def open_pull_request(self, pull_number: int) -> dict: + """Open the given pull request.""" + return self._post( + f"{self.repo_base_url}/pulls/{pull_number}", json={"state": "open"} + ) + + def close_pull_request(self, pull_number: int) -> dict: + """Close the given pull request.""" + return self._post( + f"{self.repo_base_url}/pulls/{pull_number}", json={"state": "closed"} + ) + + def add_comment_to_pull_request(self, pull_number: int, comment: str) -> dict: + """Add a comment to the given pull request.""" + return self._post( + f"{self.repo_base_url}/issues/{pull_number}/comments", + json={"body": comment}, + ) + class PullRequest: """A class that parses data returned from the GitHub API for pull requests."""