Skip to content

Commit

Permalink
Merge pull request #42 from tomtwinkle/fix/git-clone-use-token-2
Browse files Browse the repository at this point in the history
fix: git clone use token url
  • Loading branch information
tomtwinkle authored Aug 16, 2023
2 parents 30fa8fb + 01ed0bb commit db4ef67
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions internal/pkg/gh/gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import (

"github.com/go-git/go-billy/v5/memfs"

"github.com/go-git/go-git/v5/plumbing/transport/http"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
Expand Down Expand Up @@ -72,13 +70,13 @@ func New(ctx context.Context, token string, logger *slog.Logger) (Github, error)

func NewWithConfig(ctx context.Context, token string, remoteConfig RemoteConfig) (Github, error) {
f := memfs.New()
r, err := git.CloneContext(ctx, memory.NewStorage(), f, &git.CloneOptions{
Progress: os.Stdout,
Auth: &http.TokenAuth{
Token: token,
},
URL: fmt.Sprintf("https://%s:[email protected]/%s/%s", token, remoteConfig.Owner, remoteConfig.Repo),
})
options := &git.CloneOptions{
URL: getURL(token, remoteConfig),
}
if err := options.Validate(); err != nil {
return nil, err
}
r, err := git.CloneContext(ctx, memory.NewStorage(), f, options)
if err != nil {
return nil, err
}
Expand All @@ -99,6 +97,13 @@ func NewWithConfig(ctx context.Context, token string, remoteConfig RemoteConfig)
}, nil
}

func getURL(token string, remoteConfig RemoteConfig) string {
if token == "" {
return fmt.Sprintf("https://github.com/%s/%s", remoteConfig.Owner, remoteConfig.Repo)
}
return fmt.Sprintf("https://%s:[email protected]/%s/%s", token, remoteConfig.Owner, remoteConfig.Repo)
}

func gitRemoteConfig(logger *slog.Logger) (*gitConfig, error) {
cnf, err := gitRemoteConfigDir()
if err != nil {
Expand Down

0 comments on commit db4ef67

Please sign in to comment.