/
github.repo.go
78 lines (56 loc) · 1.44 KB
/
github.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
package transformations
import (
"bytes"
"encoding/json"
gogithub "github.com/google/go-github/github"
"github.com/whosonfirst/go-webhookd"
_ "log"
)
// see also: https://github.com/whosonfirst/go-whosonfirst-updated/issues/8
type GitHubRepoTransformation struct {
webhookd.WebhookTransformation
ExcludeAdditions bool
ExcludeModifications bool
ExcludeDeletions bool
}
func NewGitHubRepoTransformation(exclude_additions bool, exclude_modifications bool, exclude_deletions bool) (*GitHubRepoTransformation, error) {
p := GitHubRepoTransformation{
ExcludeAdditions: exclude_additions,
ExcludeModifications: exclude_modifications,
ExcludeDeletions: exclude_deletions,
}
return &p, nil
}
func (p *GitHubRepoTransformation) Transform(body []byte) ([]byte, *webhookd.WebhookError) {
var event gogithub.PushEvent
err := json.Unmarshal(body, &event)
if err != nil {
err := &webhookd.WebhookError{Code: 999, Message: err.Error()}
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 {
buf.WriteString(repo_name)
}
return buf.Bytes(), nil
}