-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathjira-comment-hook.go
174 lines (149 loc) · 3.78 KB
/
jira-comment-hook.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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"regexp"
"strings"
"time"
"github.com/parnurzeal/gorequest"
)
type Feed struct {
XMLName xml.Name `xml:"feed"`
Id string `xml:"id"`
Title string `xml:"title"`
Entries []Entry `xml:"entry"`
}
type Entry struct {
XMLName xml.Name `xml:"entry"`
Id string `xml:"id"`
Title string `xml:"title"`
Content string `xml:"content"`
Author Author `xml:"author"`
Category Category `xml:"category"`
Updated customTime `xml:"updated"`
}
type customTime struct {
time.Time
}
func (c *customTime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var v string
d.DecodeElement(&v, &start)
parse, _ := time.Parse(time.RFC3339Nano, v)
*c = customTime{parse}
return nil
}
func (c *customTime) UnmarshalXMLAttr(attr xml.Attr) error {
fmt.Printf("time:%v\n", attr.Value)
parse, _ := time.Parse(time.RFC3339Nano, attr.Value)
*c = customTime{parse}
return nil
}
type Category struct {
XMLName xml.Name `xml:"category"`
Term string `xml:"term,attr"`
}
type Author struct {
XMLName xml.Name `xml:"author"`
Name string `xml:"name"`
Email string `xml:"email"`
}
func (e Entry) isComment() bool {
return e.Category.Term == "comment"
}
func GetFeed() Feed {
client := &http.Client{}
req, _ := http.NewRequest("GET", os.Getenv("JIRA_URL")+"/activity?maxItems=100", nil)
req.SetBasicAuth(os.Getenv("JIRA_USERNAME"), os.Getenv("JIRA_PASSWORD"))
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err)
}
var f Feed
xml.Unmarshal(body, &f)
return f
}
func HtmlToSlackMarkup(s string) string {
//replace <a href> with the slack syntax
rp := regexp.MustCompile(`<a[^>]*href="(?P<URL>[^"]*)"[^>]*>(?P<LinkText>[^<]*)</a>`)
output := rp.ReplaceAllString(s, "<$URL|$LinkText>")
//honor the <p> tags
output = strings.Replace(output, `<p>`, `\n\n`, -1)
//remove all the html tags
rp2 := regexp.MustCompile(`<[^>|]*>`)
output = rp2.ReplaceAllString(output, "")
//clean up white space - two or more times
rp3 := regexp.MustCompile(`\s{2,}`)
output = rp3.ReplaceAllString(output, " ")
//clean up white space - two or more times
rp4 := regexp.MustCompile(`\r\n|\n|\r`)
output = rp4.ReplaceAllString(output, `\n`)
return output
}
func SendRichPostToSlack(title string, comment string) {
message := `
{
"attachments": [
{
"fallback":"%v",
"pretext":"%v",
"color":"#0000D0",
"fields":[
{
"value":"%v",
"short":false
}
]
}
]
}`
payload := fmt.Sprintf(message, title, title, comment)
fmt.Println("payload:")
fmt.Println(payload)
SendPayload(payload)
}
func SendPayload(payload string) {
request := gorequest.New()
_, _, errs := request.Post(os.Getenv("SLACK_WEBHOOK")).
Send(payload).
End()
if errs != nil {
log.Fatal(errs)
}
}
func SyncSlackMessages(anchor time.Time) {
log.Print("Syncing Slack Messages")
feed := GetFeed()
for _, e := range feed.Entries {
if e.isComment() && e.Updated.After(anchor) {
log.Print("-----------------------")
log.Print("Identified new message:", e.Id)
title := HtmlToSlackMarkup(e.Title)
content := HtmlToSlackMarkup(e.Content)
log.Print("Sanitized title: ", title)
log.Print("Sanitized content:", content)
SendRichPostToSlack(title, content)
log.Print("-----------------------")
}
}
}
func main() {
//do a full sync of legacy mesages
lastSyncTime := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
SyncSlackMessages(lastSyncTime)
lastSyncTime = time.Now()
//now do every 10 seconds
c := time.Tick(10 * time.Second)
for now := range c {
SyncSlackMessages(lastSyncTime)
lastSyncTime = now
}
}