-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
178 lines (152 loc) · 3.82 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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"crypto/tls"
"github.com/machinebox/graphql"
)
var query = `
query ($userName: String!, $id: ID) {
user(login: $userName) {
repositoriesContributedTo(
includeUserRepositories: true
contributionTypes: COMMIT
first: 100
) {
pageInfo {
hasNextPage
endCursor
}
nodes {
refs(first: 100, refPrefix: "refs/") {
nodes {
target {
... on Commit {
history(author: { id: $id }) {
pageInfo {
hasNextPage
endCursor
}
nodes {
author {
email
name
}
}
}
}
}
}
}
}
}
}
}
`
func main() {
username := flag.String("user", "", "(REQUIRED) Username of the target github account")
printsource := flag.Bool("source", false, "Print commit URLs alongside discovered identities")
showall := flag.Bool("all", false, "Print all commits (will repeat duplicate identities)")
flagtoken := flag.String("token", "", "Github API Bearer token (can also be set from the GH_TOKEN env variable)")
flag.Parse()
if *username == "" {
flag.PrintDefaults()
os.Exit(0)
}
var token = ""
if *flagtoken == "" {
token = os.Getenv("GH_TOKEN")
} else {
token = *flagtoken
}
if token == "" {
fmt.Println("Github token missing. Please generate one and set it through the -token flag or the GH_TOKEN environment variable")
os.Exit(0)
}
http.DefaultTransport.(*http.Transport).TLSClientConfig = &tls.Config{InsecureSkipVerify: true}
userreq, err := http.NewRequest("GET", fmt.Sprintf("http://api.github.com/users/%s", *username), nil)
if err != nil {
panic(err)
}
userreq.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
httpclient := http.Client{}
res, err := httpclient.Do(userreq)
if err != nil {
fmt.Printf("Error fetching user ID: %s\n", err)
os.Exit(1)
}
if res.StatusCode == 401 {
fmt.Println("Your Github token seems to be invalid.")
os.Exit(1)
}
var userres struct {
NodeID string `json:"node_id"`
}
err = json.NewDecoder(res.Body).Decode(&userres)
if err != nil {
fmt.Printf("Error parsing github api response: %s\n", err)
os.Exit(1)
}
userid := userres.NodeID
client := graphql.NewClient("https://api.github.com/graphql")
req := graphql.NewRequest(query)
req.Var("userName", username)
req.Var("id", userid)
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", token))
var respData struct {
User struct {
RepositoriesContributedTo struct {
PageInfo struct {
HasNextPage bool
EndCursor string
}
Nodes []struct {
Refs struct { //
Nodes []struct {
Target struct {
History struct {
PageInfo struct {
HasNextPage bool
EndCursor string
}
Nodes []struct {
CommitURL string
Author struct {
Email string
Name string
}
}
}
}
}
}
}
}
}
}
err = client.Run(context.Background(), req, &respData)
if err != nil {
log.Fatalf("Failed to execute request: %v", err)
}
unique := make(map[string]bool)
for _, repo := range respData.User.RepositoriesContributedTo.Nodes {
for _, ref := range repo.Refs.Nodes {
for _, commit := range ref.Target.History.Nodes {
identity := fmt.Sprintf("%s <%s>", commit.Author.Name, commit.Author.Email)
if _, exists := unique[identity]; *showall || !exists {
unique[identity] = true
if *printsource {
fmt.Printf("%s - %s\n", identity, commit.CommitURL)
} else {
fmt.Println(identity)
}
}
}
}
}
}