-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
405 lines (337 loc) · 9.6 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
package main
import (
"context"
"fmt"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
gitHttp "github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v65/github"
"github.com/tidwall/gjson"
"golang.org/x/oauth2"
"hash/fnv"
"io"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
func Hello(name string) string {
return "Hello " + name
}
type Reviewer struct {
Username string
Name string
Email string
}
type Review struct {
users []Reviewer
hash string
processed bool
}
var client *github.Client
var reviews []Review
var ctx = context.Background()
func getGhToken() string {
token := os.Getenv("GITHUB_ACCESS_TOKEN")
if token == "" {
log.Fatalf("GITHUB_ACCESS_TOKEN not set")
}
return token
}
func getBonuslyToken() string {
token := os.Getenv("BONUSLY_ACCESS_TOKEN")
if token == "" {
log.Fatalf("BONUSLY_ACCESS_TOKEN not set")
}
return token
}
// Initializes the GitHub client
func initGhClient() {
token := getGhToken()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: token},
)
tc := oauth2.NewClient(ctx, ts)
// Initialize the global client
client = github.NewClient(tc)
}
// generates a hash
func hash(s string) uint32 {
h := fnv.New32a()
_, _ = h.Write([]byte(s))
return h.Sum32()
}
// Get the authenticated user's username
func getAuthenticatedUsername() (string, error) {
user, _, err := client.Users.Get(ctx, "")
if err != nil {
return "", fmt.Errorf("error fetching authenticated user: %v", err)
}
return user.GetLogin(), nil
}
func getOpenRequests() {
username, err := getAuthenticatedUsername()
if err != nil {
log.Fatalf("Failed to retrieve authenticated user's username: %v", err)
}
// Search for open pull requests authored by the authenticated user
query := fmt.Sprintf("is:pr is:open author:%s", username)
opts := &github.SearchOptions{
ListOptions: github.ListOptions{PerPage: 50},
}
// Perform the search
result, _, err := client.Search.Issues(ctx, query, opts)
if err != nil {
log.Fatalf("Error searching for pull requests: %v", err)
}
// Print the results
log.Printf("Found %d open pull requests\n", result.GetTotal())
for _, issue := range result.Issues {
repoUrl := issue.GetRepositoryURL()
repo := strings.Split(repoUrl, "/")
repoNumber := issue.GetNumber()
repoOwner := repo[len(repo)-2]
repoName := repo[len(repo)-1]
reviewers := make([]Reviewer, 0)
ghReviews := getAllReviews(repoOwner, repoName, repoNumber)
if ghReviews == nil {
return
}
hashInput := fmt.Sprintf("%v-%v-%v", repoOwner, repoName, repoNumber)
prHash := fmt.Sprintf("%v", hash(hashInput))
for _, review := range ghReviews {
id := review.GetUser().GetID()
user, _, _ := client.Users.GetByID(ctx, id)
_username := user.GetLogin()
if _username == username { /// exclude self in the reviewers list
continue
}
name := user.GetName()
email := user.GetEmail()
reviewers = append(reviewers, Reviewer{
Username: _username,
Name: name,
Email: email,
})
}
reviews = append(reviews, Review{
users: reviewers,
hash: prHash,
processed: false,
})
}
}
func getAllReviews(owner string, repo string, id int) []*github.PullRequestReview {
opts := &github.ListOptions{PerPage: 50}
reviews, _, err := client.PullRequests.ListReviews(ctx, owner, repo, id, opts)
if err != nil {
log.Fatalln("Error getting PR reviewers:", err)
}
return reviews
}
func removeDuplicateUsers(users []Reviewer) []Reviewer {
temp := make(map[string]bool)
var result []Reviewer
for _, user := range users {
if !temp[user.Username] {
temp[user.Username] = true
result = append(result, user)
}
}
return result
}
func processRewardList() {
reviewers := make([]Reviewer, 0)
for _, value := range reviews {
reviewers = append(reviewers, value.users...)
}
reviewers = removeDuplicateUsers(reviewers)
log.Printf("Found %v unique reviewers\n", len(reviewers))
// TODO: force get email from repo commit data. This is still WIP
//forceGetEmail(reviewers)
usernames := make([]string, 0)
for _, reviewer := range reviewers {
if reviewer.Email == "" {
// use Bonusly autocomplete as a last resort to get a username without email
username := getBonuslySuggestedName(reviewer.Name)
if username != "" {
usernames = append(usernames, username)
}
} else {
username, err := getBonuslyUsernames(reviewer.Email)
if err != nil {
log.Printf("Error: %v", err)
}
usernames = append(usernames, username)
}
}
message := generateBonuslyMessage(usernames)
log.Printf("Generated message: %v\n", message)
sendBonuslyPoints(message)
}
/* Bonusly functions here */
func generateBonuslyMessage(usernames []string) string {
tag := "focus-on-continuous-improvement"
points := 5
for i, name := range usernames {
usernames[i] = "@" + name
}
userStr := strings.Join(usernames, " ")
message := fmt.Sprintf("%s Thanks for the super helpful review and great feedback! 🙌 +%d #%s", userStr, points, tag)
return message
}
func getBonuslyUsernames(email string) (string, error) {
token := getBonuslyToken()
encodedEmail := url.QueryEscape(email)
requestUrl := fmt.Sprintf("https://bonus.ly/api/v1/users?limit=1&email=%v&include_archived=false", encodedEmail)
req, _ := http.NewRequest("GET", requestUrl, nil)
req.Header.Add("accept", "application/json")
req.Header.Add("authorization", "Bearer "+token)
res, _ := http.DefaultClient.Do(req)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
body, err := io.ReadAll(res.Body)
if err != nil {
return "", fmt.Errorf("error reading response body: %v", err)
}
name := gjson.Get(string(body), "result.0.username")
if name.Exists() {
return name.String(), nil
} else {
return "", fmt.Errorf("username not found")
}
}
func sendBonuslyPoints(message string) {
token := getBonuslyToken()
requestUrl := "https://bonus.ly/api/v1/bonuses"
payload := strings.NewReader(fmt.Sprintf("{\"reason\":\"%v\"}", message))
req, _ := http.NewRequest("POST", requestUrl, payload)
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("authorization", "Bearer "+token)
res, _ := http.DefaultClient.Do(req)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
body, _ := io.ReadAll(res.Body)
if res.StatusCode >= 200 && res.StatusCode < 300 {
log.Println("Sent message:", res.Status)
} else {
fmt.Printf("Request failed with status: %s\nBody: %s\n", res.Status, body)
}
}
func getBonuslySuggestedName(name string) string {
token := getBonuslyToken()
encodedName := url.QueryEscape(name)
requestUrl := fmt.Sprintf("https://bonus.ly/api/v1/users/autocomplete?search=%v", encodedName)
req, _ := http.NewRequest("GET", requestUrl, nil)
req.Header.Add("accept", "application/json")
req.Header.Add("authorization", "Bearer "+token)
res, _ := http.DefaultClient.Do(req)
defer func(Body io.ReadCloser) {
_ = Body.Close()
}(res.Body)
body, _ := io.ReadAll(res.Body)
username := gjson.Get(string(body), "result.0.username")
if username.Exists() {
return username.String()
} else {
return ""
}
}
// brute force email search with go-git
func forceGetEmail(reviewers []Reviewer) {
for _, reviewer := range reviewers {
if reviewer.Email != "" {
}
repo := getPublicRepoByUser(reviewer.Username)
if repo != "" {
reviewer.Email = getEmailFromPublicRepo(reviewer.Username, repo)
}
println(reviewer.Email)
}
}
// gets just 1 public repo by username
func getPublicRepoByUser(user string) string {
opts := &github.RepositoryListByUserOptions{
ListOptions: github.ListOptions{PerPage: 1},
}
repos, _, err := client.Repositories.ListByUser(ctx, user, opts)
if err != nil {
log.Printf("Error fetching repositories: %v", err)
return ""
}
if len(repos) == 0 {
return ""
}
repository := repos[0].GetName()
return repository
}
func getEmailFromPublicRepo(owner string, repository string) string {
token := getGhToken()
repoUrl := fmt.Sprintf("https://github.com/%v/%v.git", owner, repository)
tempDir := filepath.Join(".", "cloned-repos")
// Clones the repository into the given dir, just as a normal git clone does
_, err := git.PlainClone(tempDir, false, &git.CloneOptions{
URL: repoUrl,
Auth: &gitHttp.BasicAuth{
Username: "--",
Password: token,
},
})
// Open the cloned repository
repo, err := git.PlainOpen(tempDir)
if err != nil {
log.Printf("Failed to open the repository: %v\n", err)
return ""
}
// Get the reference for the HEAD
_, err = repo.Reference(plumbing.HEAD, true)
if err != nil {
log.Printf("Failed to get HEAD reference: %v\n", err)
return ""
}
// Get the commits in the repository
commitIter, err := repo.CommitObjects()
if err != nil {
log.Printf("Failed to get commit objects: %v\n", err)
return ""
}
// Iterate over the commits and find the first one
var firstCommit *object.Commit
err = commitIter.ForEach(func(c *object.Commit) error {
if firstCommit == nil {
firstCommit = c
}
return nil
})
if err != nil {
log.Printf("Failed to iterate over commits: %v\n", err)
}
// Clean up by removing the temporary directory
if err := os.RemoveAll(tempDir); err != nil {
log.Printf("Failed to remove temporary directory: %v\n", err)
}
return firstCommit.Author.Email
}
func removeClosedRequests() { // find pull requests that are closed and remove them from the reviewList
}
func main() {
initGhClient()
ticker := time.NewTicker(15 * time.Minute)
defer ticker.Stop()
getOpenRequests()
processRewardList()
go func() {
for range ticker.C {
getOpenRequests()
processRewardList()
}
}()
select {}
}