-
Notifications
You must be signed in to change notification settings - Fork 0
/
merges.go
173 lines (152 loc) · 4.44 KB
/
merges.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
// Copyright 2016 Giulio Iotti. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package umarell
import (
"fmt"
)
type buildver struct {
// sha1 records the previous version compared to build.sha1.
sha1 string
build *build
}
type mergereq struct {
notif *notif
build *build
token int64
}
func newMergereq(notif *notif, token int64, build *build) *mergereq {
return &mergereq{
notif: notif,
build: build,
token: token,
}
}
type checkout struct {
stage string
dir string
ver buildver
}
func newCheckout(stage, dir string, ver buildver) *checkout {
return &checkout{
dir: dir,
stage: stage,
ver: ver,
}
}
type mergebot struct {
project string
norem map[string]struct{} // stages that cannot be removed
checkouts map[string]*checkout // stage : checkout
vers map[string]*buildver // stage : version
reqs chan *mergereq
dels chan string // stage
srv *server
}
func newMergebot(project string, s *server) *mergebot {
b := &mergebot{
project: project,
srv: s,
norem: make(map[string]struct{}),
checkouts: make(map[string]*checkout),
vers: make(map[string]*buildver),
reqs: make(chan *mergereq),
dels: make(chan string),
}
return b
}
func (b *mergebot) addUnremovable(stage string) {
b.norem[stage] = struct{}{}
}
func (b *mergebot) addCheckout(dir string, notif *notif, build *build) {
bv := buildver{
sha1: notif.sha1,
build: build,
}
b.checkouts[build.stage] = newCheckout(build.stage, dir, bv)
b.srv.log.Printf("[mergebot] %s: init %s to %s using stage %s", b.project, notif.branch, notif.sha1, build.stage)
}
func (b *mergebot) registerBuild(req *mergereq) {
bv := b.vers[req.build.stage]
if bv == nil {
bv = &buildver{
build: req.build,
}
}
bv.sha1 = req.notif.sha1
b.vers[req.build.stage] = bv
b.srv.log.Printf("[mergebot] %s: set latest revision to %s stage %s", b.project, req.notif.sha1, req.build.stage)
}
func (b *mergebot) checkMerged(notif *notif, token int64, co *checkout, pjs *projects) error {
ver := co.ver
b.srv.log.Printf("[mergebot] %s: checking that %s from %s has been merged to %s", b.project, ver.sha1, ver.build.stage, co.stage)
commits := newGitcommits()
if ver.sha1 == "" {
return fmt.Errorf("cannot fetch commits since last build, last SHA1 is empty", b.project)
}
if err := commits.since(ver.sha1, co.dir); err != nil {
return fmt.Errorf("%s: can't fetch commits since %s: %s", co.dir, ver.sha1, err)
}
merged := make([]string, 0) // merged stages to remove
for k, bv := range b.vers {
// Do not attempt to remove a checked-out stage.
if _, ok := b.norem[bv.build.stage]; ok {
b.srv.log.Printf("[mergebot] %s: merge to %s ignored", b.project, bv.build.stage)
continue
}
if commits.contains(githash(bv.sha1)) {
b.srv.log.Printf("[mergebot] %s: can remove env %s, it was merged", b.project, bv.build.stage)
b.srv.urls.del(bv.build.stage)
// As we have been called by pjs, to make a request we need to wait for the current one to finish.
// To avoid a deadlock, we must notify of the merge in the background.
go pjs.destroy(bv.build, notif, token)
merged = append(merged, k)
}
}
for _, k := range merged {
delete(b.vers, k)
}
return nil
}
func (b *mergebot) destroy(stage string) {
b.dels <- stage
}
func (b *mergebot) send(req *mergereq) {
b.reqs <- req
}
func (b *mergebot) run(pjs *projects) {
for {
select {
case req := <-b.reqs:
b.doReq(req, pjs)
case stage := <-b.dels:
delete(b.vers, stage)
}
}
}
func (b *mergebot) doReq(req *mergereq, pjs *projects) {
co, hasCheckout := b.checkouts[req.build.stage]
if !hasCheckout {
// normally update some tracked version
b.registerBuild(req)
return
}
// It's a push to a checked out stage, trigger the delete etc
if err := b.checkMerged(req.notif, req.token, co, pjs); err != nil {
b.srv.log.Printf("[mergebot] %s: failed merge check: %s", b.project, err)
}
co.ver.sha1 = req.notif.sha1
b.srv.log.Printf("[mergebot] %s: merge check done, set latest revision to %s stage %s", b.project, req.notif.sha1, co.ver.build.stage)
}
type mergebots map[string]*mergebot // project : mergebots
func makeMergebots() mergebots {
return mergebots(make(map[string]*mergebot))
}
func (m mergebots) get(project string) *mergebot {
return m[project]
}
func (m mergebots) create(project string, s *server) *mergebot {
b := newMergebot(project, s)
m[project] = b
return b
}