Skip to content

Commit

Permalink
fix: github repository fetch for both user and organization (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
isindir authored Dec 28, 2021
1 parent 85add24 commit ff4bed4
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
GO := GOPROXY=https://proxy.golang.org go

VERSION:="0.0.7"
VERSION:="0.0.8"
EXE:="git-get"
BUILD:=`git rev-parse --short HEAD`
TIME:=`date`
Expand Down
80 changes: 76 additions & 4 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ func CreateRepository(
) *github.Repository {
git := githubAuth(ctx, repositorySha)
isPrivate := true

if mirrorVisibilityMode == "public" {
isPrivate = false
}

repoDef := &github.Repository{
Name: github.String(repository),
Private: github.Bool(isPrivate),
Expand All @@ -86,12 +88,52 @@ func CreateRepository(
return resultingRepository
}

func FetchOwnerRepos(
func fetchOrgRepos(
ctx context.Context,
git *github.Client,
repoSha, owner string,
) []*github.Repository {
var repoList []*github.Repository

opts := &github.RepositoryListByOrgOptions{
ListOptions: github.ListOptions{
Page: 0,
},
}

for {
repos, res, err := git.Repositories.ListByOrg(ctx, owner, opts)
log.Debugf(
"%s: NextPage/PrevPage/FirstPage/LastPage '%d/%d/%d/%d'\n",
repoSha, res.NextPage, res.PrevPage, res.FirstPage, res.LastPage)
for repo := 0; repo < len(repos); repo++ {
log.Debugf("%s: (%d) Repo FullName '%s'", repoSha, opts.ListOptions.Page, *repos[repo].FullName)
}
repoList = append(repoList, repos...)
log.Debugf("%s: Found '%d' repositories owned by '%s'", repoSha, len(repos), owner)

opts.ListOptions = github.ListOptions{
Page: res.NextPage,
}

if res.NextPage == 0 {
break
}

if err != nil {
log.Debugf("%s: Error fetching repositories for '%s': %+v\n", repoSha, owner, err)
break
}
}

return repoList
}

func fetchUserRepos(
ctx context.Context,
git *github.Client,
repoSha, owner, githubVisibility, githubAffiliation string,
) []*github.Repository {
log.Debugf("%s: Specified owner: '%s'", repoSha, owner)
git := githubAuth(ctx, repoSha)
var repoList []*github.Repository

opts := &github.RepositoryListOptions{
Expand All @@ -107,7 +149,7 @@ func FetchOwnerRepos(
}

for {
repos, res, err := git.Repositories.List(ctx, owner, opts)
repos, res, err := git.Repositories.List(ctx, "", opts)
log.Debugf(
"%s: NextPage/PrevPage/FirstPage/LastPage '%d/%d/%d/%d'\n",
repoSha, res.NextPage, res.PrevPage, res.FirstPage, res.LastPage)
Expand All @@ -133,3 +175,33 @@ func FetchOwnerRepos(

return repoList
}

func FetchOwnerRepos(
ctx context.Context,
repoSha, owner, githubVisibility, githubAffiliation string,
) []*github.Repository {
log.Debugf("%s: Specified owner: '%s'", repoSha, owner)
git := githubAuth(ctx, repoSha)
var repoList []*github.Repository
var userType string

user, _, err := git.Users.Get(ctx, owner)
if err != nil {
log.Debugf("%s: Owner '%s' not found: '%+v'", repoSha, owner, err)
} else {
log.Debugf("%s: Owner '%s', Type: '%s'", repoSha, owner, *user.Type)
userType = *user.Type
}

switch userType {
case "Organization":
repoList = fetchOrgRepos(ctx, git, repoSha, owner)
case "User":
repoList = fetchUserRepos(ctx, git, repoSha, owner, githubVisibility, githubAffiliation)
default:
log.Fatalf("%s: Error: unknown '%s' user type", repoSha, userType)
os.Exit(1)
}

return repoList
}

0 comments on commit ff4bed4

Please sign in to comment.