This repository has been archived by the owner on Sep 23, 2021. It is now read-only.
/
events.go
78 lines (68 loc) · 1.95 KB
/
events.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
package igithub
import (
"context"
b64 "encoding/base64"
"net/http"
"github.com/google/go-github/github"
"github.com/jrbeverly/github-app-golang/lib/cobrago"
)
const managedGitConfigurationFile = ".aws/config"
func (mid *GithubMiddleware) processIssueCommentEvent(event *github.IssueCommentEvent) (cobrago.TestTriggerEvent, error) {
return cobrago.TestTriggerEvent{Key: *event.Comment.Body}, nil
}
func (mid *GithubMiddleware) processPushEvent(event *github.PushEvent) (cobrago.ConfigChangeEvent, bool, error) {
var result cobrago.ConfigChangeEvent
commit, ok := isFileChangedByCommits(managedGitConfigurationFile, event.Commits)
if !ok {
return result, false, nil
}
client, err := NewGitHubInstallation(int(*event.Installation.ID), mid.conf)
if err != nil {
return result, false, err
}
data, _, _, err := client.Repositories.GetContents(
context.Background(),
*event.Repo.Owner.Name,
*event.Repo.Name,
managedGitConfigurationFile,
&github.RepositoryContentGetOptions{
Ref: *commit.ID,
},
)
if err != nil {
return result, false, err
}
decodedContent, err := b64.StdEncoding.DecodeString(*data.Content)
if err != nil {
return result, false, err
}
result = cobrago.ConfigChangeEvent{
Key: string(decodedContent),
}
return result, true, nil
}
func (mid *GithubMiddleware) authenticate(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
event := mid.WebEvent
switch e := event.(type) {
case *github.IssueCommentEvent:
result, err := mid.processIssueCommentEvent(e)
if err != nil {
return
}
mid.Payload = result
case *github.PushEvent:
result, ok, err := mid.processPushEvent(e)
if err != nil {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
return
}
if !ok {
http.Error(w, http.StatusText(http.StatusAccepted), http.StatusAccepted)
return
}
mid.Payload = result
}
next.ServeHTTP(w, r)
})
}