mirror of
https://github.com/gabrie30/ghorg.git
synced 2025-08-06 14:27:28 +02:00
93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
|
|
"github.com/google/go-github/github"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
func getGitHubOrgCloneUrls() ([]string, error) {
|
|
|
|
ctx := context.Background()
|
|
ts := oauth2.StaticTokenSource(
|
|
&oauth2.Token{AccessToken: os.Getenv("GHORG_GITHUB_TOKEN")},
|
|
)
|
|
tc := oauth2.NewClient(ctx, ts)
|
|
client := github.NewClient(tc)
|
|
|
|
opt := &github.RepositoryListByOrgOptions{
|
|
Type: "all",
|
|
ListOptions: github.ListOptions{PerPage: 100, Page: 0},
|
|
}
|
|
|
|
// get all pages of results
|
|
var allRepos []*github.Repository
|
|
for {
|
|
repos, resp, err := client.Repositories.ListByOrg(context.Background(), args[0], opt)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
allRepos = append(allRepos, repos...)
|
|
if resp.NextPage == 0 {
|
|
break
|
|
}
|
|
opt.Page = resp.NextPage
|
|
}
|
|
cloneUrls := []string{}
|
|
|
|
for _, repo := range allRepos {
|
|
if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" {
|
|
cloneUrls = append(cloneUrls, *repo.CloneURL)
|
|
} else {
|
|
cloneUrls = append(cloneUrls, *repo.SSHURL)
|
|
}
|
|
}
|
|
|
|
return cloneUrls, nil
|
|
}
|
|
|
|
// TODO: refactor with getAllOrgCloneUrls
|
|
func getGitHubUserCloneUrls() ([]string, error) {
|
|
ctx := context.Background()
|
|
|
|
ts := oauth2.StaticTokenSource(
|
|
&oauth2.Token{AccessToken: os.Getenv("GHORG_GITHUB_TOKEN")},
|
|
)
|
|
tc := oauth2.NewClient(ctx, ts)
|
|
client := github.NewClient(tc)
|
|
|
|
opt := &github.RepositoryListOptions{
|
|
Type: "all",
|
|
ListOptions: github.ListOptions{PerPage: 100, Page: 0},
|
|
}
|
|
|
|
// get all pages of results
|
|
var allRepos []*github.Repository
|
|
for {
|
|
repos, resp, err := client.Repositories.List(context.Background(), args[0], opt)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
allRepos = append(allRepos, repos...)
|
|
if resp.NextPage == 0 {
|
|
break
|
|
}
|
|
opt.Page = resp.NextPage
|
|
}
|
|
cloneUrls := []string{}
|
|
|
|
for _, repo := range allRepos {
|
|
if os.Getenv("GHORG_CLONE_PROTOCOL") == "https" {
|
|
cloneUrls = append(cloneUrls, *repo.CloneURL)
|
|
} else {
|
|
cloneUrls = append(cloneUrls, *repo.SSHURL)
|
|
}
|
|
}
|
|
|
|
return cloneUrls, nil
|
|
}
|