/
git.go
345 lines (305 loc) · 11.5 KB
/
git.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
package git
import (
"bytes"
"encoding/json"
"errors"
"fmt"
log "github.com/Sirupsen/logrus"
authentication "github.com/boyvanduuren/octorunner/lib/auth"
"github.com/boyvanduuren/octorunner/lib/common"
"github.com/boyvanduuren/octorunner/lib/pipeline"
"github.com/docker/docker/client"
"github.com/google/go-github/github"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
"strings"
"github.com/boyvanduuren/octorunner/lib/persist"
)
const (
eventHeader = "X-GitHub-Event"
forwardedHeader = "X-Forwarded-For"
signatureHeader = "X-Hub-Signature"
tmpDirPrefix = "octorunner-"
tmpFilePrefix = "archive-"
pipelineFile = ".octorunner"
EnvPrefix = "octorunner"
envRepoToken = "%s_%s_TOKEN"
envRepoSecret = "%s_%s_SECRET"
)
var Auth authentication.Method
const repositoryData string = "repositoryData"
type hookPayload struct {
Ref, Before, After, Compare string
Created, Deleted, Forced bool
Repository struct {
ID int
Name string
FullName string `json:"full_name"`
Owner struct {
Name string `json:"name"`
} `json:"owner"`
Private bool
} `json:"repository"`
Pusher struct {
Name, Email string
} `json:"pusher"`
Sender struct {
Login string
ID int
} `json:"sender"`
}
// HandleWebhook is called when we receive a request on our listener and is responsible
// for decoding the payload and passing it to the appropriate handler for that particular event.
// If the received event is not supported we log an error and return without doing anything.
func HandleWebhook(w http.ResponseWriter, r *http.Request, v url.Values) {
// Map Github webhook events to functions that handle them
supportedEvents := map[string]func(hookPayload){
"push": handlePush,
}
// Return 200 to the client
w.WriteHeader(200)
log.Info("Received request on listener")
// Request might be proxied, so check if there's an X-Forwarded-For header
forwardedFor := r.Header.Get(forwardedHeader)
var remoteAddr string
if forwardedFor != "" {
remoteAddr = forwardedFor
} else {
remoteAddr = r.RemoteAddr
}
log.Debug("Request from " + r.UserAgent() + " at " + remoteAddr)
// Check which event we received and assign the appropriate handler to eventHandler
var eventHandler func(hookPayload)
event := r.Header.Get(eventHeader)
if event == "" {
log.Error("Header \"" + eventHeader + "\" not set, returning")
return
} else if val, exists := supportedEvents[event]; exists {
eventHandler = val
log.Debug("Found appropriate handler for \"" + event + "\" event")
} else {
log.Error("Received \"" + eventHeader + "\", but found no supporting handler for \"" +
event + "\" event, returning")
return
}
// Read the body of the request
payloadBody, err := ioutil.ReadAll(r.Body)
defer r.Body.Close()
if err != nil {
log.Errorf("Error while reading payload: %v", err)
} else {
log.Debug("Received body ", string(payloadBody))
}
// Try to decode the payload
jsonDecoder := json.NewDecoder(bytes.NewReader(payloadBody))
var payload hookPayload
err = jsonDecoder.Decode(&payload)
if err != nil {
log.Error("Error while decoding payload: ", err)
return
}
log.Debug("Decoded payload to ", payload)
// The repository that this payload is for might have a secret configured, in which case we expect
// a signature with the payload. The given signature then needs to match a signature we calculate ourselves.
// Only then will we call our handler, else we'll log an error and return
repoSecret := Auth.RequestSecret(payload.Repository.FullName)
if len(repoSecret) == 0 {
// We need to manually search the env, because viper doesn't seem to load these
// environment vars into the config.
repoSecretEnvKey := fmt.Sprintf(envRepoSecret, strings.ToUpper(EnvPrefix), payload.Repository.FullName)
log.Debugf("Couldn't find secret in config file, looking if environment var %q exists", repoSecretEnvKey)
repoSecret = []byte(os.Getenv(repoSecretEnvKey))
if len(repoSecret) == 0 {
log.Error("No secret was configured, cannot verify their signature")
} else {
log.Debugf("Found secret for %q in environment", payload.Repository.FullName)
signature := r.Header.Get(signatureHeader)
if signature == "" {
log.Error("Expected signature for payload, but none given")
return
}
log.Debug("Received signature " + signature)
calculatedSignature := "sha1=" + authentication.CalculateSignature(repoSecret, payloadBody)
log.Debug("Calculated signature ", calculatedSignature)
if !authentication.CompareSignatures([]byte(signature), []byte(calculatedSignature)) {
log.Error("Signatures didn't match")
return
}
}
}
go eventHandler(payload)
}
// Handle a push event to a Github repository. We will need to look at the settings for octorunner
// in this repository and take action accordingly.
func handlePush(payload hookPayload) {
log.Info("Handling received push event")
// Create a context for this request
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
repoFullName := payload.Repository.FullName
repoToken := Auth.RequestToken(repoFullName)
log.Info("Repository \"" + repoFullName + "\" was pushed to")
// See if we have a token for this repository. If we don't we won't be able to set a status so we abort.
// we cannot download the repository from github
if repoToken == nil {
// We need to manually search the env, because viper doesn't seem to load these
// environment vars into the config.
repoTokenEnvKey := fmt.Sprintf(envRepoToken, strings.ToUpper(EnvPrefix), payload.Repository.FullName)
log.Debugf("Couldn't find token in config file, looking if environment var %q exists", repoTokenEnvKey)
repoTokenEnvVal := os.Getenv(repoTokenEnvKey)
if repoTokenEnvVal == "" {
log.Errorf("Didn't find token for %q, this means we won't be able to set a status. Aborting.",
repoFullName)
return
}
log.Debugf("Found token for %q in environment", repoFullName)
repoToken = &oauth2.Token{AccessToken: repoTokenEnvVal}
}
repoName := payload.Repository.Name
repoOwner := payload.Repository.Owner.Name
commitID := payload.After
/*
When a commit is merged from a branch to another branch, the "after" ID is set to
"0000000000000000000000000000000000000000", and the "previous" ID is the ID of the commit being merged.
That commit will probably already have a state assigned, so we can just return
*/
if commitID == "0000000000000000000000000000000000000000" {
log.Info("Not doing anything, this was a merge commit")
return
}
httpClient := oauth2.NewClient(ctx, oauth2.StaticTokenSource(repoToken))
gitClient := github.NewClient(httpClient)
repoDir, err := getRepository(ctx, httpClient, gitClient, repoName, repoOwner, commitID, repoToken)
if err != nil {
log.Errorf("Error while downloading copy of repository: %v", err)
return
}
ctx = context.WithValue(ctx, repositoryData, map[string]string{
"fullName": repoFullName,
"commitId": commitID,
"fsLocation": repoDir,
})
repoPipeline, err := readPipelineConfig(repoDir)
if err != nil {
log.Errorf("Error while reading pipeline configuration: %v", err)
return
}
// set state of commit to pending
log.Debug("Setting state to pending")
gitSetState(ctx, gitClient, "pending", repoOwner, repoName, commitID)
// create Docker client
cli, err := client.NewEnvClient()
if err != nil {
log.Errorf("Error while creating connection to Docker: %q", err)
gitSetState(ctx, gitClient, "error", repoOwner, repoName, commitID)
return
}
defer cli.Close()
exitcode, err := repoPipeline.Execute(ctx, cli, &persist.DBConn)
if err != nil {
log.Errorf("Error while executing pipeline: %v", err)
gitSetState(ctx, gitClient, "error", repoOwner, repoName, commitID)
return
}
log.Debugf("Pipeline returned %d, setting state accordingly", exitcode)
if exitcode == 0 {
gitSetState(ctx, gitClient, "success", repoOwner, repoName, commitID)
} else {
gitSetState(ctx, gitClient, "failure", repoOwner, repoName, commitID)
}
}
func getRepository(ctx context.Context, httpClient *http.Client, gitClient *github.Client, repoName string, repoOwner string,
commitID string, repoToken *oauth2.Token) (string, error) {
const githubArchiveURL = "https://github.com/%s/%s/archive/%s.zip"
const githubArchiveFormat = "zipball"
var archiveURL *url.URL
var err error
log.Info("Downloading archive of latest commit in push")
if repoToken == nil {
// no repoToken, so this is a public repository
archiveURL, err = url.Parse(fmt.Sprintf(githubArchiveURL, repoOwner, repoName, commitID))
if err != nil {
return "", fmt.Errorf("Error while constructing archive URL: %v", err)
}
} else {
log.Debug("Getting archive URL for \"" + repoOwner + "/" + repoName + "\", ref \"" + commitID + "\"")
archiveURL, _, err = gitClient.Repositories.GetArchiveLink(ctx, repoOwner, repoName, githubArchiveFormat,
&github.RepositoryContentGetOptions{Ref: commitID})
if err != nil {
return "", fmt.Errorf("Error while getting archive URL: %v", err)
}
}
log.Debug("Found archive URL ", archiveURL)
tmpDir, err := ioutil.TempDir("", tmpDirPrefix)
if err != nil {
log.Error()
return "", fmt.Errorf("Error while creating temporary directory: %v", err)
}
log.Debug("Created temporary directory " + tmpDir)
archivePath, err := downloadFile(httpClient, archiveURL, tmpDir)
if err != nil {
return "", fmt.Errorf("Error while downloading archive: %v", err)
}
log.Debug("Archive downloaded to ", archivePath.Name())
repoDir, err := common.Unzip(archivePath.Name(), tmpDir)
if err != nil {
return "", fmt.Errorf("Error while unpacking archive: %v", err)
}
log.Debugf("Unpacked archive to %q", repoDir)
// cleanup the archive
archivePath.Close()
os.Remove(archivePath.Name())
// we should now have a copy of the repository at the latest commit
if common.CheckDirNotExists(repoDir) {
log.Error()
return "", fmt.Errorf("Repository not found at expected directory \"%s\" after unpacking", repoDir)
}
log.Debug("Repository unpacked to ", repoDir)
return repoDir, nil
}
func downloadFile(httpClient *http.Client, url *url.URL, downloadDirectory string) (*os.File, error) {
log.Debug("Downloading \"" + url.String() + "\"")
filePath, err := ioutil.TempFile(downloadDirectory, tmpFilePrefix)
if err != nil {
return nil, err
}
resp, err := httpClient.Get(url.String())
defer resp.Body.Close()
n, err := io.Copy(filePath, resp.Body)
if err != nil {
return nil, err
}
log.Debugf("Downloaded %d bytes", n)
return filePath, nil
}
func readPipelineConfig(directory string) (pipeline.Pipeline, error) {
var pipelineConfig pipeline.Pipeline
pipelineConfigPath := path.Join(directory, strings.Join([]string{pipelineFile, ".yaml"}, ""))
if _, err := os.Stat(pipelineConfigPath); os.IsNotExist(err) == true {
pipelineConfigPath = path.Join(directory, strings.Join([]string{pipelineFile, ".yml"}, ""))
if _, err := os.Stat(pipelineConfigPath); os.IsNotExist(err) == true {
return pipelineConfig, errors.New("Couldn't find .octorunner.yaml or .octorunner.yml in repository")
}
}
pipelineConfigBuf, err := ioutil.ReadFile(pipelineConfigPath)
if err != nil {
return pipelineConfig, fmt.Errorf("Error while reading from %s: %v", pipelineConfigPath, err)
}
pipelineConfig, err = pipeline.ParseConfig(pipelineConfigBuf)
return pipelineConfig, err
}
func gitSetState(ctx context.Context, git *github.Client, state string, owner string, repo string, commit string) {
repoStatusContext := "continuous-integration/octorunner"
git.Repositories.CreateStatus(ctx, owner, repo, commit,
&github.RepoStatus{
State: &state,
Context: &repoStatusContext,
},
)
}