-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathfeed.go
More file actions
141 lines (115 loc) · 3.42 KB
/
feed.go
File metadata and controls
141 lines (115 loc) · 3.42 KB
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
package controllers
import (
"github.com/Sirupsen/logrus"
"github.com/etcinit/gonduit/constants"
"github.com/etcinit/phabulous/app/factories"
"github.com/etcinit/phabulous/app/interfaces"
"github.com/etcinit/phabulous/app/messages"
"github.com/etcinit/phabulous/app/resolvers"
"github.com/gin-gonic/gin"
"github.com/jacobstr/confer"
)
// FeedController handles feed webhook routes
type FeedController struct {
Config *confer.Config `inject:""`
Factory *factories.GonduitFactory `inject:""`
Commits *resolvers.CommitResolver `inject:""`
Tasks *resolvers.TaskResolver `inject:""`
Differential *resolvers.DifferentialResolver `inject:""`
Logger *logrus.Logger `inject:""`
Connector interfaces.Connector
}
// Register registers the route handlers for this controller
func (f *FeedController) Register(r *gin.RouterGroup) {
front := r.Group("/feed")
{
front.POST("/receive", f.postReceive)
}
}
func (f *FeedController) postReceive(c *gin.Context) {
conduit, err := f.Factory.Make()
if err != nil {
f.Logger.Error("ERROR making factory: ", err)
return
}
c.Request.ParseForm()
f.Logger.Debug(c.Request.PostForm.Encode())
inString := string(c.Request.PostForm.Get("storyData[objectPHID]"))
res, err := conduit.PHIDQuerySingle(inString)
if err != nil {
f.Logger.Error("ERROR with input ", inString, ": ", err)
return
}
storyText := c.Request.PostForm.Get("storyText")
if res.URI != "" {
storyText += " (<" + res.URI + "|More info>)"
}
phidType := constants.PhidType(res.Type)
icon := messages.PhidTypeToIcon(phidType)
if typeAllowed(f.Config, res.Type) {
f.Connector.PostOnFeed(storyText)
}
switch phidType {
case constants.PhidTypeCommit:
channelName, err := f.Commits.Resolve(res.Name)
if err != nil {
f.Logger.Error(err)
}
if channelName != "" {
f.Connector.Post(channelName, storyText, icon, false)
}
// Support "all" channel.
channelMap := f.Config.GetStringMapString("channels.repositories")
if channelName, ok := channelMap["all"]; ok == true {
f.Connector.Post(channelName, storyText, icon, false)
}
break
case constants.PhidTypeTask:
channelNames, err := f.Tasks.Resolve(res.PHID)
if err != nil {
f.Logger.Error(err)
}
f.Logger.Debug(channelNames)
for _,channelName := range channelNames {
f.Logger.Debug(channelName)
f.Connector.Post(channelName, storyText, icon, false)
f.Logger.Debug("posted")
}
break
case constants.PhidTypeDifferentialRevision:
channelName, err := f.Differential.Resolve(res.PHID)
if err != nil {
f.Logger.Error(err)
}
if channelName != "" {
f.Connector.Post(channelName, storyText, icon, false)
}
// Support "all" channel.
channelMap := f.Config.GetStringMapString("channels.repositories")
if channelName, ok := channelMap["all"]; ok == true {
f.Connector.Post(channelName, storyText, icon, false)
}
break
}
c.JSON(200, gin.H{
"status": "success",
"messages": []string{
"OK",
},
})
}
// typeAllowed returns whether a feed event should be processed in the main
// feed channel.
func typeAllowed(config *confer.Config, currentType string) bool {
allowedTypes := config.GetStringSlice("channels.feedTypes")
// If no filters are specified, everything is allowed.
if len(allowedTypes) == 0 {
return true
}
for _, eventType := range allowedTypes {
if eventType == currentType {
return true
}
}
return false
}