@@ -220,6 +220,73 @@ def api_uncached(
220220 self ._process_error (response )
221221 return response .json ()
222222
223+ def api_post (
224+ self ,
225+ url : str ,
226+ auth : AuthLevel = AuthLevel .GITHUB ,
227+ json : Any = None ,
228+ params : dict [str , Any ] | None = None ,
229+ ) -> Any :
230+ api_requests .append (f"POST { self ._api_url } { url } " )
231+ response = requests .post (
232+ f"{ self ._api_url } { url } " ,
233+ headers = self ._auth_headers (auth = auth ),
234+ json = json ,
235+ params = params ,
236+ )
237+ self ._process_error (response )
238+ if response .content :
239+ return response .json ()
240+ return None
241+
242+ def api_patch (
243+ self ,
244+ url : str ,
245+ auth : AuthLevel = AuthLevel .GITHUB ,
246+ json : Any = None ,
247+ ) -> Any :
248+ api_requests .append (f"PATCH { self ._api_url } { url } " )
249+ response = requests .patch (
250+ f"{ self ._api_url } { url } " ,
251+ headers = self ._auth_headers (auth = auth ),
252+ json = json ,
253+ )
254+ self ._process_error (response )
255+ if response .content :
256+ return response .json ()
257+ return None
258+
259+ def api_put (
260+ self ,
261+ url : str ,
262+ auth : AuthLevel = AuthLevel .GITHUB ,
263+ json : Any = None ,
264+ ) -> Any :
265+ api_requests .append (f"PUT { self ._api_url } { url } " )
266+ response = requests .put (
267+ f"{ self ._api_url } { url } " ,
268+ headers = self ._auth_headers (auth = auth ),
269+ json = json ,
270+ )
271+ self ._process_error (response )
272+ if response .content :
273+ return response .json ()
274+ return None
275+
276+ def api_delete (
277+ self ,
278+ url : str ,
279+ auth : AuthLevel = AuthLevel .GITHUB ,
280+ json : Any = None ,
281+ ) -> None :
282+ api_requests .append (f"DELETE { self ._api_url } { url } " )
283+ response = requests .delete (
284+ f"{ self ._api_url } { url } " ,
285+ headers = self ._auth_headers (auth = auth ),
286+ json = json ,
287+ )
288+ self ._process_error (response )
289+
223290 def api (
224291 self ,
225292 url : str ,
@@ -254,11 +321,18 @@ def username(self) -> str | None:
254321 return None
255322 return str (self .api ("/user" , auth = AuthLevel .GITHUB )["login" ])
256323
257- def release_id (self , tag : str ) -> int :
258- """Get the GitHub release ID number for a tag."""
324+ def get_release_id (self , tag : str ) -> int | None :
325+ """Get the GitHub release ID number for a tag, or None if not found ."""
259326 for release in self .api (f"/repos/{ self .repository ()} /releases" ):
260327 if release ["tag_name" ] == tag :
261328 return int (release ["id" ])
329+ return None
330+
331+ def release_id (self , tag : str ) -> int :
332+ """Get the GitHub release ID number for a tag."""
333+ rid = self .get_release_id (tag )
334+ if rid is not None :
335+ return rid
262336 raise ValueError (f"Release { tag } not found in { self .repository ()} " )
263337
264338 def actor (self ) -> str :
@@ -325,21 +399,17 @@ def next_milestone(self) -> Milestone:
325399
326400 def assign_milestone (self , issue_id : int , milestone : int ) -> None :
327401 """Assign the given milestone to the given issue."""
328- response = requests .patch (
329- f"{ self ._api_url } /repos/{ self .repository ()} /issues/{ issue_id } " ,
330- headers = self ._auth_headers (AuthLevel .GITHUB ),
402+ self .api_patch (
403+ f"/repos/{ self .repository ()} /issues/{ issue_id } " ,
331404 json = {"milestone" : milestone },
332405 )
333- self ._process_error (response )
334406
335407 def close_milestone (self , number : int ) -> None :
336408 """Close the milestone with the given number."""
337- response = requests .patch (
338- f"{ self ._api_url } /repos/{ self .repository ()} /milestones/{ number } " ,
339- headers = self ._auth_headers (AuthLevel .GITHUB ),
409+ self .api_patch (
410+ f"/repos/{ self .repository ()} /milestones/{ number } " ,
340411 json = {"state" : "closed" },
341412 )
342- self ._process_error (response )
343413
344414 def open_milestone_issues (self , milestone : int ) -> list [Issue ]:
345415 """Get all the open issues for a given milestone."""
@@ -362,21 +432,17 @@ def get_issue(self, issue_number: int) -> Issue:
362432
363433 def rename_issue (self , issue_number : int , title : str ) -> None :
364434 """Rename the issue with the given number."""
365- response = requests .patch (
366- f"{ self ._api_url } /repos/{ self .repository ()} /issues/{ issue_number } " ,
367- headers = self ._auth_headers (AuthLevel .GITHUB ),
435+ self .api_patch (
436+ f"/repos/{ self .repository ()} /issues/{ issue_number } " ,
368437 json = {"title" : title },
369438 )
370- self ._process_error (response )
371439
372440 def close_issue (self , issue_number : int ) -> None :
373441 """Close the issue with the given number."""
374- response = requests .patch (
375- f"{ self ._api_url } /repos/{ self .repository ()} /issues/{ issue_number } " ,
376- headers = self ._auth_headers (AuthLevel .GITHUB ),
442+ self .api_patch (
443+ f"/repos/{ self .repository ()} /issues/{ issue_number } " ,
377444 json = {"state" : "closed" },
378445 )
379- self ._process_error (response )
380446
381447 def latest_release (self ) -> str :
382448 """Get the name of the current release in the repository."""
@@ -400,29 +466,25 @@ def release_candidates(self, version: str) -> list[int]:
400466
401467 def issue_assign (self , issue_id : int , assignees : list [str ]) -> None :
402468 """Assign the given issue to the given list of users."""
403- response = requests .post (
404- f"{ self ._api_url } /repos/{ self .repository ()} /issues/{ issue_id } /assignees" ,
405- headers = self ._auth_headers (AuthLevel .GITHUB ),
469+ self .api_post (
470+ f"/repos/{ self .repository ()} /issues/{ issue_id } /assignees" ,
406471 json = {"assignees" : assignees },
407472 )
408- self ._process_error (response )
409473
410474 def issue_unassign (self , issue_id : int , assignees : list [str ]) -> None :
411475 """Unassign the given issue from the given list of users."""
412- response = requests .delete (
413- f"{ self ._api_url } /repos/{ self .repository ()} /issues/{ issue_id } /assignees" ,
414- headers = self ._auth_headers (AuthLevel .GITHUB ),
476+ self .api_delete (
477+ f"/repos/{ self .repository ()} /issues/{ issue_id } /assignees" ,
415478 json = {"assignees" : assignees },
416479 )
417- self ._process_error (response )
418480
419481 def create_pr (
420482 self , title : str , body : str , head : str , base : str , milestone : int
421483 ) -> PullRequest :
422484 """Create a pull request with the given title and body."""
423- response = requests . post (
424- f"{ self . _api_url } /repos/{ self .repository ()} /pulls" ,
425- headers = self . _auth_headers ( AuthLevel .RELEASER ) ,
485+ pr_json = self . api_post (
486+ f"/repos/{ self .repository ()} /pulls" ,
487+ auth = AuthLevel .RELEASER ,
426488 json = {
427489 "title" : title ,
428490 "body" : body ,
@@ -431,8 +493,7 @@ def create_pr(
431493 "draft" : True ,
432494 },
433495 )
434- self ._process_error (response )
435- pr = PullRequest .fromJSON (response .json ())
496+ pr = PullRequest .fromJSON (pr_json )
436497 if milestone :
437498 self .change_issue (pr .number , {"milestone" : milestone })
438499 return pr
@@ -469,21 +530,17 @@ def find_pr_for_branch(
469530
470531 def change_pr (self , number : int , changes : dict [str , str | int ]) -> None :
471532 """Modify a PR with the given number."""
472- response = requests .patch (
473- f"{ self ._api_url } /repos/{ self .repository ()} /pulls/{ number } " ,
474- headers = self ._auth_headers (AuthLevel .GITHUB ),
533+ self .api_patch (
534+ f"/repos/{ self .repository ()} /pulls/{ number } " ,
475535 json = changes ,
476536 )
477- self ._process_error (response )
478537
479538 def change_issue (self , number : int , changes : dict [str , str | int ]) -> None :
480539 """Modify an issue with the given number."""
481- response = requests .patch (
482- f"{ self ._api_url } /repos/{ self .repository ()} /issues/{ number } " ,
483- headers = self ._auth_headers (AuthLevel .GITHUB ),
540+ self .api_patch (
541+ f"/repos/{ self .repository ()} /issues/{ number } " ,
484542 json = changes ,
485543 )
486- self ._process_error (response )
487544
488545 def checks (self , commit : str ) -> dict [str , CheckRun ]:
489546 """Return all the GitHub Actions results."""
@@ -527,11 +584,14 @@ def download_artifact(self, name: str, run_id: int) -> bytes:
527584
528585 def release_assets (self , tag : str ) -> list [ReleaseAsset ]:
529586 """Return all the assets for a given tag."""
587+ rid = self .get_release_id (tag )
588+ if rid is None :
589+ return []
530590 return [
531591 ReleaseAsset .fromJSON (a )
532- for a in self .api_uncached (
533- f"/repos/ { self . repository () } /releases/ { self . release_id ( tag ) } "
534- )[ "assets" ]
592+ for a in self .api_uncached (f"/repos/ { self . repository () } /releases/ { rid } " )[
593+ "assets "
594+ ]
535595 ]
536596
537597 def download_asset (self , asset_id : int ) -> bytes :
@@ -550,8 +610,9 @@ def upload_asset(
550610 self , tag : str , filename : str , content_type : str , data : bytes | IO [bytes ]
551611 ) -> None :
552612 """Upload an asset to the release with the given tag."""
613+ rid = self .release_id (tag )
553614 response = requests .post (
554- f"https://uploads.github.com/repos/{ self .repository ()} /releases/{ self . release_id ( tag ) } /assets" ,
615+ f"https://uploads.github.com/repos/{ self .repository ()} /releases/{ rid } /assets" ,
555616 headers = {
556617 "Content-Type" : content_type ,
557618 ** self ._auth_headers (AuthLevel .GITHUB ),
@@ -654,9 +715,8 @@ def tag(
654715 """Create an unsigned tag (by github-actions[bot]) for the given commit."""
655716 if not tag_message .endswith ("\n " ):
656717 tag_message += "\n "
657- tag_response = requests .post (
658- f"{ self ._api_url } /repos/{ slug } /git/tags" ,
659- headers = self ._auth_headers (AuthLevel .GITHUB ),
718+ tag_response = self .api_post (
719+ f"/repos/{ slug } /git/tags" ,
660720 json = {
661721 "tag" : tag_name ,
662722 "message" : tag_message ,
@@ -668,37 +728,47 @@ def tag(
668728 },
669729 },
670730 )
671- self ._process_error (tag_response )
672- tag_ref_response = requests .post (
673- f"{ self ._api_url } /repos/{ slug } /git/refs" ,
674- headers = self ._auth_headers (AuthLevel .GITHUB ),
731+ tag_ref_response = self .api_post (
732+ f"/repos/{ slug } /git/refs" ,
675733 json = {
676734 "ref" : f"refs/tags/{ tag_name } " ,
677- "sha" : tag_response .json ()["sha" ],
735+ "sha" : tag_response ["sha" ],
736+ },
737+ )
738+ return str (tag_response ["sha" ])
739+
740+ def create_release (self , tag : str , notes : str , prerelease : bool ) -> Any :
741+ """Create a new release on GitHub."""
742+ return self .api_post (
743+ f"/repos/{ self .repository ()} /releases" ,
744+ json = {
745+ "tag_name" : tag ,
746+ "body" : notes ,
747+ "prerelease" : prerelease ,
748+ "draft" : True ,
678749 },
679750 )
680- self ._process_error (tag_ref_response )
681- return str (tag_response .json ()["sha" ])
682751
683752 def set_release_notes (self , tag : str , notes : str , prerelease : bool ) -> None :
684753 """Set the release notes for a given tag in the release description."""
685- response = requests .patch (
686- f"{ self ._api_url } /repos/{ self .repository ()} /releases/{ self .release_id (tag )} " ,
687- headers = self ._auth_headers (AuthLevel .GITHUB ),
754+ self .api_patch (
755+ f"/repos/{ self .repository ()} /releases/{ self .release_id (tag )} " ,
688756 json = {
689757 "body" : notes ,
690758 "tag_name" : tag ,
691759 "prerelease" : prerelease ,
692760 },
693761 )
694- self ._process_error (response )
695762
696763 def release_is_published (self , tag : str ) -> bool :
697764 """Check if the release with the given tag is published."""
765+ rid = self .get_release_id (tag )
766+ if rid is None :
767+ return False
698768 return (
699- self .api_uncached (
700- f"/repos/ { self . repository () } /releases/ { self . release_id ( tag ) } "
701- )[ "published_at" ]
769+ self .api_uncached (f"/repos/ { self . repository () } /releases/ { rid } " )[
770+ "published_at "
771+ ]
702772 is not None
703773 )
704774
@@ -743,6 +813,10 @@ def username() -> str | None:
743813 return DEFAULT_GITHUB .username ()
744814
745815
816+ def get_release_id (tag : str ) -> int | None :
817+ return DEFAULT_GITHUB .get_release_id (tag )
818+
819+
746820def release_id (tag : str ) -> int :
747821 return DEFAULT_GITHUB .release_id (tag )
748822
0 commit comments