@@ -3,9 +3,7 @@ package git
3
3
import (
4
4
"bufio"
5
5
"context"
6
- "encoding/json"
7
6
"fmt"
8
- "io"
9
7
"io/fs"
10
8
"net/http"
11
9
"net/url"
@@ -14,7 +12,6 @@ import (
14
12
"path/filepath"
15
13
"strings"
16
14
"sync"
17
- "time"
18
15
19
16
"github.com/pkg/errors"
20
17
"github.com/rs/zerolog/log"
@@ -23,7 +20,6 @@ import (
23
20
24
21
"github.com/zapier/kubechecks/pkg"
25
22
"github.com/zapier/kubechecks/pkg/config"
26
- "github.com/zapier/kubechecks/pkg/vcs"
27
23
"github.com/zapier/kubechecks/telemetry"
28
24
)
29
25
@@ -213,7 +209,7 @@ func (r *Repo) MergeIntoTarget(ctx context.Context, ref string) error {
213
209
attribute .String ("sha" , ref ),
214
210
))
215
211
defer span .End ()
216
- merge_command := []string {"merge" , ref }
212
+ mergeCommand := []string {"merge" , ref }
217
213
// For shallow clones, we need to pull the ref into the repo
218
214
if r .Shallow {
219
215
ref = strings .TrimPrefix (ref , "origin/" )
@@ -227,10 +223,10 @@ func (r *Repo) MergeIntoTarget(ctx context.Context, ref string) error {
227
223
// When merging shallow clones, we need to allow unrelated histories
228
224
// and use the "theirs" strategy to avoid conflicts
229
225
// cons of this is that it may not be entirely accurate and may overwrite changes in the target branch
230
- merge_command = []string {"merge" , ref , "--allow-unrelated-histories" , "-X" , "theirs" }
226
+ mergeCommand = []string {"merge" , ref , "--allow-unrelated-histories" , "-X" , "theirs" }
231
227
}
232
228
233
- cmd := r .execGitCommand (merge_command ... )
229
+ cmd := r .execGitCommand (mergeCommand ... )
234
230
out , err := cmd .CombinedOutput ()
235
231
if err != nil {
236
232
telemetry .SetError (span , err , "merge commit into branch" )
@@ -298,10 +294,9 @@ func censorVcsToken(cfg config.ServerConfig, args []string) []string {
298
294
}
299
295
300
296
// SetCredentials ensures Git auth is set up for cloning
301
- func SetCredentials (cfg config.ServerConfig , vcsClient vcs.Client ) error {
302
- email := vcsClient .Email ()
303
- username := vcsClient .Username ()
304
- cloneUsername := vcsClient .CloneUsername ()
297
+ func SetCredentials (ctx context.Context , cfg config.ServerConfig , email , username , cloneUrl string ) error {
298
+ _ , span := tracer .Start (ctx , "SetCredentials" )
299
+ defer span .End ()
305
300
306
301
cmd := execCommand (cfg , "git" , "config" , "--global" , "user.email" , email )
307
302
err := cmd .Run ()
@@ -315,14 +310,6 @@ func SetCredentials(cfg config.ServerConfig, vcsClient vcs.Client) error {
315
310
return errors .Wrap (err , "failed to set git user name" )
316
311
}
317
312
318
- httpClient := & http.Client {
319
- Timeout : 30 * time .Second ,
320
- }
321
- cloneUrl , err := getCloneUrl (cloneUsername , cfg , httpClient )
322
- if err != nil {
323
- return errors .Wrap (err , "failed to get clone url" )
324
- }
325
-
326
313
homedir , err := os .UserHomeDir ()
327
314
if err != nil {
328
315
return errors .Wrap (err , "unable to get home directory" )
@@ -350,73 +337,17 @@ func SetCredentials(cfg config.ServerConfig, vcsClient vcs.Client) error {
350
337
return nil
351
338
}
352
339
353
- func getCloneUrl (user string , cfg config.ServerConfig , httpClient HTTPClient ) (string , error ) {
354
- vcsBaseUrl := cfg .VcsBaseUrl
355
- vcsType := cfg .VcsType
356
- vcsToken := cfg .VcsToken
357
-
340
+ func BuildCloneURL (baseURL , user , password string ) (string , error ) {
358
341
var hostname , scheme string
359
342
360
- if vcsBaseUrl == "" {
361
- // hack: but it does happen to work for now
362
- hostname = fmt .Sprintf ("%s.com" , vcsType )
363
- scheme = "https"
364
- } else {
365
- parts , err := url .Parse (vcsBaseUrl )
366
- if err != nil {
367
- return "" , errors .Wrapf (err , "failed to parse %q" , vcsBaseUrl )
368
- }
369
- hostname = parts .Host
370
- scheme = parts .Scheme
371
- }
372
-
373
- if cfg .IsGithubApp () {
374
- stringAppId := fmt .Sprintf ("%d" , cfg .GithubAppID )
375
- jwt , err := pkg .CreateJWT (cfg .GithubPrivateKey , stringAppId )
376
- if err != nil {
377
- return "" , errors .Wrapf (err , "failed to create jwt" )
378
- }
379
- url := fmt .Sprintf ("https://api.github.com/app/installations/%d/access_tokens" , cfg .GithubInstallationID )
380
-
381
- req , err := http .NewRequest (http .MethodPost , url , nil )
382
- if err != nil {
383
- return "" , errors .Wrapf (err , "failed to create request" )
384
- }
385
- req .Header .Add ("Accept" , "application/vnd.github.v3+json" )
386
- req .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , jwt ))
387
-
388
- resp , err := httpClient .Do (req )
389
- if err != nil {
390
- return "" , errors .Wrapf (err , "failed to get response" )
391
- }
392
- defer func () {
393
- if closeErr := resp .Body .Close (); closeErr != nil {
394
- log .Warn ().Err (closeErr ).Msg ("failed to close response body" )
395
- }
396
- }()
397
-
398
- body , err := io .ReadAll (resp .Body )
399
- if err != nil {
400
- return "" , errors .Wrapf (err , "failed to read response" )
401
- }
402
-
403
- var result interface {}
404
- err = json .Unmarshal (body , & result )
405
- if err != nil {
406
- return "" , errors .Wrapf (err , "failed to unmarshal response" )
407
- }
408
-
409
- data , ok := result .(map [string ]interface {})
410
- if ! ok {
411
- return "" , errors .New ("failed to convert response to map" )
412
- }
413
-
414
- if token , ok := data ["token" ].(string ); ok {
415
- vcsToken = token
416
- }
343
+ parts , err := url .Parse (baseURL )
344
+ if err != nil {
345
+ return "" , errors .Wrapf (err , "failed to parse %q" , baseURL )
417
346
}
347
+ hostname = parts .Host
348
+ scheme = parts .Scheme
418
349
419
- return fmt .Sprintf ("%s://%s:%s@%s" , scheme , user , vcsToken , hostname ), nil
350
+ return fmt .Sprintf ("%s://%s:%s@%s" , scheme , user , password , hostname ), nil
420
351
}
421
352
422
353
// GetListOfChangedFiles returns a list of files that have changed between the current branch and the target branch
0 commit comments