From 1b982cec6b887a55d4331f480140339cecafae74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paulo=20Roberto=20Brand=C3=A3o?= Date: Thu, 9 Aug 2018 11:14:12 -0300 Subject: [PATCH] Fetching Organization Private Repos Added function GetRepositoriesFromOrganization for downloading of private repo when target is an Organization using SSH URL --- core/github.go | 220 +++++++++++++++++++++++++++++-------------------- main.go | 8 +- 2 files changed, 136 insertions(+), 92 deletions(-) diff --git a/core/github.go b/core/github.go index a1941701..a32552be 100644 --- a/core/github.go +++ b/core/github.go @@ -1,113 +1,151 @@ package core import ( - "context" + "context" - "github.com/google/go-github/github" + "github.com/google/go-github/github" ) type GithubOwner struct { - Login *string - ID *int64 - Type *string - Name *string - AvatarURL *string - URL *string - Company *string - Blog *string - Location *string - Email *string - Bio *string + Login *string + ID *int64 + Type *string + Name *string + AvatarURL *string + URL *string + Company *string + Blog *string + Location *string + Email *string + Bio *string } type GithubRepository struct { - Owner *string - ID *int64 - Name *string - FullName *string - CloneURL *string - URL *string - DefaultBranch *string - Description *string - Homepage *string + Owner *string + ID *int64 + Name *string + FullName *string + CloneURL *string + URL *string + DefaultBranch *string + Description *string + Homepage *string } func GetUserOrOrganization(login string, client *github.Client) (*GithubOwner, error) { - ctx := context.Background() - user, _, err := client.Users.Get(ctx, login) - if err != nil { - return nil, err - } - return &GithubOwner{ - Login: user.Login, - ID: user.ID, - Type: user.Type, - Name: user.Name, - AvatarURL: user.AvatarURL, - URL: user.HTMLURL, - Company: user.Company, - Blog: user.Blog, - Location: user.Location, - Email: user.Email, - Bio: user.Bio, - }, nil + ctx := context.Background() + user, _, err := client.Users.Get(ctx, login) + if err != nil { + return nil, err + } + return &GithubOwner{ + Login: user.Login, + ID: user.ID, + Type: user.Type, + Name: user.Name, + AvatarURL: user.AvatarURL, + URL: user.HTMLURL, + Company: user.Company, + Blog: user.Blog, + Location: user.Location, + Email: user.Email, + Bio: user.Bio, + }, nil } func GetRepositoriesFromOwner(login *string, client *github.Client) ([]*GithubRepository, error) { - var allRepos []*GithubRepository - loginVal := *login - ctx := context.Background() - opt := &github.RepositoryListOptions{ - Type: "sources", - } + var allRepos []*GithubRepository + loginVal := *login + ctx := context.Background() + opt := &github.RepositoryListOptions{ + Type: "sources", + } - for { - repos, resp, err := client.Repositories.List(ctx, loginVal, opt) - if err != nil { - return allRepos, err - } - for _, repo := range repos { - if !*repo.Fork { - r := GithubRepository{ - Owner: repo.Owner.Login, - ID: repo.ID, - Name: repo.Name, - FullName: repo.FullName, - CloneURL: repo.CloneURL, - URL: repo.HTMLURL, - DefaultBranch: repo.DefaultBranch, - Description: repo.Description, - Homepage: repo.Homepage, - } - allRepos = append(allRepos, &r) - } - } - if resp.NextPage == 0 { - break - } - opt.Page = resp.NextPage - } + for { + repos, resp, err := client.Repositories.List(ctx, loginVal, opt) + if err != nil { + return allRepos, err + } + for _, repo := range repos { + if !*repo.Fork { + r := GithubRepository{ + Owner: repo.Owner.Login, + ID: repo.ID, + Name: repo.Name, + FullName: repo.FullName, + CloneURL: repo.CloneURL, + URL: repo.HTMLURL, + DefaultBranch: repo.DefaultBranch, + Description: repo.Description, + Homepage: repo.Homepage, + } + allRepos = append(allRepos, &r) + } + } + if resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } - return allRepos, nil + return allRepos, nil +} + +func GetRepositoriesFromOrganization(login *string, client *github.Client) ([]*GithubRepository, error) { + var allRepos []*GithubRepository + loginVal := *login + ctx := context.Background() + opt := &github.RepositoryListByOrgOptions{ + Type: "sources", + } + + for { + repos, resp, err := client.Repositories.ListByOrg(ctx, loginVal, opt) + if err != nil { + return allRepos, err + } + for _, repo := range repos { + if !*repo.Fork { + r := GithubRepository{ + Owner: repo.Owner.Login, + ID: repo.ID, + Name: repo.Name, + FullName: repo.FullName, + CloneURL: repo.SSHURL, + URL: repo.HTMLURL, + DefaultBranch: repo.DefaultBranch, + Description: repo.Description, + Homepage: repo.Homepage, + } + allRepos = append(allRepos, &r) + } + } + if resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } + + return allRepos, nil } func GetOrganizationMembers(login *string, client *github.Client) ([]*GithubOwner, error) { - var allMembers []*GithubOwner - loginVal := *login - ctx := context.Background() - opt := &github.ListMembersOptions{} - for { - members, resp, err := client.Organizations.ListMembers(ctx, loginVal, opt) - if err != nil { - return allMembers, err - } - for _, member := range members { - allMembers = append(allMembers, &GithubOwner{Login: member.Login, ID: member.ID, Type: member.Type}) - } - if resp.NextPage == 0 { - break - } - opt.Page = resp.NextPage - } - return allMembers, nil + var allMembers []*GithubOwner + loginVal := *login + ctx := context.Background() + opt := &github.ListMembersOptions{} + for { + members, resp, err := client.Organizations.ListMembers(ctx, loginVal, opt) + if err != nil { + return allMembers, err + } + for _, member := range members { + allMembers = append(allMembers, &GithubOwner{Login: member.Login, ID: member.ID, Type: member.Type}) + } + if resp.NextPage == 0 { + break + } + opt.Page = resp.NextPage + } + return allMembers, nil } diff --git a/main.go b/main.go index a693ad89..26020505 100644 --- a/main.go +++ b/main.go @@ -57,12 +57,18 @@ func GatherRepositories(sess *core.Session) { for i := 0; i < threadNum; i++ { go func() { for { + var repos []*core.GithubRepository + var err error target, ok := <-ch if !ok { wg.Done() return } - repos, err := core.GetRepositoriesFromOwner(target.Login, sess.GithubClient) + if *target.Type == "Organization" { + repos, err = core.GetRepositoriesFromOrganization(target.Login, sess.GithubClient) + } else { + repos, err = core.GetRepositoriesFromOwner(target.Login, sess.GithubClient) + } if err != nil { sess.Out.Error(" Failed to retrieve repositories from %s: %s\n", *target.Login, err) }