This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
repowatch.go
180 lines (144 loc) · 3.68 KB
/
repowatch.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"regexp"
"strings"
"time"
"github.com/Sirupsen/logrus"
"github.com/cenkalti/backoff"
"github.com/robfig/cron"
"gopkg.in/yaml.v2"
)
type repoWatch struct {
WatchedRepos []watchedRepo `yaml:"watched_repos"`
}
type watchedRepo struct {
Repo string `yaml:"repo"`
Interval string `yaml:"interval"`
}
type githubCommit struct {
SHA string `json:"sha"`
}
func init() {
watcher := &repoWatch{}
go watcher.Run()
}
func (r *repoWatch) Run() {
r.loadWatches()
c := cron.New()
c.AddFunc("@every 1m", r.iterateRepos)
c.Start()
for {
select {}
}
}
func (r *repoWatch) iterateRepos() {
var threadLimit = make(chan bool, 10)
for _, repo := range r.WatchedRepos {
if r.isLocked(repo.Repo) {
continue
}
threadLimit <- true
go func(repo watchedRepo) {
err := backoff.Retry(func() error {
lastBuild, err := r.getLastBuild(repo.Repo)
if err != nil {
return err
}
lastCommit, err := r.getLastCommit(repo.Repo)
if err != nil {
return err
}
if !strings.HasPrefix(lastCommit, lastBuild) {
urlString := "https://gobuilder.me/api/v1/webhook/cli"
resp, err := http.PostForm(urlString, url.Values{
"repository": []string{repo.Repo},
})
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("GoBuilder did not respond with 200 OK to trigger")
}
log.Printf("Triggered build for repo %s", repo.Repo)
}
d, err := time.ParseDuration(repo.Interval)
if err != nil {
log.Errorf("Error while parsing interval for repo %s: %s", repo.Repo, err)
d = time.Minute * 10
}
r.lockRepo(repo.Repo, d)
return nil
}, backoff.NewExponentialBackOff())
if err != nil {
log.WithFields(logrus.Fields{}).Printf("Error while fetching watched repo: %s", err)
}
<-threadLimit
}(repo)
}
for len(threadLimit) > 0 {
select {}
}
}
func (r *repoWatch) isLocked(repo string) bool {
redisKey := fmt.Sprintf("project::%s::repowatch", repo)
lock, err := redisClient.Get(redisKey)
if err != nil || string(lock) == "locked" {
if err != nil {
log.Errorf("Error while loading repowatch lock: %s", err)
}
return true
}
return false
}
func (r *repoWatch) lockRepo(repo string, d time.Duration) error {
redisKey := fmt.Sprintf("project::%s::repowatch", repo)
return redisClient.Set(redisKey, "locked", int(d.Seconds()), 0, false, false)
}
func (r *repoWatch) loadWatches() error {
content, err := ioutil.ReadFile("repowatch.yml")
if err != nil {
return err
}
return yaml.Unmarshal(content, r)
}
func (r *repoWatch) getLastCommit(repo string) (string, error) {
// https://api.github.com/repos/Luzifer/password/commits
rex := regexp.MustCompile(`github\.com/([^/]+/[^/]+)/?`)
repoParts := rex.FindStringSubmatch(repo)
if len(repoParts) < 2 {
return "", fmt.Errorf("Repo did not match GitHub-RegEx")
}
repoPart := repoParts[1]
url := fmt.Sprintf("https://api.github.com/repos/%s/commits", repoPart)
result := []githubCommit{}
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return "", err
}
if len(result) < 1 {
return "", fmt.Errorf("Did not found one or more commits")
}
return result[0].SHA, nil
}
func (r *repoWatch) getLastBuild(repo string) (string, error) {
redisKey := fmt.Sprintf("project::%s::built-commits", repo)
commits, err := redisClient.ZRevRangeByScore(redisKey, "+inf", "-inf", true, true, 0, 1)
if err != nil {
return "", err
}
if len(commits) < 1 {
return "", nil
}
return commits[0], nil
}