/
github.go
157 lines (141 loc) · 4.53 KB
/
github.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright © 2019 Matt Konda <mkonda@jemurai.com>
//
package cmd
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-github/github"
"golang.org/x/oauth2"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// githubCmd represents the github command
var githubCmd = &cobra.Command{
Use: "github",
Short: "Get information from GitHub",
Long: `Pull information out of GitHub as part of a user audit.`,
Run: func(cmd *cobra.Command, args []string) {
public := viper.GetString("public")
org := viper.GetString("github-org")
user := viper.GetString("github-user")
githubToken := viper.GetString("github-token")
fmt.Println("github called with", org, " and ", user, " and ", public)
client := getGithubClient(githubToken)
if org != "" {
getOrgUsers(org, client)
getOrgRepos(org, client)
getOrgUserRepos(org, client)
}
if user != "" {
getUserRepos(user, client)
}
},
}
func getUserRepos(user string, client *github.Client) {
fmt.Println("----------------------------------------------------------------")
fmt.Println("Getting", user, " repositories:")
opt := &github.RepositoryListOptions{}
ctx := context.Background()
repos, _, err := client.Repositories.List(ctx, user, opt)
if err != nil {
fmt.Println(err)
}
for i := 0; i < len(repos); i++ {
visitRepo(*repos[i])
}
}
func getOrgRepos(org string, client *github.Client) {
fmt.Println("----------------------------------------------------------------")
fmt.Println("Getting", org, " repositories:")
// opt := &github.RepositoryListByOrgOptions{Type: "public"}
opt := &github.RepositoryListByOrgOptions{}
ctx := context.Background()
repos, _, err := client.Repositories.ListByOrg(ctx, org, opt)
if err != nil {
fmt.Println(err)
}
for i := 0; i < len(repos); i++ {
visitRepo(*repos[i])
}
}
func getOrgUserRepos(org string, client *github.Client) {
fmt.Println("Getting", org, " user repositories:")
opt := &github.ListMembersOptions{}
ctx := context.Background()
users, _, err := client.Organizations.ListMembers(ctx, org, opt)
if err != nil {
fmt.Println(err)
}
fmt.Println("Handling", len(users), " users from ", org)
for j := 0; j < len(users); j++ {
fmt.Println("----------------------------------------------------------------")
fmt.Println(*users[j].Login)
getUserRepos(*users[j].Login, client)
}
}
func getOrgUsers(org string, client *github.Client) {
fmt.Println("Getting", org, " users:")
opt := &github.ListMembersOptions{}
ctx := context.Background()
users, _, err := client.Organizations.ListMembers(ctx, org, opt)
if err != nil {
fmt.Println(err)
}
fmt.Println("Handling", len(users), " users from ", org)
for j := 0; j < len(users); j++ {
visitGithubUser(*users[j])
}
}
func visitGithubUser(user github.User) {
m := map[string]interface{}{
"login": *user.Login,
"url": *user.URL,
}
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
fmt.Println("error:", err)
}
fmt.Print(string(b))
// fmt.Println(*repo.Name, "\t\t\t", *repo.Private, "\t", *repo.UpdatedAt, "\t", *repo.CloneURL)
}
func visitRepo(repo github.Repository) {
m := map[string]interface{}{"name": *repo.Name,
"url": *repo.CloneURL,
"owner": *repo.Owner.Login,
"update": *repo.UpdatedAt,
"create": *repo.CreatedAt,
"private": *repo.Private,
}
b, err := json.MarshalIndent(m, "", " ")
if err != nil {
fmt.Println("error:", err)
}
fmt.Print(string(b))
// fmt.Println(*repo.Name, "\t\t\t", *repo.Private, "\t", *repo.UpdatedAt, "\t", *repo.CloneURL)
}
func init() {
rootCmd.AddCommand(githubCmd)
githubCmd.Flags().String("github-org", "", "The organization to audit.")
githubCmd.Flags().String("github-user", "", "The user to audit.")
githubCmd.Flags().String("github-token", "", "Github token")
githubCmd.Flags().String("github-public", "", "Should the output only include public repos?")
viper.BindPFlag("github-org", githubCmd.Flags().Lookup("github-org"))
viper.BindPFlag("github-user", githubCmd.Flags().Lookup("github-user"))
viper.BindPFlag("github-token", githubCmd.Flags().Lookup("github-token"))
viper.BindPFlag("github-public", githubCmd.Flags().Lookup("github-public"))
}
func getGithubClient(token string) *github.Client {
if token == "" {
fmt.Println("Warning: empty token so searching public.")
githubClient := github.NewClient(nil)
return githubClient
}
// If the token is defined, get an OAuth client.
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
oauth2Client := oauth2.NewClient(context.Background(), ts)
githubClient := github.NewClient(oauth2Client)
return githubClient
}