-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl_bitbucket.go
219 lines (171 loc) · 5.75 KB
/
url_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
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
package url
import (
"fmt"
"net/url"
"regexp"
"strings"
"time"
"github.com/seabird-chat/seabird-go/pb"
"github.com/seabird-irc/seabird-url-plugin/internal"
)
type bitbucketUser struct {
Username string `json:"username"`
DisplayName string `json:"display_name"`
}
type bitbucketRepo struct {
Scm string `json:"scm"`
Description string `json:"description"`
FullName string `json:"full_name"`
Language string `json:"language"`
UpdatedOn string `json:"updated_on"`
}
type bitbucketIssue struct {
Status string `json:"status"`
Priority string `json:"priority"`
Title string `json:"title"`
ReportedBy bitbucketUser `json:"reported_by"`
CommentCount int `json:"comment_count"`
CreatedOn string `json:"created_on"`
Metadata struct {
Kind string `json:"kind"`
} `json:"metadata"`
}
type bitbucketPullRequest struct {
State string `json:"state"`
Title string `json:"title"`
Author bitbucketUser `json:"author"`
CommentCount int `json:"comment_count"`
CreatedOn string `json:"created_on"`
}
var (
bitbucketUserRegex = regexp.MustCompile(`^/([^/]+)$`)
bitbucketRepoRegex = regexp.MustCompile(`^/([^/]+)/([^/]+)$`)
bitbucketIssueRegex = regexp.MustCompile(`^/([^/]+)/([^/]+)/issue/([^/]+)/[^/]+$`)
bitbucketPullRegex = regexp.MustCompile(`^/([^/]+)/([^/]+)/pull-request/([^/]+)/.*$`)
bitbucketPrefix = "[Bitbucket]"
userURL = "https://bitbucket.org/api/2.0/users/%s"
repoURL = "https://bitbucket.org/api/2.0/repositories/%s/%s"
repoIssuesURL = "https://bitbucket.org/api/1.0/repositories/%s/%s/issues/%s"
repoPullRequestsURL = "https://bitbucket.org/api/2.0/repositories/%s/%s/pullrequests/%s"
)
type BitbucketProvider struct{}
func NewBitbucketProvider() *BitbucketProvider {
return &BitbucketProvider{}
}
func (p *BitbucketProvider) GetCallbacks() map[string]URLCallback {
return map[string]URLCallback{
"bitbucket.org": bitbucketCallback,
}
}
func (p *BitbucketProvider) GetMessageCallback() MessageCallback {
return nil
}
func bitbucketCallback(c *Client, source *pb.ChannelSource, url *url.URL) bool {
//nolint:gocritic
if bitbucketUserRegex.MatchString(url.Path) {
return bitbucketGetUser(c, source, url)
} else if bitbucketRepoRegex.MatchString(url.Path) {
return bitbucketGetRepo(c, source, url)
} else if bitbucketIssueRegex.MatchString(url.Path) {
return bitbucketGetIssue(c, source, url)
} else if bitbucketPullRegex.MatchString(url.Path) {
return bitbucketGetPull(c, source, url)
}
return false
}
func bitbucketGetUser(c *Client, source *pb.ChannelSource, url *url.URL) bool {
matches := bitbucketUserRegex.FindStringSubmatch(url.Path)
if len(matches) != 2 {
return false
}
user := matches[1]
bu := &bitbucketUser{}
if err := internal.GetJSON(fmt.Sprintf(userURL, user), bu); err != nil {
return false
}
// Jay Vana (@jsvana)
c.Replyf(source, "%s %s (@%s)", bitbucketPrefix, bu.DisplayName, bu.Username)
return true
}
func bitbucketGetRepo(c *Client, source *pb.ChannelSource, url *url.URL) bool {
matches := bitbucketRepoRegex.FindStringSubmatch(url.Path)
if len(matches) != 3 {
return false
}
user := matches[1]
repo := matches[2]
br := &bitbucketRepo{}
if err := internal.GetJSON(fmt.Sprintf(repoURL, user, repo), br); err != nil {
return false
}
// chriskempson/base16-iterm2 [Shell] Last pushed to 15 Nov 2014 - Base16 for iTerm2
out := br.FullName
if br.Language != "" {
out += " [" + br.Language + "]"
}
tm, err := time.Parse(time.RFC3339, br.UpdatedOn)
if err != nil {
return false
}
out += " Last pushed to " + tm.Format("2 Jan 2006")
c.Replyf(source, "%s %s", bitbucketPrefix, out)
return true
}
func bitbucketGetIssue(c *Client, source *pb.ChannelSource, url *url.URL) bool {
matches := bitbucketIssueRegex.FindStringSubmatch(url.Path)
if len(matches) != 4 {
return false
}
user := matches[1]
repo := matches[2]
issueNum := matches[3]
bi := &bitbucketIssue{}
if err := internal.GetJSON(fmt.Sprintf(repoIssuesURL, user, repo, issueNum), bi); err != nil {
return false
}
// If there isn't a user, we can probably assume they're anonymous
if bi.ReportedBy.Username == "" {
bi.ReportedBy.Username = "Anonymous"
}
// Issue #51 on belak/go-seabird [open] - Expand issues plugin with more of Bitbucket [created 3 Jan 2015]
out := fmt.Sprintf("Issue #%s on %s/%s [%s]", issueNum, user, repo, bi.Status)
if bi.Priority != "" && bi.Metadata.Kind != "" {
out += " [" + bi.Priority + " - " + bi.Metadata.Kind + "]"
}
out += " by " + bi.ReportedBy.Username
if bi.Title != "" {
out += " - " + bi.Title
}
tm, err := time.Parse("2006-01-02T15:04:05.000", bi.CreatedOn)
if err != nil {
return false
}
out += " [created " + tm.Format("2 Jan 2006") + "]"
c.Replyf(source, "%s %s", bitbucketPrefix, out)
return true
}
func bitbucketGetPull(c *Client, source *pb.ChannelSource, url *url.URL) bool {
matches := bitbucketPullRegex.FindStringSubmatch(url.Path)
if len(matches) != 4 {
return false
}
user := matches[1]
repo := matches[2]
pullNum := matches[3]
bpr := &bitbucketPullRequest{}
if err := internal.GetJSON(fmt.Sprintf(repoPullRequestsURL, user, repo, pullNum), bpr); err != nil {
return false
}
// Pull request #59 on belak/go-seabird created by jsvana [open] - Add stuff to links [created 4 Jan 2015]
out := fmt.Sprintf("Pull request #%s on %s/%s created by %s [%s]", pullNum, user, repo, bpr.Author.Username, strings.ToLower(bpr.State))
if bpr.Title != "" {
out += " - " + bpr.Title
}
tm, err := time.Parse("2006-01-02T15:04:05.000000-07:00", bpr.CreatedOn)
if err != nil {
return false
}
out += " [created " + tm.Format("2 Jan 2006") + "]"
c.Replyf(source, "%s %s", bitbucketPrefix, out)
return true
}