Skip to content

Commit 5cead3e

Browse files
authored
chore: move github-specific things to their own package (#65)
Signed-off-by: Kent <[email protected]>
1 parent e007f6e commit 5cead3e

File tree

2 files changed

+76
-41
lines changed

2 files changed

+76
-41
lines changed

internal/github/pr.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"regexp"
6+
7+
"github.com/google/go-github/v47/github"
8+
"github.com/pkg/errors"
9+
"golang.org/x/oauth2"
10+
11+
"github.com/akuityio/bookkeeper/internal/git"
12+
)
13+
14+
func OpenPR(
15+
ctx context.Context,
16+
repoURL string,
17+
title string,
18+
body string,
19+
targetBranch string,
20+
commitBranch string,
21+
repoCreds git.RepoCredentials,
22+
) (string, error) {
23+
owner, repo, err := parseGitHubURL(repoURL)
24+
if err != nil {
25+
return "", err
26+
}
27+
githubClient := github.NewClient(
28+
oauth2.NewClient(
29+
ctx,
30+
oauth2.StaticTokenSource(
31+
&oauth2.Token{AccessToken: repoCreds.Password},
32+
),
33+
),
34+
)
35+
pr, _, err := githubClient.PullRequests.Create(
36+
ctx,
37+
owner,
38+
repo,
39+
&github.NewPullRequest{
40+
Title: github.String(title),
41+
Base: github.String(targetBranch),
42+
Head: github.String(commitBranch),
43+
Body: github.String(body),
44+
MaintainerCanModify: github.Bool(false),
45+
},
46+
)
47+
// We don't unconditionally return *pr.HTMLURL and err because if err != nil,
48+
// pr might be == nil
49+
if err != nil {
50+
return "",
51+
errors.Wrap(err, "error opening pull request to the target branch")
52+
}
53+
return *pr.HTMLURL, nil
54+
}
55+
56+
func parseGitHubURL(url string) (string, string, error) {
57+
regex := regexp.MustCompile(`^https\://github\.com/([\w-]+)/([\w-]+).*`)
58+
parts := regex.FindStringSubmatch(url)
59+
if len(parts) != 3 {
60+
return "", "", errors.Errorf("error parsing github repository URL %q", url)
61+
}
62+
return parts[1], parts[2], nil
63+
}

service.go

Lines changed: 13 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9-
"regexp"
109
"strings"
1110

1211
"github.com/ghodss/yaml"
13-
"github.com/google/go-github/v47/github"
1412
"github.com/pkg/errors"
1513
uuid "github.com/satori/go.uuid"
1614
log "github.com/sirupsen/logrus"
17-
"golang.org/x/oauth2"
1815

1916
"github.com/akuityio/bookkeeper/internal/config"
2017
"github.com/akuityio/bookkeeper/internal/git"
18+
"github.com/akuityio/bookkeeper/internal/github"
2119
"github.com/akuityio/bookkeeper/internal/metadata"
2220
)
2321

@@ -205,23 +203,7 @@ func (s *service) RenderConfig(
205203
Debug("pushed fully-rendered configuration")
206204

207205
// Open a PR if requested
208-
//
209-
// TODO: Support git providers other than GitHub
210-
//
211-
// TODO: Move this into its own github package
212-
if commitBranch != req.TargetBranch {
213-
var owner, repo string
214-
if owner, repo, err = parseGitHubURL(req.RepoURL); err != nil {
215-
return res, err
216-
}
217-
githubClient := github.NewClient(
218-
oauth2.NewClient(
219-
ctx,
220-
oauth2.StaticTokenSource(
221-
&oauth2.Token{AccessToken: req.RepoCreds.Password},
222-
),
223-
),
224-
)
206+
if branchConfig.OpenPR {
225207
commitMsgParts := strings.SplitN(commitMsg, "\n", 2)
226208
// PR title is just the first line of the commit message
227209
prTitle := fmt.Sprintf("%s --> %s", commitMsgParts[0], req.TargetBranch)
@@ -230,25 +212,24 @@ func (s *service) RenderConfig(
230212
if len(commitMsgParts) == 2 {
231213
prBody = strings.TrimSpace(commitMsgParts[1])
232214
}
233-
var pr *github.PullRequest
234-
if pr, _, err = githubClient.PullRequests.Create(
215+
// TODO: Support git providers other than GitHub
216+
if res.PullRequestURL, err = github.OpenPR(
235217
ctx,
236-
owner,
237-
repo,
238-
&github.NewPullRequest{
239-
Title: github.String(prTitle),
240-
Base: github.String(req.TargetBranch),
241-
Head: github.String(commitBranch),
242-
Body: github.String(prBody),
243-
MaintainerCanModify: github.Bool(false),
218+
req.RepoURL,
219+
prTitle,
220+
prBody,
221+
req.TargetBranch,
222+
commitBranch,
223+
git.RepoCredentials{
224+
Username: req.RepoCreds.Username,
225+
Password: req.RepoCreds.Password,
244226
},
245227
); err != nil {
246228
return res,
247229
errors.Wrap(err, "error opening pull request to the target branch")
248230
}
249-
logger.WithField("prURL", *pr.HTMLURL).Debug("opened PR")
231+
logger.WithField("prURL", res.PullRequestURL).Debug("opened PR")
250232
res.ActionTaken = ActionTakenOpenedPR
251-
res.PullRequestURL = *pr.HTMLURL
252233
} else {
253234
res.ActionTaken = ActionTakenPushedDirectly
254235
res.CommitID = commitID
@@ -325,15 +306,6 @@ func (s *service) switchToCommitBranch(
325306
return commitBranch, nil
326307
}
327308

328-
func parseGitHubURL(url string) (string, string, error) {
329-
regex := regexp.MustCompile(`^https\://github\.com/([\w-]+)/([\w-]+).*`)
330-
parts := regex.FindStringSubmatch(url)
331-
if len(parts) != 3 {
332-
return "", "", errors.Errorf("error parsing github repository URL %q", url)
333-
}
334-
return parts[1], parts[2], nil
335-
}
336-
337309
// checkoutSourceCommit examines a RenderRequest and determines if it is
338310
// requesting to render configuration from a specific commit. If not, it returns
339311
// the ID of the most recent commit.

0 commit comments

Comments
 (0)