-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitbucket.go
85 lines (73 loc) · 1.8 KB
/
bitbucket.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
package main
import (
"encoding/json"
"log"
"net/http"
)
// See https://confluence.atlassian.com/display/BITBUCKET/POST+hook+management
type BitbucketNotification struct {
CanonURL string `json:"Canon_url"`
User string
Repository BitbucketRepository
Truncated bool
Commits []BitbucketCommit
}
type BitbucketRepository struct {
AbsoluteURL string `json:"Absolute_url"`
Fork bool
IsPrivate bool `json:"Is_private"`
Name string
Owner string
Scm string
Slug string
Website string
}
type BitbucketCommit struct {
Author string
Branches []string
Branch string
Files []BitbucketFile
Message string
Node string
Parents []string
RawAuthor string `json:"Raw_author"`
RawNode string `json:"Raw_node"`
Revision int
Size int
Timestamp string
UTCTimestamp string
}
type BitbucketFile struct {
File string
Type string
}
func BitbucketParse(r *http.Request) (n Notification, err error) {
r.ParseForm()
payload := r.Form.Get("payload")
log.Printf("Received payload %s", payload)
bytes := []byte(payload)
return bitbucketParseBytes(bytes)
}
func bitbucketParseBytes(bytes []byte) (n BitbucketNotification, err error) {
err = json.Unmarshal(bytes, &n)
return
}
func (n BitbucketNotification) RepositoryURL() (repositoryURL string) {
repositoryURL = n.CanonURL + n.Repository.AbsoluteURL
if repositoryURL[len(repositoryURL)-1] == '/' {
repositoryURL = repositoryURL[:len(repositoryURL)-1]
}
return
}
func (n BitbucketNotification) Branches() (branches map[string]bool) {
branches = make(map[string]bool)
for _, commit := range n.Commits {
if commit.Branch != "" {
branches[commit.Branch] = true
}
for _, branch := range commit.Branches {
branches[branch] = true
}
}
return
}