-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
189 lines (144 loc) · 3.76 KB
/
main.go
File metadata and controls
189 lines (144 loc) · 3.76 KB
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
181
182
183
184
185
186
187
188
189
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"strings"
"github.com/google/uuid"
"github.com/juicemia/log"
"gitlab.com/run-ci/webhooks/gitlab/queue"
git "gopkg.in/src-d/go-git.v4"
yaml "gopkg.in/yaml.v2"
)
var logger *log.Logger
var clonesdir string
var q PipelineSender
type PipelineSender interface {
SendPipeline(queue.Event) error
}
func init() {
log.SetLevelFromEnv("WEBHOOK_LOG_LEVEL")
logger = log.New("webhooks/gitlab/main")
clonesdir = os.Getenv("WEBHOOK_CLONES_DIR")
if clonesdir == "" {
var err error
clonesdir, err = os.Getwd()
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
}).Fatal("error getting current working directory for WEBHOOK_CLONES_DIR")
}
}
q = queue.NewEchoQueue()
}
func main() {
logger.Info("booting gitlab webhook")
http.HandleFunc("/", handle)
http.ListenAndServe(":9090", nil)
}
func handle(wr http.ResponseWriter, req *http.Request) {
logger = logger.CloneWith(map[string]interface{}{
"method": req.Method,
"url": req.URL.String(),
})
logger.Debug("handling request")
body, err := ioutil.ReadAll(req.Body)
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
}).Error("error reading request body")
wr.WriteHeader(http.StatusInternalServerError)
return
}
logger.Debug("unmarshaling request body")
var ev PushEvent
err = json.Unmarshal(body, &ev)
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
}).Error("error unmarshaling request body")
wr.WriteHeader(http.StatusBadRequest)
return
}
logger = logger.CloneWith(map[string]interface{}{
"event": ev,
})
logger.Debug("got event")
clonedir := fmt.Sprintf("%v/%v", clonesdir, uuid.New())
logger = logger.CloneWith(map[string]interface{}{
"clonedir": clonedir,
})
logger.Debug("cloning repo")
_, err = git.PlainClone(clonedir, false, &git.CloneOptions{
URL: ev.Repository.GitHTTPURL,
Progress: os.Stdout,
})
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
}).Error("unable to clone git repository")
wr.WriteHeader(http.StatusInternalServerError)
return
}
pipelinesdir := fmt.Sprintf("%v/pipelines", clonedir)
finfos, err := ioutil.ReadDir(pipelinesdir)
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
"dir": pipelinesdir,
}).Error("unable to list directory")
wr.WriteHeader(http.StatusInternalServerError)
return
}
for _, finfo := range finfos {
// The file name corresponds to the pipeline name.
name := strings.Split(finfo.Name(), ".")[0]
logger := logger.CloneWith(map[string]interface{}{
"pipeline_name": name,
})
logger.Debug("processing pipeline")
logger.Debug("opening pipeline file")
f, err := os.Open(fmt.Sprintf("%v/%v", pipelinesdir, finfo.Name()))
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
}).Error("unable to open pipeline file")
continue
}
logger.Debug("reading pipeline file")
buf, err := ioutil.ReadAll(f)
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
}).Error("unable to read pipeline file")
continue
}
logger.Debug("loading pipeline")
var p queue.Event
err = yaml.UnmarshalStrict(buf, &p)
if err != nil {
logger = logger.CloneWith(map[string]interface{}{
"error": err,
})
logger.Error("unable to unmarshal pipeline")
continue
}
p.Name = name
p.Remote = ev.Repository.GitHTTPURL
logger = logger.CloneWith(map[string]interface{}{
"pipeline": p,
})
logger.Debug("sending pipeline")
err = q.SendPipeline(p)
if err != nil {
logger.CloneWith(map[string]interface{}{
"error": err,
}).Error("unable to send pipeline")
continue
}
}
wr.WriteHeader(http.StatusNoContent)
return
}