Skip to content

Commit

Permalink
Issue #18: Scan all organization's repositories
Browse files Browse the repository at this point in the history
Via:

	gh-collab-scanner --org ${org}
  • Loading branch information
nicokosi committed Mar 10, 2022
1 parent 72d830a commit a1ffb2f
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 20 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,22 @@ will display something like:
(current repo)
Repo nicokosi/gh-collab-scanner has: description 鈽戯笍, README 鈽戯笍, topics 鈽戯笍, 1 collaborator 馃懁, community profile score: 33 馃挴

For any GitHub repository, via its full name ${org}/${repo} (i.e. python/peps):
For any GitHub repository, via its full name ${org}/${repo} (i.e. python/peps), use the `--repo` flag:

```sh
gh collab-scanner --repo python/peps
```

will display something like:
It will display something like:

Repo python/peps has: description 鈽戯笍, README 鈽戯笍, no topics 馃槆, community profile score: 71 馃挴

In order to scan all repositories for a given organization, use the `--org` flag:

```sh
gh collab-scanner --org python
```

Need help? Run:

```sh
Expand Down
75 changes: 57 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ import (

type config struct {
repo string
org string
verbose bool
}

func parseFlags() config {
repo := flag.String("repo", "", "a optional GitHub repository (i.e. 'python/peps') ; use repo for current folder if omitted")
repo := flag.String("repo", "", "a optional GitHub repository (i.e. 'python/peps') ; use repo for current folder if omitted and no 'org' 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' flag")
verbose := flag.Bool("verbose", false, "mode that outputs several lines (otherwise, outputs a one-liner) ; default: false")
flag.Parse()
return config{*repo, *verbose}
return config{*repo, *org, *verbose}
}

type owner struct{ Login string }
Expand All @@ -37,26 +39,63 @@ type collaborator struct {

func main() {
config := parseFlags()
repoWithOrg, error := getRepo(config)
if error != nil {
fmt.Println(error)
if strings.Contains(error.Error(), "none of the git remotes configured for this repository point to a known GitHub host") {
println("If current folder is related to a GitHub repository, please check 'gh auth status' and 'gh config list'.")
if len(config.org) > 0 {
repos := []repo{}
repos, error := getRepos(config)
if error != nil {
fmt.Println(error)
os.Exit(2)
}
for _, repo := range repos {
repoWithOrg := config.org + "/" + repo.Name
repoMessage, repo, validRepo := scanRepo(config, repoWithOrg)
if validRepo {
fmt.Printf(repoWithOrg + ": " + repoMessage)
collaboratorsMessage := scanCollaborators(config, repoWithOrg)
fmt.Printf(collaboratorsMessage)
if strings.Compare(repo.Visibility, "public") == 0 {
communityScoreMessage := scanCommunityScore(config, repoWithOrg)
fmt.Printf(communityScoreMessage)
}
}
}
} else if len(config.repo) > 0 {
repoWithOrg, error := getRepo(config)
if error != nil {
fmt.Println(error)
if strings.Contains(error.Error(), "none of the git remotes configured for this repository point to a known GitHub host") {
println("If current folder is related to a GitHub repository, please check 'gh auth status' and 'gh config list'.")
}
os.Exit(1)
}
repoMessage, repo, validRepo := scanRepo(config, repoWithOrg)
if validRepo {
fmt.Printf(repoMessage)
collaboratorsMessage := scanCollaborators(config, repoWithOrg)
fmt.Printf(collaboratorsMessage)
if strings.Compare(repo.Visibility, "public") == 0 {
communityScoreMessage := scanCommunityScore(config, repoWithOrg)
fmt.Printf(communityScoreMessage)
}
}
os.Exit(1)
}
}

repoMessage, repo, validRepo := scanRepo(config, repoWithOrg)
if validRepo {
fmt.Printf(repoMessage)
collaboratorsMessage := scanCollaborators(config, repoWithOrg)
fmt.Printf(collaboratorsMessage)

if strings.Compare(repo.Visibility, "public") == 0 {
communityScoreMessage := scanCommunityScore(config, repoWithOrg)
fmt.Printf(communityScoreMessage)
}
func getRepos(config config) ([]repo, error) {
if len(config.org) < 1 {
return []repo{}, nil
}
client, err := gh.RESTClient(nil)
if err != nil {
fmt.Println(err)
return []repo{}, err
}
// 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",
&repos)
return repos, err
}

func getRepo(config config) (string, error) {
Expand Down
14 changes: 14 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,20 @@ func TestScanRepo(t *testing.T) {
assert.Equal(t, "README 鈽戯笍, topics 鈽戯笍, ", message)
}

func TestGetRepos_for_org(t *testing.T) {
defer gock.Off()
defer gock.DisableNetworking()
gock.New("https://api.github.com").
Get("/orgs/acme/repos").
Reply(200).
File("test_repos.json")

repositories, error := getRepos(config{org: "acme"})

assert.NotEmpty(t, repositories)
assert.Nil(t, error)
}

func TestScanRepo_Verbose(t *testing.T) {
defer gock.Off()
defer gock.DisableNetworking()
Expand Down
8 changes: 8 additions & 0 deletions test_repos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"fullName": "Bug Bunny"
},
{
"fullName": "Coyote"
}
]

0 comments on commit a1ffb2f

Please sign in to comment.