From f8e609d4bfd055874e38178c1f6a9b88576c41ea Mon Sep 17 00:00:00 2001 From: Adam Kobi Date: Thu, 15 Nov 2018 17:58:54 +0200 Subject: [PATCH] added case for when using oauth token for both listing and cloning repos --- github.go | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/github.go b/github.go index 744cd9a2..201259c6 100644 --- a/github.go +++ b/github.go @@ -14,6 +14,7 @@ import ( log "github.com/sirupsen/logrus" "golang.org/x/oauth2" git "gopkg.in/src-d/go-git.v4" + gitHttp "gopkg.in/src-d/go-git.v4/plumbing/transport/http" "gopkg.in/src-d/go-git.v4/storage/memory" ) @@ -177,6 +178,7 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) { repo *git.Repository err error ) + githubToken := os.Getenv("GITHUB_TOKEN") if opts.ExcludeForks && githubRepo.GetFork() { return nil, fmt.Errorf("skipping %s, excluding forks", *githubRepo.Name) } @@ -191,22 +193,38 @@ func cloneGithubRepo(githubRepo *github.Repository) (*RepoDescriptor, error) { if err != nil { return nil, fmt.Errorf("unable to generater owner temp dir: %v", err) } - if sshAuth != nil { + if sshAuth != nil && githubToken == "" { repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{ URL: *githubRepo.SSHURL, Auth: sshAuth, }) + } else if githubToken != "" { + repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{ + URL: *githubRepo.CloneURL, + Auth: &gitHttp.BasicAuth{ + Username: "fakeUsername", // yes, this can be anything except an empty string + Password: githubToken, + }, + }) } else { repo, err = git.PlainClone(fmt.Sprintf("%s/%s", ownerDir, *githubRepo.Name), false, &git.CloneOptions{ URL: *githubRepo.CloneURL, }) } } else { - if sshAuth != nil { + if sshAuth != nil && githubToken == "" { repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ URL: *githubRepo.SSHURL, Auth: sshAuth, }) + } else if githubToken != "" { + repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ + URL: *githubRepo.CloneURL, + Auth: &gitHttp.BasicAuth{ + Username: "fakeUsername", // yes, this can be anything except an empty string + Password: githubToken, + }, + }) } else { repo, err = git.Clone(memory.NewStorage(), nil, &git.CloneOptions{ URL: *githubRepo.CloneURL,