Skip to content

Commit b523f8e

Browse files
committed
Use PyGithub for pull request creation, deletion
1 parent 7ef5da8 commit b523f8e

File tree

2 files changed

+27
-49
lines changed

2 files changed

+27
-49
lines changed

cookie_python/manage/github.py

+15
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import contextlib
22
import os
33
from functools import cached_property, lru_cache
4+
from typing import Optional
45

56
from github import Github
7+
from github.PullRequest import PullRequest
68
from github.Repository import Repository
79

810

@@ -22,3 +24,16 @@ def find_repo(self, search: str) -> Repository:
2224
with contextlib.suppress(IndexError):
2325
search = os.path.splitext(search.split(":")[1])[0]
2426
return self._gh.get_repo(search)
27+
28+
def find_pr(
29+
self, repo: Repository, head: str, base: str = "main"
30+
) -> Optional[PullRequest]:
31+
pulls = [
32+
pr
33+
for pr in repo.get_pulls(head=f"{self.username}:{head}", base=base)
34+
]
35+
if not pulls:
36+
return None
37+
if len(pulls) > 1:
38+
raise Exception(f"Multiple PRs found matching {head}")
39+
return pulls[0]

cookie_python/manage/repo.py

+12-49
Original file line numberDiff line numberDiff line change
@@ -115,41 +115,15 @@ def lint_test(self) -> None:
115115
self.logger.error("Resolve errors and exit shell to continue")
116116
self.shell()
117117

118-
def find_existing_pr(self) -> Optional[str]:
119-
with contextlib.suppress(
120-
subprocess.CalledProcessError, json.JSONDecodeError, TypeError
121-
):
122-
for pr in json.loads(
123-
self.run(
124-
[
125-
"gh",
126-
"pr",
127-
"list",
128-
"-H",
129-
self.branch,
130-
"-B",
131-
"main",
132-
"--json",
133-
",".join(("url", "headRefName", "baseRefName")),
134-
],
135-
capture_output=True,
136-
check=True,
137-
).stdout.decode()
138-
):
139-
pr_url = str(pr.pop("url"))
140-
if pr == {"headRefName": self.branch, "baseRefName": "main"}:
141-
return pr_url
142-
return None
143-
144118
def close_existing_pr(self) -> None:
145119
# Locate existing PR
146-
pr_url = self.find_existing_pr()
147-
if pr_url:
120+
pr = self.gh.find_pr(self.repo, self.branch)
121+
if pr:
148122
if self.dry_run:
149-
self.logger.info(f"Would close existing PR {pr_url}")
123+
self.logger.info(f"Would close existing PR {pr.url}")
150124
else:
151-
self.run(["gh", "pr", "close", pr_url])
152-
self.logger.info(f"Closed existing PR {pr_url}")
125+
pr.edit(state="closed")
126+
self.logger.info(f"Closed existing PR {pr.url}")
153127
if self.dry_run:
154128
return
155129
# Delete existing branch
@@ -168,21 +142,10 @@ def open_pr(self, message: str) -> None:
168142
return
169143
self.run(["git", "push", "origin", self.branch])
170144
commit_title, _, *commit_body = message.splitlines()
171-
pr_url = self.run(
172-
[
173-
"gh",
174-
"pr",
175-
"create",
176-
"--title",
177-
commit_title.strip(),
178-
"--body-file",
179-
"-",
180-
"--base",
181-
"main",
182-
"--head",
183-
self.branch,
184-
],
185-
input=os.linesep.join(commit_body).encode("utf-8"),
186-
capture_output=True,
187-
).stdout.decode()
188-
self.logger.success(f"Opened PR {pr_url}")
145+
pr = self.repo.create_pull(
146+
base="main",
147+
head=self.branch,
148+
title=commit_title.strip(),
149+
body=os.linesep.join(commit_body),
150+
)
151+
self.logger.success(f"Opened PR {pr.url}")

0 commit comments

Comments
 (0)