-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvendor-update-monitor.go
208 lines (186 loc) · 4.61 KB
/
vendor-update-monitor.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"strings"
)
type Commit struct {
ID string `json:"id"`
Added []string `json:"added"`
Removed []string `json:"removed"`
Modified []string `json:"modified"`
}
type GitEvent struct {
Ref string `json:"ref"`
After string `json:"after"`
Repository struct {
Name string `json:"name"`
} `json:"repository"`
Commits []Commit `json:"commits"`
Compare string `json:"compare"`
}
type Config struct {
Patterns []string `json:"patterns"`
Ref string `json:"ref"`
SlackWebhook string `json:"slack"`
Port json.Number `json:"port"`
}
type WebhookResponse struct {
Files []string `json:"files"`
ShouldAlert bool `json:"shouldAlert"`
}
type SlackText struct {
Type string `json:"type"`
Text string `json:"text"`
}
type SlackBlock struct {
Type string `json:"type"`
Text SlackText `json:"text"`
}
type SlackBlocks struct {
Blocks []SlackBlock `json:"blocks"`
}
func main() {
http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {
// reread config so we don't have to restart the service for changes to take affect
config, configErr := getConfig()
if configErr != nil {
fmt.Println("Could not parse config")
return
}
defer r.Body.Close()
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Println("Error reading request body:", err)
return
}
eventType := r.Header.Get("X-GitHub-Event")
switch eventType {
case "push":
var pushEvent GitEvent
err := json.Unmarshal(body, &pushEvent)
if err != nil {
fmt.Println("Error unmarshaling push event:", err)
return
}
ref := pushEvent.Ref
if ref == config.Ref {
fmt.Println("New commit on branch")
files := getViolatingFiles(config, &pushEvent)
w.Header().Set("Content-Type", "application/json")
shouldAlert := len(files) > 0
if shouldAlert {
sendSlackMessage(config, files, pushEvent.Compare)
}
payload, _ := json.Marshal(WebhookResponse{Files: files, ShouldAlert: shouldAlert})
w.Write(payload)
return
}
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
})
config, configErr := getConfig()
if configErr != nil {
fmt.Println("Could not parse config")
return
}
err := http.ListenAndServe(fmt.Sprintf(":%s", config.Port), nil)
if err != nil {
fmt.Println("Error starting server:", err)
os.Exit(1)
}
}
func getConfig() (*Config, error) {
file, fileErr := os.ReadFile(os.Args[1])
if fileErr != nil {
return nil, fileErr
}
var config Config
err := json.Unmarshal(file, &config)
if err != nil {
return nil, err
}
return &config, nil
}
func getViolatingFiles(config *Config, pushEvent *GitEvent) []string {
patterns := getPatterns(config)
files := make([]string, 0)
for _, commit := range pushEvent.Commits {
files = append(files, getMatchingFiles(patterns, commit.Added)...)
files = append(files, getMatchingFiles(patterns, commit.Modified)...)
files = append(files, getMatchingFiles(patterns, commit.Removed)...)
}
return removeDuplicates(files)
}
func getPatterns(config *Config) []*regexp.Regexp {
patterns := make([]*regexp.Regexp, len(config.Patterns))
for i, p := range config.Patterns {
r, err := regexp.Compile(p)
if err != nil {
panic(err)
}
patterns[i] = r
}
return patterns
}
func getMatchingFiles(patterns []*regexp.Regexp, filenames []string) []string {
matches := make([]string, 0)
for _, filename := range filenames {
for _, pattern := range patterns {
if pattern.MatchString(filename) {
matches = append(matches, filename)
break
}
}
}
return matches
}
func removeDuplicates(strs []string) []string {
seen := make(map[string]bool)
result := make([]string, 0)
for _, str := range strs {
if !seen[str] {
seen[str] = true
result = append(result, str)
}
}
return result
}
func sendSlackMessage(config *Config, files []string, compare string) {
blocks := []SlackBlock{{
Type: "header",
Text: SlackText{
Type: "plain_text",
Text: "Someone is changing flagged files",
},
}}
blocks = append(blocks, SlackBlock{
Type: "section",
Text: SlackText{
Type: "mrkdwn",
Text: fmt.Sprintf("```%s```", strings.Join(files, "\n")),
},
})
blocks = append(blocks, SlackBlock{
Type: "section",
Text: SlackText{
Type: "mrkdwn",
Text: fmt.Sprintf("<%s|Link to github>", compare),
},
})
jsonStr, _ := json.Marshal(SlackBlocks{Blocks: blocks})
_, sendErr := http.Post(
config.SlackWebhook,
"application/json",
bytes.NewBuffer(jsonStr),
)
if sendErr != nil {
fmt.Println("Could not send to slack")
}
}