-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformation.repo.go
253 lines (186 loc) · 6.8 KB
/
transformation.repo.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
package github
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"regexp"
"strconv"
gogithub "github.com/google/go-github/v48/github"
"github.com/whosonfirst/go-webhookd/v3"
"github.com/whosonfirst/go-webhookd/v3/transformation"
)
func init() {
ctx := context.Background()
err := transformation.RegisterTransformation(ctx, "githubrepo", NewGitHubRepoTransformation)
if err != nil {
panic(err)
}
}
// see also: https://github.com/whosonfirst/go-whosonfirst-updated/issues/8
// GitHubRepoTransformation implements the `webhookd.WebhookTransformation` interface for transforming GitHub
// commit webhook messages in to the name of the repository where the commit occurred.
type GitHubRepoTransformation struct {
webhookd.WebhookTransformation
// ExcludeAdditions is a boolean flag to exclude newly added files from consideration.
ExcludeAdditions bool
// ExcludeModifications is a boolean flag to exclude updated (modified) files from consideration.
ExcludeModifications bool
// ExcludeDeletions is a boolean flag to exclude updated (modified) files from consideration.
ExcludeDeletions bool
// A boolean flag signaling the commit message should be prepended to the top of the final output in the form of '#message {COMMIT_MESSAGE}'
prepend_message bool
// A boolean flag signaling the commit author should be prepended to the top of the final output in the form of '#author {COMMIT_AUTHOR}'
prepend_author bool
// An optional regular expression that will be compared to the commit message; if it matches the transformer will return an error with code `webhookd.HaltEvent`
halt_on_message *regexp.Regexp
// An optional regular expression that will be compared to the commit author; if it matches the transformer will return an error with code `webhookd.HaltEvent`
halt_on_author *regexp.Regexp
}
// NewGitHubRepoTransformation() creates a new `GitHubRepoTransformation` instance, configured by 'uri'
// which is expected to take the form of:
//
// githubrepo://?{PARAMETERS}
//
// Where {PARAMTERS} is:
// * `?exclude_additions` An optional boolean value to exclude newly added files from consideration.
// * `?exclude_modifications` An optional boolean value to exclude update (modified) files from consideration.
// * `?exclude_deletions` An optional boolean value to exclude deleted files from consideration.
// * `?prepend_message` An optional boolean value to prepend the commit message to the final output. This takes the form of '#message {COMMIT_MESSAGE}'
// * `?prepend_author` An optional boolean value to prepend the name of the commit author to the final output. This takes the form of '#author {COMMIT_AUTHOR}'
// * `?halt_on_message` An optional regular expression that will be compared to the commit message; if it matches the transformer will return an error with code `webhookd.HaltEvent`
// * `?halt_on_author` An optional regular expression that will be compared to the commit author; if it matches the transformer will return an error with code `webhookd.HaltEvent`
func NewGitHubRepoTransformation(ctx context.Context, uri string) (webhookd.WebhookTransformation, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("Failed to parse URI, %w", err)
}
q := u.Query()
q_additions := q.Get("exclude_additions")
q_modifications := q.Get("exclude_modifications")
q_deletions := q.Get("exclude_deletions")
q_message := q.Get("prepend_message")
q_author := q.Get("prepend_author")
q_halt_on_message := q.Get("halt_on_message")
q_halt_on_author := q.Get("halt_on_author")
exclude_additions := false
exclude_modifications := false
exclude_deletions := false
prepend_message := false
prepend_author := false
if q_additions != "" {
v, err := strconv.ParseBool(q_additions)
if err != nil {
return nil, fmt.Errorf("Failed to parse '%s', %w", q_additions, err)
}
exclude_additions = v
}
if q_modifications != "" {
v, err := strconv.ParseBool(q_modifications)
if err != nil {
return nil, fmt.Errorf("Failed to parse '%s', %w", q_modifications, err)
}
exclude_modifications = v
}
if q_deletions != "" {
v, err := strconv.ParseBool(q_deletions)
if err != nil {
return nil, fmt.Errorf("Failed to parse '%s', %w", q_deletions, err)
}
exclude_deletions = v
}
if q_message != "" {
v, err := strconv.ParseBool(q_message)
if err != nil {
return nil, fmt.Errorf("Failed to parse '%s', %w", q_message, err)
}
prepend_message = v
}
if q_author != "" {
v, err := strconv.ParseBool(q_author)
if err != nil {
return nil, fmt.Errorf("Failed to parse '%s', %w", q_author, err)
}
prepend_author = v
}
p := GitHubRepoTransformation{
ExcludeAdditions: exclude_additions,
ExcludeModifications: exclude_modifications,
ExcludeDeletions: exclude_deletions,
prepend_message: prepend_message,
prepend_author: prepend_author,
}
if q_halt_on_message != "" {
r, err := regexp.Compile(q_halt_on_message)
if err != nil {
return nil, fmt.Errorf("Failed to parse ?halt_on_message= parameter, %w", err)
}
p.halt_on_message = r
}
if q_halt_on_author != "" {
r, err := regexp.Compile(q_halt_on_author)
if err != nil {
return nil, fmt.Errorf("Failed to parse ?halt_on_author= parameter, %w", err)
}
p.halt_on_author = r
}
return &p, nil
}
// Transform() transforms 'body' (which is assumed to be a GitHub commit webhook message) in to name of the repository
// where the commit occurred.
func (p *GitHubRepoTransformation) Transform(ctx context.Context, body []byte) ([]byte, *webhookd.WebhookError) {
select {
case <-ctx.Done():
return nil, nil
default:
// pass
}
var event gogithub.PushEvent
err := json.Unmarshal(body, &event)
if err != nil {
err := &webhookd.WebhookError{Code: 999, Message: err.Error()}
return nil, err
}
if p.halt_on_message != nil && p.halt_on_message.MatchString(*event.HeadCommit.Message) {
err := &webhookd.WebhookError{Code: webhookd.HaltEvent, Message: "Halt"}
return nil, err
}
if p.halt_on_author != nil && p.halt_on_author.MatchString(*event.HeadCommit.Author.Name) {
err := &webhookd.WebhookError{Code: webhookd.HaltEvent, Message: "Halt"}
return nil, err
}
buf := new(bytes.Buffer)
repo := event.Repo
repo_name := *repo.Name
has_updates := false
for _, c := range event.Commits {
if !p.ExcludeAdditions {
if len(c.Added) > 0 {
has_updates = true
}
}
if !p.ExcludeModifications {
if len(c.Modified) > 0 {
has_updates = true
}
}
if !p.ExcludeDeletions {
if len(c.Removed) > 0 {
has_updates = true
}
}
}
if has_updates {
if p.prepend_message {
msg := fmt.Sprintf("#message %s\n", *event.HeadCommit.Message)
buf.WriteString(msg)
}
if p.prepend_author {
msg := fmt.Sprintf("#author %s\n", *event.HeadCommit.Author.Name)
buf.WriteString(msg)
}
buf.WriteString(repo_name)
}
return buf.Bytes(), nil
}