Skip to content

Commit

Permalink
Fix #22: Handle pagination for organisation/user 馃搫
Browse files Browse the repository at this point in the history
Current page can be set via a new '--page' flag (default: 1):

    $ gh collab-scanner --org softwarevidal --page 2
    $ gh collab-scanner --user nicokosi --page 2

Page size is hard-coded to 100.

PS:

- I could not retrieve pagination info (page x on y) because
of this limitation 馃槶:
cli/go-gh#23

- I tried to guess current page via Exec for
"gh repo list $user --json id --limit 1000" but response time
was very slow. 馃悓
See https://github.com/cli/go-gh/blob/trunk/example_gh_test.go#L16
  • Loading branch information
nicokosi committed Mar 14, 2022
1 parent d66f36f commit 14fa20b
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions main.go
Expand Up @@ -4,6 +4,7 @@ import (
"flag"
"fmt"
"os"
"strconv"
"strings"

"github.com/cli/go-gh"
Expand All @@ -13,16 +14,18 @@ type config struct {
repo string
org string
user string
page int
verbose bool
}

func parseFlags() config {
repo := flag.String("repo", "", "a optional GitHub repository (i.e. 'python/peps') ; use repo for current folder if omitted and no 'org' nor 'user' flag")
org := flag.String("org", "", "a optional GitHub organization (i.e. 'python') to scan the repositories from (100 max) ; use repo for current folder if omitted and no 'repo' nor 'user' flag")
user := flag.String("user", "", "a optional GitHub user (i.e. 'torvalds') to scan the repositories from (100 max); use repo for current folder if omitted and no 'repo' nor 'org' flag")
page := flag.Int("page", 1, "Page number for 'repo' and 'user' flags, 100 repositories per page")
verbose := flag.Bool("verbose", false, "mode that outputs several lines (otherwise, outputs a one-liner) ; default: false")
flag.Parse()
return config{*repo, *org, *user, *verbose}
return config{*repo, *org, *user, *page, *verbose}
}

type owner struct{ Login string }
Expand Down Expand Up @@ -96,14 +99,14 @@ func getRepos(config config) ([]repo, error) {
// https://docs.github.com/en/rest/reference/repos#list-organization-repositories
repos := []repo{}
err = client.Get(
"orgs/"+config.org+"/repos?sort=full_name&per_page=100",
"orgs/"+config.org+"/repos?sort=full_name&per_page=100&page="+strconv.Itoa(config.page),
&repos)
return repos, err
} else {
// https://docs.github.com/en/rest/reference/repos#list-repositories-for-a-user
repos := []repo{}
err = client.Get(
"users/"+config.user+"/repos?sort=full_name&per_page=100",
"users/"+config.user+"/repos?sort=full_name&per_page=100&page="+strconv.Itoa(config.page),
&repos)
return repos, err
}
Expand Down

0 comments on commit 14fa20b

Please sign in to comment.