-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (146 loc) · 3.87 KB
/
main.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
_ "embed"
"fmt"
"github.com/shurcooL/graphql"
"log"
"os"
"strings"
"github.com/cli/go-gh"
"github.com/cli/go-gh/pkg/api"
"github.com/spf13/cobra"
)
//go:embed version.txt
var Version string
func init() {
cobra.OnInitialize()
}
// NewCommand is the root command of gh-vanity.
func NewCommand() *cobra.Command {
var (
company string
employee bool
owner string
name string
)
var command = &cobra.Command{
Use: "gh-vanity",
Short: "Show who's starred a repository and what company(s) they worked for.",
DisableAutoGenTag: true,
Version: Version,
RunE: func(cmd *cobra.Command, args []string) error {
client, err := gh.GQLClient(nil)
if err != nil {
return nil
}
companies := strings.Split(company, ",")
err = getStargazers(companies, employee, owner, name, client)
if err != nil {
return nil
}
return nil
},
}
command.Flags().StringVarP(&company, "company", "c", "", "Filter stargazers by company name(s). Can be comma separated. If no names are given, then all stargazers will output.")
command.Flags().BoolVarP(&employee, "employee", "e", false, "Filter stargazers that are GitHub employees.")
command.Flags().StringVarP(&name, "name", "n", "", "The name of the GitHub repository.")
command.Flags().StringVarP(&owner, "owner", "o", "", "The owner or organization of the GitHub repository.")
err := command.MarkFlagRequired("name")
if err != nil {
return nil
}
err = command.MarkFlagRequired("owner")
if err != nil {
return nil
}
return command
}
type userNode struct {
Company graphql.String
IsEmployee graphql.Boolean
Name graphql.String
URL graphql.String
Organizations struct {
Edges []struct {
Node struct {
Name graphql.String
}
}
} `graphql:"organizations(first: 10)"`
}
func filterStargazers(companies []string, employee bool, u userNode) bool {
company := strings.ReplaceAll(string(u.Company), "@", "")
company = strings.TrimSpace(company)
company = strings.ToLower(company)
for _, v := range companies {
if v == company || v == "" {
return true
}
}
if employee {
return bool(u.IsEmployee)
}
return false
}
func getOrganizations(user userNode) []string {
var organizations []string
if len(user.Organizations.Edges) > 0 {
for _, o := range user.Organizations.Edges {
organizations = append(organizations, string(o.Node.Name))
}
}
return organizations
}
func getStargazers(companies []string, employee bool, owner string, name string, client api.GQLClient) error {
var query struct {
Repository struct {
Stargazers struct {
TotalCount graphql.Int
Edges []struct {
Node userNode
}
PageInfo struct {
EndCursor graphql.String
HasNextPage graphql.Boolean
}
} `graphql:"stargazers(first: 100, after: $cursor)"`
} `graphql:"repository(name: $name, owner: $owner)"`
}
variables := map[string]interface{}{
"name": graphql.String(name),
"owner": graphql.String(owner),
"cursor": (*graphql.String)(nil),
}
once := false
for {
err := client.Query("Stargazers", &query, variables)
if err != nil {
log.Fatal(err)
}
stargazers := query.Repository.Stargazers
if !once && stargazers.TotalCount > 1000 {
fmt.Println("warning: there are more than 1000 stargazers. this may take a while :)")
once = true
}
for _, v := range stargazers.Edges {
user := v.Node
if !filterStargazers(companies, employee, user) {
continue
}
organizations := strings.Join(getOrganizations(user), ", ")
fmt.Printf("[%s] %s (%s): %s\n", user.Company, user.Name, user.URL, organizations)
}
if !stargazers.PageInfo.HasNextPage {
break
}
variables["cursor"] = stargazers.PageInfo.EndCursor
}
return nil
}
func main() {
command := NewCommand()
if err := command.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}