github.com/emmahsax/go-git-helper@v0.0.8-0.20240519163017-907b9de0fa52/internal/github/github.go (about) 1 package github 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 8 "github.com/emmahsax/go-git-helper/internal/configfile" 9 "github.com/emmahsax/go-git-helper/internal/utils" 10 "github.com/google/go-github/v60/github" 11 "golang.org/x/oauth2" 12 ) 13 14 type GitHub struct { 15 Debug bool 16 Client *github.Client 17 } 18 19 func NewGitHub(debugB bool) *GitHub { 20 cf := configfile.NewConfigFile(debugB) 21 c := newGitHubClient(cf.GitHubToken()) 22 23 return &GitHub{ 24 Debug: debugB, 25 Client: c, 26 } 27 } 28 29 func (c *GitHub) CreatePullRequest(owner, repo string, options *github.NewPullRequest) (*github.PullRequest, error) { 30 var err error 31 var pr *github.PullRequest 32 33 for { 34 pr, _, err = c.Client.PullRequests.Create(context.Background(), owner, repo, options) 35 if err != nil { 36 if strings.Contains(err.Error(), "422 Draft pull requests are not supported in this repository.") { 37 fmt.Println("Draft pull requests are not supported in this repository. Retrying.") 38 options.Draft = github.Bool(false) 39 continue 40 } 41 utils.HandleError(err, c.Debug, nil) 42 return nil, err 43 } 44 45 break 46 } 47 48 return pr, nil 49 } 50 51 func newGitHubClient(token string) *github.Client { 52 ctx := context.Background() 53 ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}) 54 tc := oauth2.NewClient(ctx, ts) 55 git := github.NewClient(tc) 56 return git 57 }