@@ -6,18 +6,16 @@ import (
6
6
"fmt"
7
7
"os"
8
8
"path/filepath"
9
- "regexp"
10
9
"strings"
11
10
12
11
"github.com/ghodss/yaml"
13
- "github.com/google/go-github/v47/github"
14
12
"github.com/pkg/errors"
15
13
uuid "github.com/satori/go.uuid"
16
14
log "github.com/sirupsen/logrus"
17
- "golang.org/x/oauth2"
18
15
19
16
"github.com/akuityio/bookkeeper/internal/config"
20
17
"github.com/akuityio/bookkeeper/internal/git"
18
+ "github.com/akuityio/bookkeeper/internal/github"
21
19
"github.com/akuityio/bookkeeper/internal/metadata"
22
20
)
23
21
@@ -205,23 +203,7 @@ func (s *service) RenderConfig(
205
203
Debug ("pushed fully-rendered configuration" )
206
204
207
205
// 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 {
225
207
commitMsgParts := strings .SplitN (commitMsg , "\n " , 2 )
226
208
// PR title is just the first line of the commit message
227
209
prTitle := fmt .Sprintf ("%s --> %s" , commitMsgParts [0 ], req .TargetBranch )
@@ -230,25 +212,24 @@ func (s *service) RenderConfig(
230
212
if len (commitMsgParts ) == 2 {
231
213
prBody = strings .TrimSpace (commitMsgParts [1 ])
232
214
}
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 (
235
217
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 ,
244
226
},
245
227
); err != nil {
246
228
return res ,
247
229
errors .Wrap (err , "error opening pull request to the target branch" )
248
230
}
249
- logger .WithField ("prURL" , * pr . HTMLURL ).Debug ("opened PR" )
231
+ logger .WithField ("prURL" , res . PullRequestURL ).Debug ("opened PR" )
250
232
res .ActionTaken = ActionTakenOpenedPR
251
- res .PullRequestURL = * pr .HTMLURL
252
233
} else {
253
234
res .ActionTaken = ActionTakenPushedDirectly
254
235
res .CommitID = commitID
@@ -325,15 +306,6 @@ func (s *service) switchToCommitBranch(
325
306
return commitBranch , nil
326
307
}
327
308
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
-
337
309
// checkoutSourceCommit examines a RenderRequest and determines if it is
338
310
// requesting to render configuration from a specific commit. If not, it returns
339
311
// the ID of the most recent commit.
0 commit comments