-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
135 lines (117 loc) · 3.56 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
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"strings"
"time"
)
// https://docs.github.com/en/graphql/reference/objects#contributionscollection
type GithubAPIGQLResponse struct {
Data struct {
User struct {
Name string `json:"name"`
ContributionsCollection struct {
TotalCommitContributions int `json:"totalCommitContributions"`
TotalIssueContributions int `json:"totalIssueContributions"`
TotalPullRequestContributions int `json:"totalPullRequestContributions"`
TotalPullRequestReviewContributions int `json:"totalPullRequestReviewContributions"`
TotalRepositoriesWithContributedCommits int `json:"totalRepositoriesWithContributedCommits"`
ContributionCalendar struct {
TotalContributions int `json:"totalContributions"`
Weeks []struct {
ContributionDays []struct {
ContributionCount int `json:"contributionCount"`
Date string `json:"date"`
Weekday int `json:"weekday"`
} `json:"contributionDays"`
} `json:"weeks"`
} `json:"contributionCalendar"`
} `json:"contributionsCollection"`
} `json:"user"`
} `json:"data"`
}
func readGitHubTestFile() *GithubAPIGQLResponse {
file, _ := ioutil.ReadFile("./test/github-response.json")
var resp GithubAPIGQLResponse
_ = json.Unmarshal([]byte(file), &resp)
return &resp
}
type GitHub struct {
client http.Client
token string
}
func NewGitHub(token string) *GitHub {
var netTransport = &http.Transport{
Dial: (&net.Dialer{
Timeout: 5 * time.Second,
}).Dial,
TLSHandshakeTimeout: 5 * time.Second,
}
return &GitHub{
client: http.Client{
Timeout: time.Second * 10,
Transport: netTransport,
},
token: token,
}
}
func (gh *GitHub) gqlRequest() *GithubAPIGQLResponse {
query := fmt.Sprintf(`
query {
user(login: "%s") {
name contributionsCollection (from: "%s" , to: "%s"){
totalCommitContributions
totalIssueContributions
totalPullRequestContributions
totalPullRequestReviewContributions
totalRepositoriesWithContributedCommits
contributionCalendar {
totalContributions weeks {
contributionDays {
contributionCount date weekday
}
}
}
}
}
}`, "noelruault", time.Now().AddDate(-1, 0, 0).Format(time.RFC3339), time.Now().Format(time.RFC3339))
graphQLRequest := struct {
Query string `json:"query"`
Variables string `json:"variables"`
}{Query: query}
gqlMarshalled, err := json.Marshal(graphQLRequest)
if err != nil {
panic(err)
}
ss := string(gqlMarshalled)
req, err := http.NewRequest(http.MethodPost, "https://api.github.com/graphql", strings.NewReader(ss))
if err != nil {
log.Fatal(err)
}
decodeToken, _ := base64.StdEncoding.DecodeString(gh.token)
req.Header.Add("Authorization", "Bearer "+string(decodeToken))
req.Header.Add("Content-Type", "application/json")
res, err := gh.client.Do(req)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
var ghresp GithubAPIGQLResponse
json.NewDecoder(res.Body).Decode(&ghresp)
return &ghresp
}
func getGitHubMonthlyContributions(contributions *GithubAPIGQLResponse) map[string]int {
contributionData := make(map[string]int, 0)
weeks := contributions.Data.User.ContributionsCollection.ContributionCalendar.Weeks
for i := range weeks {
for j := range weeks[i].ContributionDays {
contributionData[weeks[i].ContributionDays[j].Date] += weeks[i].ContributionDays[j].ContributionCount
}
}
return contributionData
}