diff --git a/CHANGELOG.md b/CHANGELOG.md index 728febfd..19944326 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,10 @@ CHANGELOG ========= +1.21.0 +---- +- added support for cloning repositories using github api + 1.20.0 ---- - adding gitlab user and group support 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,