-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitstats.go
134 lines (110 loc) · 2.61 KB
/
gitstats.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
package main
// https://medium.com/@marcus.olsson/writing-a-go-client-for-your-restful-api-c193a2f4998c
import (
"encoding/json"
"fmt"
"os"
"net/url"
"github.com/valyala/fasthttp"
)
type config struct {
Repositories []repository `json:"repositories"`
}
type repository struct {
Owner string `json:"owner"`
Name string `json:"name"`
Branch string `json:"branch"`
}
type commit struct {
Sha string `json: "sha"`
Author author `json: "author"`
}
type author struct {
Login string `json: "login"`
ID int `json: "id"`
}
type stats struct {
Additions int `json: "additions"`
Deletions int `json: "deletions"`
Total int `json: "total"`
}
type client struct {
baseURL *url.URL
http *fasthttp.HostClient
values map[int]map[string]int
}
const defaultConfigFile = "config.json"
const host = "api.github.com"
func loadConfiguration(file string) (config, error) {
var config config
configFile, err := os.Open(file)
if err != nil {
return config, err
}
jsonParser := json.NewDecoder(configFile)
jsonParser.Decode(&config)
return config, nil
}
func (c *client) do(url string, v interface{}) error {
statusCode, body, err := c.http.Get(nil, url)
if err != nil {
return err
}
if statusCode != fasthttp.StatusOK {
return fmt.Errorf("Unexpected status code: %d. Expecting %d", statusCode, fasthttp.StatusOK)
}
return json.Unmarshal(body, v)
}
func (c *client) commits(repository *repository) ([]commit, error) {
var commits []commit
rel := &url.URL{
Path: "repos/" + repository.Owner + "/" + repository.Name + "/commits",
}
u := c.baseURL.ResolveReference(rel)
err := c.do(u.String(), &commits)
return commits, err
}
func (c *client) stats(repository *repository, ref string) (stats, error) {
var stats stats
rel := &url.URL{
Path: "repos/" + repository.Owner + "/" + repository.Name + "/commits/" + ref,
}
u := c.baseURL.ResolveReference(rel)
err := c.do(u.String(), &stats)
return stats, err
}
func (c *client) add(id int, key string, value int) {
mm, ok := c.values[id]
if !ok {
mm = make(map[string]int)
c.values[id] = mm
}
mm[key] += value
}
func main() {
config, err := loadConfiguration(defaultConfigFile)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", config)
client := &client{
baseURL: &url.URL{
Scheme: "https",
Host: host,
},
http: &fasthttp.HostClient{
Addr: host,
IsTLS: true,
},
}
commits, err2 := client.commits(&config.Repositories[0])
if err2 != nil {
panic(err2)
}
fmt.Printf("%+v\n", commits[0])
stats, err3 := client.stats(&config.Repositories[0], commits[1].Sha)
if err3 != nil {
panic(err3)
}
fmt.Printf("%+v\n", stats)
}