This repository has been archived by the owner on Nov 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (182 loc) · 4.9 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
package main
import (
"context"
"fmt"
"html/template"
"io"
"log"
"os"
"regexp"
"strconv"
"time"
ColorExtractor "github.com/HackSquadDev/contributor-of-the-xxx-golang/colorExtractor"
"github.com/HackSquadDev/contributor-of-the-xxx-golang/handler"
"github.com/HackSquadDev/contributor-of-the-xxx-golang/types"
"github.com/joho/godotenv"
"github.com/labstack/echo/v4"
"github.com/machinebox/graphql"
)
type TemplateRegistry struct {
templates *template.Template
}
// Implement e.Renderer interface
func (t *TemplateRegistry) Render(w io.Writer, name string, data interface{}, c echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
func main() {
// Load ENVs
err := godotenv.Load(".env")
if err != nil {
log.Fatal("Error loading .env file")
}
port, err := strconv.Atoi(os.Getenv("PORT"))
if err != nil {
panic(err)
}
// Initialize Echo
e := echo.New()
e.Renderer = &TemplateRegistry{
templates: template.Must(template.ParseGlob("public/views/*.go.html")),
}
e.GET("/", home)
e.Logger.Fatal(e.Start(fmt.Sprintf(":%d", port)))
}
func home(c echo.Context) error {
client := graphql.NewClient("https://api.github.com/graphql")
client2 := graphql.NewClient("https://api.github.com/graphql")
orgData := getOrganizationDetails(client2)
response := requestMergedPrs(client, "")
hasNextPage := response.Search.PageInfo.HasNextPage
// html := ""
endCursor := ""
PrCount := make(map[string]int)
DataMap := make(map[string]types.RepositoryResponse)
// bots have apps in their url.
// Ex: https://github.com/apps/copybara-service
r, err := regexp.Compile("^.*/apps/.*$")
if err != nil {
c.Logger().Panic("Unable to compile regexp: %v", err)
}
for hasNextPage {
response = requestMergedPrs(client, endCursor)
hasNextPage = response.Search.PageInfo.HasNextPage
endCursor = response.Search.PageInfo.EndCursor
for i := 0; i < len(response.Search.Nodes); i++ {
// if it is a bot then don't count
if r.MatchString(response.Search.Nodes[i].Author.Url) {
continue
}
DataMap[response.Search.Nodes[i].Author.Login] = response.Search.Nodes[i]
PrCount[response.Search.Nodes[i].Author.Login]++
}
}
winnerName := ""
highScore := 0
for name, prs := range PrCount {
/* html += name + ": " + fmt.Sprintf("%d", prs)
html += "<br/>" */
if prs > highScore {
highScore = prs
winnerName = name
}
}
var winnerData types.RepositoryResponse
c.Logger().Printf("Winner %s: PRs: %d", winnerName, highScore)
for name, dude := range DataMap {
if name == winnerName {
winnerData = dude
break
}
}
// get organization details
// get the organization avatar
imageLink := orgData.Organization.AvatarUrl
fmt.Printf("%v \n", imageLink)
// get dominant colors from this image using colorExtractor module
dominantColors, err := ColorExtractor.GetColors(imageLink)
if err != nil {
c.Logger().Panic("Unable to get colors: %v", err)
}
// log the dominant colors
c.Logger().Printf("\nDominant colors: %v\n", dominantColors)
// c.Logger().Printf("%v", winnerData)
return handler.HomeHandler(c, winnerData, highScore, orgData, dominantColors)
// return c.HTML(http.StatusOK, html)
}
// GraphQL Related Functions
func requestMergedPrs(client *graphql.Client, endCursor string) types.SearchResponse {
// NOTE: DON'T EVEN THINK ABOUT TOUCHING THESE LINES.
// refer https://stackoverflow.com/questions/33119748/convert-time-time-to-string#comment70144458_33119937
// YYYY-MM-DD
aWeekAgo := time.Now().AddDate(0, 0, -7).Format("2006-01-02")
today := time.Now().Format("2006-01-02")
query := fmt.Sprintf(`
{
search(
first: 100
type: ISSUE,
query: "org:%s is:pr is:merged merged:%s..%s -author:robot"
%s
) {
nodes {
... on PullRequest {
title
url
author {
avatarUrl
url
login
}
}
}
pageInfo{
hasNextPage
endCursor
}
}
}
`, os.Getenv("GITHUB_ORG_NAME"), aWeekAgo, today, checkEndCursor(endCursor))
// fmt.Printf(query)
request := makeRequest(query)
var resp types.SearchResponse
err := client.Run(context.Background(), request, &resp)
if err != nil {
log.Fatal(err)
}
return resp
}
// graphql function to get organization details
func getOrganizationDetails(client *graphql.Client) types.OrganizationResponse {
query := fmt.Sprintf(`
{
organization(login: "%s") {
name
url
avatarUrl
login
}
}
`, os.Getenv("GITHUB_ORG_NAME"))
request := makeRequest(query)
var resp types.OrganizationResponse
err := client.Run(context.Background(), request, &resp)
if err != nil {
log.Fatal(err)
}
return resp
}
func checkEndCursor(cursor string) string {
if cursor != "" {
return fmt.Sprintf("after:\"%s\"", cursor)
}
return ""
}
func makeRequest(query string) *graphql.Request {
request := graphql.NewRequest(query)
githubToken := os.Getenv("GITHUB_TOKEN")
if githubToken == "" {
log.Fatal("GITHUB_TOKEN is empty.")
}
request.Header.Set("Authorization", fmt.Sprintf("bearer %s", githubToken))
return request
}