-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
175 lines (152 loc) · 3.57 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"time"
bolt "go.etcd.io/bbolt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type User struct {
User string `json:"login"`
PublicRepos int `json:"public_repos"`
}
type Repo struct {
FullName string `json:"full_name"`
CreatedAt string `json:"created_at"`
}
type Repos []Repo
// Returns a list of all repositories of a user going through the paginated results from users GitHub API
func getAllRepos(user string) Repos {
reposPerPage := 100
githubRepoUrl := "https://api.github.com/users/" + user + "/repos?per_page=" + strconv.Itoa(reposPerPage)
var totalResults Repos
for page := 0; ; page++ {
res, err := http.Get(githubRepoUrl + "&" + strconv.Itoa(page))
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
log.Fatal(err)
}
if res.StatusCode != 200 {
log.Fatal(res.StatusCode)
}
var repos []Repo
err = json.Unmarshal([]byte(body), &repos)
if err != nil {
log.Fatal(err)
}
for _, repo := range repos {
totalResults = append(totalResults, repo)
}
if len(repos) < reposPerPage {
break
}
}
return totalResults
}
// Returns number of repositories for user
func getNumberOfRepos(user string) int {
githubRepoUrl := "https://api.github.com/users/" + user
var u User
res, err := http.Get(githubRepoUrl)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(res.Body)
defer res.Body.Close()
if err != nil {
log.Fatal(err)
}
if res.StatusCode != 200 {
log.Fatal(res.StatusCode)
}
err = json.Unmarshal([]byte(body), &u)
if err != nil {
log.Fatal(err)
}
numberOfRepos.Add(float64(u.PublicRepos))
return u.PublicRepos
}
// Saves new repository in the form of 'full_name' as key and 'created_at' as a value
// Also, notifies if there is a repo created
func (repo *Repo) saveRepo(db *bolt.DB, bucketName string) error {
err := db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(bucketName))
r := b.Get([]byte(repo.FullName))
if r == nil {
err := repo.notify()
if err != nil {
return err
}
return b.Put([]byte(repo.FullName), []byte(repo.CreatedAt))
}
return nil
})
return err
}
// Notifiies when there is a new repository created
func (repo *Repo) notify() error {
_, err := fmt.Println(repo.CreatedAt, "repository", repo.FullName, "created")
if err != nil {
return err
}
return nil
}
var (
numberOfRepos = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "number_of_github_repos",
Help: "Number of repositories for user",
})
)
func main() {
user := os.Getenv("GITHUB_USERNAME")
checkInterval, _ := strconv.Atoi(os.Getenv("CHECK_INTERVAL"))
interval := time.Duration(checkInterval) * time.Second
dbPath := os.Getenv("DB_PATH") + "/repos.db"
db, err := bolt.Open(dbPath, 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
bucketName := user
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucketName))
if err != nil {
return err
}
return nil
})
if err != nil {
log.Fatal(err)
}
go func() {
quit := make(chan bool)
repoCheck:
for {
select {
case <-quit:
break repoCheck
default:
getNumberOfRepos(user)
repos := getAllRepos(user)
for _, r := range repos {
r.saveRepo(db, bucketName)
}
}
time.Sleep(interval)
}
}()
http.Handle("/metrics", promhttp.Handler())
prometheus.MustRegister(numberOfRepos)
log.Fatal(http.ListenAndServe(":8080", nil))
}