-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: move github-specific things to their own package (#65)
Signed-off-by: Kent <[email protected]>
- Loading branch information
Showing
2 changed files
with
76 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"regexp" | ||
|
||
"github.com/google/go-github/v47/github" | ||
"github.com/pkg/errors" | ||
"golang.org/x/oauth2" | ||
|
||
"github.com/akuityio/bookkeeper/internal/git" | ||
) | ||
|
||
func OpenPR( | ||
ctx context.Context, | ||
repoURL string, | ||
title string, | ||
body string, | ||
targetBranch string, | ||
commitBranch string, | ||
repoCreds git.RepoCredentials, | ||
) (string, error) { | ||
owner, repo, err := parseGitHubURL(repoURL) | ||
if err != nil { | ||
return "", err | ||
} | ||
githubClient := github.NewClient( | ||
oauth2.NewClient( | ||
ctx, | ||
oauth2.StaticTokenSource( | ||
&oauth2.Token{AccessToken: repoCreds.Password}, | ||
), | ||
), | ||
) | ||
pr, _, err := githubClient.PullRequests.Create( | ||
ctx, | ||
owner, | ||
repo, | ||
&github.NewPullRequest{ | ||
Title: github.String(title), | ||
Base: github.String(targetBranch), | ||
Head: github.String(commitBranch), | ||
Body: github.String(body), | ||
MaintainerCanModify: github.Bool(false), | ||
}, | ||
) | ||
// We don't unconditionally return *pr.HTMLURL and err because if err != nil, | ||
// pr might be == nil | ||
if err != nil { | ||
return "", | ||
errors.Wrap(err, "error opening pull request to the target branch") | ||
} | ||
return *pr.HTMLURL, nil | ||
} | ||
|
||
func parseGitHubURL(url string) (string, string, error) { | ||
regex := regexp.MustCompile(`^https\://github\.com/([\w-]+)/([\w-]+).*`) | ||
parts := regex.FindStringSubmatch(url) | ||
if len(parts) != 3 { | ||
return "", "", errors.Errorf("error parsing github repository URL %q", url) | ||
} | ||
return parts[1], parts[2], nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters