forked from fluxcd/flux-recv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.go
72 lines (63 loc) · 1.81 KB
/
github.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
package main
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/google/go-github/v28/github"
fluxapi "github.com/fluxcd/flux/pkg/api"
fluxapi_v9 "github.com/fluxcd/flux/pkg/api/v9"
)
const GitHub = "GitHub"
func init() {
Sources[GitHub] = handleGithubPush
}
func handleGithubPush(s fluxapi.Server, key []byte, w http.ResponseWriter, r *http.Request) {
payload, err := github.ValidatePayload(r, key)
if err != nil {
http.Error(w, "The GitHub signature header is invalid.", 401)
log(GitHub, "invalid signature:", err.Error())
return
}
hook, err := github.ParseWebHook(github.WebHookType(r), payload)
if err != nil {
http.Error(w, "Cannot parse payload", http.StatusBadRequest)
log(GitHub, "could not parse payload:", err.Error())
return
}
switch hook := hook.(type) {
case *github.PingEvent:
w.WriteHeader(http.StatusOK)
w.Write([]byte("Pong"))
case *github.PushEvent:
update := fluxapi_v9.GitUpdate{
URL: *hook.Repo.SSHURL,
Branch: strings.TrimPrefix(*hook.Ref, "refs/heads/"),
}
change := fluxapi_v9.Change{
Kind: fluxapi_v9.GitChange,
Source: update,
}
ctx := r.Context()
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err := s.NotifyChange(ctx, change)
if err != nil {
select {
case <-ctx.Done():
http.Error(w, "Timed out waiting for response from downstream API", http.StatusRequestTimeout)
log(GitHub, "timed out")
default:
http.Error(w, "Error while calling downstream API", http.StatusInternalServerError)
log(GitHub, "error:", err.Error())
}
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
default:
w.WriteHeader(http.StatusOK)
w.Write([]byte("unexpected hook kind, but OK"))
log(GitHub, "unexpected webhook payload", fmt.Sprintf("received webhook: %T\n%s", hook, github.Stringify(hook)))
}
}