1313
1414import loguru
1515
16+ from .github import GithubRepo
17+
1618
1719class RepoSandbox :
1820 def __init__ (self , repo : str , dry_run : bool = False ) -> None :
1921 self ._stack = contextlib .ExitStack ()
20- self .repo = repo
22+ self .repo = self . gh . find_repo ( repo )
2123 self .branch = "update-cookie"
2224 self .dry_run = dry_run
2325
@@ -32,9 +34,30 @@ def __exit__(
3234 ) -> None :
3335 self ._stack .close ()
3436
37+ @cached_property
38+ def gh (self ) -> GithubRepo :
39+ return GithubRepo ()
40+
41+ @cached_property
42+ def latest_release (self ) -> str :
43+ return self .repo .get_latest_release ().title
44+
45+ def create_release (self , tag : str ) -> None :
46+ print (f"Should create release { tag } " )
47+ # PyGithub's repository create_tag_and_release() doesn't support
48+ # generate_release_notes
49+ self .repo ._requester .requestJsonAndCheck ( # type: ignore
50+ "POST" ,
51+ f"/repos/{ self .repo .full_name } /releases" ,
52+ input = {
53+ "tag_name" : tag ,
54+ "generate_release_notes" : True ,
55+ },
56+ )
57+
3558 @cached_property
3659 def logger (self ) -> "loguru.Logger" :
37- return loguru .logger .bind (repo = self .repo )
60+ return loguru .logger .bind (repo = self .repo . full_name )
3861
3962 @cached_property
4063 def tempdir (self ) -> Path :
@@ -47,7 +70,9 @@ def tempdir(self) -> Path:
4770 @cached_property
4871 def clone_path (self ) -> Path :
4972 subprocess .run (
50- ["git" , "clone" , self .repo , "repo" ], cwd = self .tempdir , check = True
73+ ["git" , "clone" , self .repo .ssh_url , "repo" ],
74+ cwd = self .tempdir ,
75+ check = True ,
5176 )
5277 clone_path = self .tempdir / "repo"
5378 for cmd in (
@@ -107,41 +132,15 @@ def lint_test(self) -> None:
107132 self .logger .error ("Resolve errors and exit shell to continue" )
108133 self .shell ()
109134
110- def find_existing_pr (self ) -> Optional [str ]:
111- with contextlib .suppress (
112- subprocess .CalledProcessError , json .JSONDecodeError , TypeError
113- ):
114- for pr in json .loads (
115- self .run (
116- [
117- "gh" ,
118- "pr" ,
119- "list" ,
120- "-H" ,
121- self .branch ,
122- "-B" ,
123- "main" ,
124- "--json" ,
125- "," .join (("url" , "headRefName" , "baseRefName" )),
126- ],
127- capture_output = True ,
128- check = True ,
129- ).stdout .decode ()
130- ):
131- pr_url = str (pr .pop ("url" ))
132- if pr == {"headRefName" : self .branch , "baseRefName" : "main" }:
133- return pr_url
134- return None
135-
136135 def close_existing_pr (self ) -> None :
137136 # Locate existing PR
138- pr_url = self .find_existing_pr ( )
139- if pr_url :
137+ pr = self .gh . find_pr ( self . repo , self . branch )
138+ if pr :
140139 if self .dry_run :
141- self .logger .info (f"Would close existing PR { pr_url } " )
140+ self .logger .info (f"Would close existing PR { pr . url } " )
142141 else :
143- self . run ([ "gh" , "pr" , "close" , pr_url ] )
144- self .logger .info (f"Closed existing PR { pr_url } " )
142+ pr . edit ( state = "closed" )
143+ self .logger .info (f"Closed existing PR { pr . url } " )
145144 if self .dry_run :
146145 return
147146 # Delete existing branch
@@ -160,21 +159,10 @@ def open_pr(self, message: str) -> None:
160159 return
161160 self .run (["git" , "push" , "origin" , self .branch ])
162161 commit_title , _ , * commit_body = message .splitlines ()
163- pr_url = self .run (
164- [
165- "gh" ,
166- "pr" ,
167- "create" ,
168- "--title" ,
169- commit_title .strip (),
170- "--body-file" ,
171- "-" ,
172- "--base" ,
173- "main" ,
174- "--head" ,
175- self .branch ,
176- ],
177- input = os .linesep .join (commit_body ).encode ("utf-8" ),
178- capture_output = True ,
179- ).stdout .decode ()
180- self .logger .success (f"Opened PR { pr_url } " )
162+ pr = self .repo .create_pull (
163+ base = "main" ,
164+ head = self .branch ,
165+ title = commit_title .strip (),
166+ body = os .linesep .join (commit_body ),
167+ )
168+ self .logger .success (f"Opened PR { pr .url } " )
0 commit comments