@@ -16,13 +16,14 @@ import (
1616)
1717
1818type PullRequestOptions struct {
19- Title string
20- Body string
21- Token string
22- Org string
23- Repo string
24- BaseBranch string
25- HeadBranch string
19+ Title string
20+ Body string
21+ Token string
22+ ApprovalToken string
23+ Org string
24+ Repo string
25+ BaseBranch string
26+ HeadBranch string
2627}
2728
2829var prOpts = & PullRequestOptions {}
@@ -33,6 +34,9 @@ var pullRequestCommand = &cobra.Command{
3334 Short : "Creates a PR to update the changelog." ,
3435 Run : func (c * cobra.Command , args []string ) {
3536 client := NewClient (prOpts .Token )
37+ // PRs can't be approved by the author of the PR. Thus we need two clients.
38+ // One for creating the PR and one for approving it.
39+ approvalClient := NewClient (prOpts .ApprovalToken )
3640 context := context .Background ()
3741 newPr := & github.NewPullRequest {
3842 Title : & prOpts .Title ,
@@ -100,13 +104,30 @@ var pullRequestCommand = &cobra.Command{
100104 l += * label .Name
101105 }
102106 logger .WithField ("labels" , l ).WithField ("pr" , pr .Number ).Info ("PR labels successfully added" )
107+
108+ retries = 0
109+ for {
110+ retries ++
111+ if retries > 60 {
112+ logger .WithError (err ).Fatal ("Timeout trying to approve PR" )
113+ }
114+ event := "APPROVE"
115+ _ , _ , err := approvalClient .PullRequests .CreateReview (context , prOpts .Org , prOpts .Repo , * pr .Number , & github.PullRequestReviewRequest {Event : & event })
116+ if err != nil {
117+ logger .WithError (err ).Error ("Error approving PR. Trying again in a bit." )
118+ time .Sleep (time .Second )
119+ } else {
120+ break
121+ }
122+ }
103123 },
104124}
105125
106126func init () {
107127 // Setup prFlags before the command is initialized
108128 prFlags := pullRequestCommand .PersistentFlags ()
109129 prFlags .StringVarP (& prOpts .Token , "token" , "t" , prOpts .Token , "a GitHub personal API token to perform authenticated requests" )
130+ prFlags .StringVarP (& prOpts .ApprovalToken , "approval-token" , "a" , prOpts .Token , "a GitHub personal API token to perform PR approval" )
110131 prFlags .StringVarP (& prOpts .Org , "org" , "o" , prOpts .Org , "the github organization" )
111132 prFlags .StringVarP (& prOpts .Repo , "repo" , "r" , prOpts .Repo , "the github repository name" )
112133 prFlags .StringVarP (& prOpts .HeadBranch , "head" , "H" , "main" , "the head branch for pull requests" )
0 commit comments