mirror of
https://github.com/expo/phabulous.git
synced 2026-11-27 16:46:14 -10:00
117 lines
2.8 KiB
Go
117 lines
2.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"github.com/Sirupsen/logrus"
|
|
"github.com/etcinit/gonduit/constants"
|
|
"github.com/etcinit/phabulous/app/bot"
|
|
"github.com/etcinit/phabulous/app/factories"
|
|
"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:""`
|
|
Slacker *bot.SlackService `inject:""`
|
|
Factory *factories.GonduitFactory `inject:""`
|
|
Commits *resolvers.CommitResolver `inject:""`
|
|
Tasks *resolvers.TaskResolver `inject:""`
|
|
Differential *resolvers.DifferentialResolver `inject:""`
|
|
Logger *logrus.Logger `inject:""`
|
|
}
|
|
|
|
// 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 {
|
|
panic(err)
|
|
}
|
|
|
|
c.Request.ParseForm()
|
|
|
|
f.Logger.Debug(c.Request.PostForm.Encode())
|
|
|
|
res, err := conduit.PHIDQuerySingle(
|
|
string(c.Request.PostForm.Get("storyData[objectPHID]")),
|
|
)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
storyText := c.Request.PostForm.Get("storyText")
|
|
|
|
if res.URI != "" {
|
|
storyText += " (<" + res.URI + "|More info>)"
|
|
}
|
|
|
|
phidType := constants.PhidType(res.Type)
|
|
icon := messages.PhidTypeToIcon(phidType)
|
|
|
|
f.Slacker.FeedPost(storyText)
|
|
|
|
switch phidType {
|
|
case constants.PhidTypeCommit:
|
|
channelName, err := f.Commits.Resolve(res.Name)
|
|
if err != nil {
|
|
f.Logger.Error(err)
|
|
}
|
|
|
|
if channelName != "" {
|
|
f.Slacker.SimplePost(channelName, storyText, icon, false)
|
|
}
|
|
|
|
// Support "all" channel.
|
|
channelMap := f.Config.GetStringMapString("channels.repositories")
|
|
|
|
if channelName, ok := channelMap["all"]; ok == true {
|
|
f.Slacker.SimplePost(channelName, storyText, icon, false)
|
|
}
|
|
break
|
|
case constants.PhidTypeTask:
|
|
channelName, err := f.Tasks.Resolve(res.PHID)
|
|
if err != nil {
|
|
f.Logger.Error(err)
|
|
}
|
|
|
|
if channelName != "" {
|
|
f.Slacker.SimplePost(channelName, storyText, icon, false)
|
|
}
|
|
break
|
|
case constants.PhidTypeDifferentialRevision:
|
|
channelName, err := f.Differential.Resolve(res.PHID)
|
|
if err != nil {
|
|
f.Logger.Error(err)
|
|
}
|
|
|
|
if channelName != "" {
|
|
f.Slacker.SimplePost(channelName, storyText, icon, false)
|
|
}
|
|
|
|
// Support "all" channel.
|
|
channelMap := f.Config.GetStringMapString("channels.repositories")
|
|
|
|
if channelName, ok := channelMap["all"]; ok == true {
|
|
f.Slacker.SimplePost(channelName, storyText, icon, false)
|
|
}
|
|
break
|
|
}
|
|
|
|
c.JSON(200, gin.H{
|
|
"status": "success",
|
|
"messages": []string{
|
|
"OK",
|
|
},
|
|
})
|
|
}
|