/
github_listener.go
121 lines (99 loc) · 2.69 KB
/
github_listener.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
// Copyright 2019 Clivern. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package controller
import (
"fmt"
"net/http"
"strings"
"github.com/clivern/rabbit/internal/app/model"
"github.com/clivern/rabbit/pkg"
"github.com/clivern/hippo"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"go.uber.org/zap"
)
// GithubListener controller
func GithubListener(c *gin.Context, messages chan<- string) {
rawBody, _ := c.GetRawData()
body := string(rawBody)
parser := &pkg.GithubWebhookParser{
UserAgent: c.GetHeader("User-Agent"),
GithubDelivery: c.GetHeader("X-GitHub-Delivery"),
GitHubEvent: c.GetHeader("X-GitHub-Event"),
HubSignature: c.GetHeader("X-Hub-Signature"),
Body: body,
}
logger, _ := hippo.NewLogger(
viper.GetString("log.level"),
viper.GetString("log.format"),
[]string{viper.GetString("log.output")},
)
defer logger.Sync()
ok := parser.VerifySignature(viper.GetString("integrations.github.webhook_secret"))
eventName := parser.GetGitHubEvent()
if !ok && viper.GetString("integrations.github.webhook_secret") != "" {
c.JSON(http.StatusForbidden, gin.H{
"status": "Oops!",
})
return
}
// Ping event received
if eventName == "ping" {
var pingEvent pkg.GithubPingEvent
pingEvent.LoadFromJSON(rawBody)
logger.Info(fmt.Sprintf(
`Github event %s received`,
eventName,
), zap.String("CorrelationID", c.Request.Header.Get("X-Correlation-ID")))
c.JSON(http.StatusOK, gin.H{
"status": "Nice!",
})
return
}
if eventName != "create" {
c.JSON(http.StatusOK, gin.H{
"status": "Nice, Skip!",
})
return
}
var createEvent pkg.GithubCreateEvent
createEvent.LoadFromJSON(rawBody)
logger.Info(fmt.Sprintf(
`Github event %s received`,
eventName,
), zap.String("CorrelationID", c.Request.Header.Get("X-Correlation-ID")))
if createEvent.RefType != "tag" {
logger.Info(fmt.Sprintf(
`Github create event received but RefType is %s not tag`,
createEvent.RefType,
), zap.String("CorrelationID", c.Request.Header.Get("X-Correlation-ID")))
c.JSON(http.StatusOK, gin.H{
"status": "Nice!",
})
return
}
href := strings.ReplaceAll(
viper.GetString("integrations.github.https_format"),
"[.RepoFullName]",
createEvent.Repository.FullName,
)
if viper.GetString("integrations.github.clone_with") == "ssh" {
href = strings.ReplaceAll(
viper.GetString("integrations.github.ssh_format"),
"[.RepoFullName]",
createEvent.Repository.FullName,
)
}
releaseRequest := model.ReleaseRequest{
Name: createEvent.Repository.Name,
URL: href,
Version: createEvent.Ref,
}
passToWorker(
c,
messages,
logger,
releaseRequest,
)
}