PLT-7408 Move webhook handling into api4 package to fix EnableAPIv3 config setting (#7219)
* Move webhook handling into api4 package to fix EnableAPIv3 config setting * Fix unit test
Этот коммит содержится в:
коммит произвёл
Saturnino Abril
родитель
7d3719b5c6
Коммит
0033e3e37b
@@ -4,12 +4,9 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
"github.com/gorilla/mux"
|
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
@@ -28,12 +25,6 @@ func InitWebhook() {
|
|||||||
BaseRoutes.Hooks.Handle("/outgoing/regen_token", ApiUserRequired(regenOutgoingHookToken)).Methods("POST")
|
BaseRoutes.Hooks.Handle("/outgoing/regen_token", ApiUserRequired(regenOutgoingHookToken)).Methods("POST")
|
||||||
BaseRoutes.Hooks.Handle("/outgoing/delete", ApiUserRequired(deleteOutgoingHook)).Methods("POST")
|
BaseRoutes.Hooks.Handle("/outgoing/delete", ApiUserRequired(deleteOutgoingHook)).Methods("POST")
|
||||||
BaseRoutes.Hooks.Handle("/outgoing/list", ApiUserRequired(getOutgoingHooks)).Methods("GET")
|
BaseRoutes.Hooks.Handle("/outgoing/list", ApiUserRequired(getOutgoingHooks)).Methods("GET")
|
||||||
|
|
||||||
BaseRoutes.Hooks.Handle("/{id:[A-Za-z0-9]+}", ApiAppHandler(incomingWebhook)).Methods("POST")
|
|
||||||
|
|
||||||
// Old route. Remove eventually.
|
|
||||||
mr := app.Srv.Router
|
|
||||||
mr.Handle("/hooks/{id:[A-Za-z0-9]+}", ApiAppHandler(incomingWebhook)).Methods("POST")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -340,46 +331,3 @@ func regenOutgoingHookToken(c *Context, w http.ResponseWriter, r *http.Request)
|
|||||||
w.Write([]byte(rhook.ToJson()))
|
w.Write([]byte(rhook.ToJson()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
||||||
params := mux.Vars(r)
|
|
||||||
id := params["id"]
|
|
||||||
|
|
||||||
r.ParseForm()
|
|
||||||
|
|
||||||
var payload io.Reader
|
|
||||||
contentType := r.Header.Get("Content-Type")
|
|
||||||
if strings.Split(contentType, "; ")[0] == "application/x-www-form-urlencoded" {
|
|
||||||
payload = strings.NewReader(r.FormValue("payload"))
|
|
||||||
} else {
|
|
||||||
payload = r.Body
|
|
||||||
}
|
|
||||||
|
|
||||||
if utils.Cfg.LogSettings.EnableWebhookDebugging {
|
|
||||||
var err error
|
|
||||||
payload, err = utils.DebugReader(
|
|
||||||
payload,
|
|
||||||
utils.T("api.webhook.incoming.debug"),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
c.Err = model.NewLocAppError(
|
|
||||||
"incomingWebhook",
|
|
||||||
"api.webhook.incoming.debug.error",
|
|
||||||
nil,
|
|
||||||
err.Error(),
|
|
||||||
)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
parsedRequest := model.IncomingWebhookRequestFromJson(payload)
|
|
||||||
|
|
||||||
err := app.HandleIncomingWebhook(id, parsedRequest)
|
|
||||||
if err != nil {
|
|
||||||
c.Err = err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "text/plain")
|
|
||||||
w.Write([]byte("ok"))
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,9 +4,12 @@
|
|||||||
package api4
|
package api4
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
|
|
||||||
l4g "github.com/alecthomas/log4go"
|
l4g "github.com/alecthomas/log4go"
|
||||||
|
"github.com/gorilla/mux"
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/utils"
|
"github.com/mattermost/platform/utils"
|
||||||
@@ -27,6 +30,11 @@ func InitWebhook() {
|
|||||||
BaseRoutes.OutgoingHook.Handle("", ApiSessionRequired(updateOutgoingHook)).Methods("PUT")
|
BaseRoutes.OutgoingHook.Handle("", ApiSessionRequired(updateOutgoingHook)).Methods("PUT")
|
||||||
BaseRoutes.OutgoingHook.Handle("", ApiSessionRequired(deleteOutgoingHook)).Methods("DELETE")
|
BaseRoutes.OutgoingHook.Handle("", ApiSessionRequired(deleteOutgoingHook)).Methods("DELETE")
|
||||||
BaseRoutes.OutgoingHook.Handle("/regen_token", ApiSessionRequired(regenOutgoingHookToken)).Methods("POST")
|
BaseRoutes.OutgoingHook.Handle("/regen_token", ApiSessionRequired(regenOutgoingHookToken)).Methods("POST")
|
||||||
|
|
||||||
|
BaseRoutes.Root.Handle("/hooks/{id:[A-Za-z0-9]+}", ApiHandler(incomingWebhook)).Methods("POST")
|
||||||
|
|
||||||
|
// Old endpoint for backwards compatibility
|
||||||
|
BaseRoutes.Root.Handle("/api/v3/teams/{team_id:[A-Za-z0-9]+}/hooks/{id:[A-Za-z0-9]+}", ApiHandler(incomingWebhook)).Methods("POST")
|
||||||
}
|
}
|
||||||
|
|
||||||
func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
func createIncomingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
@@ -435,3 +443,46 @@ func deleteOutgoingHook(c *Context, w http.ResponseWriter, r *http.Request) {
|
|||||||
c.LogAudit("success")
|
c.LogAudit("success")
|
||||||
ReturnStatusOK(w)
|
ReturnStatusOK(w)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func incomingWebhook(c *Context, w http.ResponseWriter, r *http.Request) {
|
||||||
|
params := mux.Vars(r)
|
||||||
|
id := params["id"]
|
||||||
|
|
||||||
|
r.ParseForm()
|
||||||
|
|
||||||
|
var payload io.Reader
|
||||||
|
contentType := r.Header.Get("Content-Type")
|
||||||
|
if strings.Split(contentType, "; ")[0] == "application/x-www-form-urlencoded" {
|
||||||
|
payload = strings.NewReader(r.FormValue("payload"))
|
||||||
|
} else {
|
||||||
|
payload = r.Body
|
||||||
|
}
|
||||||
|
|
||||||
|
if utils.Cfg.LogSettings.EnableWebhookDebugging {
|
||||||
|
var err error
|
||||||
|
payload, err = utils.DebugReader(
|
||||||
|
payload,
|
||||||
|
utils.T("api.webhook.incoming.debug"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
c.Err = model.NewLocAppError(
|
||||||
|
"incomingWebhook",
|
||||||
|
"api.webhook.incoming.debug.error",
|
||||||
|
nil,
|
||||||
|
err.Error(),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedRequest := model.IncomingWebhookRequestFromJson(payload)
|
||||||
|
|
||||||
|
err := app.HandleIncomingWebhook(id, parsedRequest)
|
||||||
|
if err != nil {
|
||||||
|
c.Err = err
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "text/plain")
|
||||||
|
w.Write([]byte("ok"))
|
||||||
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/mattermost/platform/api"
|
"github.com/mattermost/platform/api"
|
||||||
|
"github.com/mattermost/platform/api4"
|
||||||
"github.com/mattermost/platform/app"
|
"github.com/mattermost/platform/app"
|
||||||
"github.com/mattermost/platform/model"
|
"github.com/mattermost/platform/model"
|
||||||
"github.com/mattermost/platform/store"
|
"github.com/mattermost/platform/store"
|
||||||
@@ -26,6 +27,7 @@ func Setup() {
|
|||||||
app.InitStores()
|
app.InitStores()
|
||||||
api.InitRouter()
|
api.InitRouter()
|
||||||
app.StartServer()
|
app.StartServer()
|
||||||
|
api4.InitApi(false)
|
||||||
api.InitApi()
|
api.InitApi()
|
||||||
InitWeb()
|
InitWeb()
|
||||||
URL = "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress
|
URL = "http://localhost" + utils.Cfg.ServiceSettings.ListenAddress
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user