-
Notifications
You must be signed in to change notification settings - Fork 10
/
handler_aha_out.go
164 lines (144 loc) · 4.13 KB
/
handler_aha_out.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
package aha
import (
"encoding/json"
"fmt"
"html"
"strings"
"time"
"github.com/grokify/chathooks/pkg/config"
"github.com/grokify/chathooks/pkg/handlers"
"github.com/grokify/chathooks/pkg/models"
cc "github.com/grokify/commonchat"
"github.com/microcosm-cc/bluemonday"
"github.com/rs/zerolog/log"
)
const (
DisplayName = "Aha!"
HandlerKey = "aha"
MessageDirection = "out"
DocumentationURL = "https://support.aha.io/hc/en-us/articles/202000997-Integrate-with-Webhooks"
MessageBodyType = models.JSON
)
func NewHandler() handlers.Handler {
return handlers.Handler{MessageBodyType: MessageBodyType, Normalize: Normalize}
}
func Normalize(cfg config.Configuration, hReq handlers.HandlerRequest) (cc.Message, error) {
ccMsg := cc.NewMessage()
iconURL, err := cfg.GetAppIconURL(HandlerKey)
if err == nil {
ccMsg.IconURL = iconURL.String()
}
src, err := AhaOutMessageFromBytes(hReq.Body)
if err != nil {
return ccMsg, err
}
ccMsg.Title = src.Title()
p := bluemonday.StrictPolicy()
if src.Audit != nil && len(src.Audit.Changes) > 0 {
attachment := cc.NewAttachment()
for _, change := range src.Audit.Changes {
field := cc.Field{}
key := strings.TrimSpace(change.FieldName)
val := strings.TrimSpace(change.Value)
val = p.Sanitize(val)
val = html.UnescapeString(val)
addField := false
if len(key) > 0 {
field.Title = key
addField = true
}
if len(val) > 0 {
field.Value = val
addField = true
}
if key != "Description" {
field.Short = true
}
if addField {
attachment.AddField(field)
}
}
ccMsg.AddAttachment(attachment)
}
return ccMsg, nil
}
func AhaOutMessageFromBytes(bytes []byte) (AhaOutMessage, error) {
log.Debug().
Str("type", "message.raw").
Str("inbound_body", string(bytes)).
Str("handler", HandlerKey).
Msg(config.InfoInputMessageParseBegin)
resp := AhaOutMessage{}
err := json.Unmarshal(bytes, &resp)
if err != nil {
log.Warn().
Err(err).
Str("handler", HandlerKey).
Str("request_body", string(bytes)).
Msg(config.ErrorInputMessageParseFailed)
}
return resp, err
}
type AhaOutMessage struct {
Event string `json:"event,omitempty"`
Audit *AhaOutAudit `json:"audit,omitempty"`
}
func (aom *AhaOutMessage) Activity() string {
if aom.Audit == nil {
return ""
}
return aom.Audit.Activity()
}
func (aom *AhaOutMessage) Title() string {
if aom.Audit == nil {
return ""
}
return aom.Audit.Title()
}
type AhaOutAudit struct {
ID string `json:"id,omitempty"`
AuditAction string `json:"audit_action,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
Interesting bool `json:"interesting,omitempty"`
AuditableType string `json:"auditable_type,omitempty"`
AuditableID string `json:"auditable_id,omitempty"`
User *AhaOutUser `json:"user,omitempty"`
Description string `json:"description,omitempty"`
AuditableURL string `json:"auditable_url,omitempty"`
Changes []AhaOutChange `json:"changes,omitempty"`
}
func (aoa *AhaOutAudit) Activity() string {
return fmt.Sprintf("%v %v %v", DisplayName, aoa.AuditAction, aoa.AuditableType)
}
func (aoa *AhaOutAudit) Title() string {
username := strings.TrimSpace(aoa.User.Name)
description := strings.TrimSpace(aoa.Description)
itemURL := strings.TrimSpace(aoa.AuditableURL)
title := ""
if len(description) > 0 && len(itemURL) > 0 {
title = fmt.Sprintf("[%v](%v)", description, itemURL)
} else if len(itemURL) > 0 {
title = fmt.Sprintf("[%v](%v)", itemURL, itemURL)
} else if len(description) > 0 {
title = description
}
if len(username) > 0 {
if len(title) > 0 {
title = fmt.Sprintf("**%v** %v", username, title)
} else {
title = fmt.Sprintf("**%v**", username)
}
}
return title
}
type AhaOutUser struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
CreatedAt time.Time `json:"created_at,omitempty"`
UpdatedAt time.Time `json:"updated_at,omitempty"`
}
type AhaOutChange struct {
FieldName string `json:"field_name,omitempty"`
Value string `json:"value,omitempty"`
}