[MM-47846] Execute Work Template (#22055)

Этот коммит содержится в:
Julien Tant
2023-01-25 10:24:01 -07:00
коммит произвёл GitHub
родитель 53938167f8
Коммит 753ee85c6b
24 изменённых файлов: 1710 добавлений и 380 удалений

147
api4/work_templates.go Обычный файл
Просмотреть файл

@@ -0,0 +1,147 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"encoding/json"
"net/http"
"github.com/mattermost/mattermost-server/v6/app/worktemplates"
"github.com/mattermost/mattermost-server/v6/model"
)
func (api *API) InitWorkTemplate() {
api.BaseRoutes.WorkTemplates.Handle("/categories", api.APISessionRequired(getWorkTemplateCategories)).Methods("GET")
api.BaseRoutes.WorkTemplates.Handle("/categories/{category}/templates", api.APISessionRequired(getWorkTemplates)).Methods("GET")
api.BaseRoutes.WorkTemplates.Handle("/execute", api.APIHandler(executeWorkTemplate)).Methods("POST")
}
func areWorkTemplatesEnabled(c *Context) *model.AppError {
if !c.App.Config().FeatureFlags.WorkTemplate {
return model.NewAppError("areWorkTemplatesEnabled", "api.work_templates.disabled", nil, "feature flag is off", http.StatusNotFound)
}
// we have to make sure that playbooks plugin is enabled and board is a product
pbActive, err := c.App.IsPluginActive(model.PluginIdPlaybooks)
if err != nil {
return model.NewAppError("areWorkTemplatesEnabled", "api.work_templates.disabled", nil, "", http.StatusInternalServerError).Wrap(err)
}
if !pbActive {
return model.NewAppError("areWorkTemplatesEnabled", "api.work_templates.disabled", nil, "playbook plugin not active", http.StatusNotFound)
}
hasBoard, err := c.App.HasBoardProduct()
if err != nil {
return model.NewAppError("areWorkTemplatesEnabled", "api.work_templates.disabled", nil, "", http.StatusInternalServerError).Wrap(err)
}
if !hasBoard {
return model.NewAppError("areWorkTemplatesEnabled", "api.work_templates.disabled", nil, "board product not found", http.StatusNotFound)
}
return nil
}
func getWorkTemplateCategories(c *Context, w http.ResponseWriter, r *http.Request) {
appErr := areWorkTemplatesEnabled(c)
if appErr != nil {
c.Err = appErr
return
}
t := c.AppContext.GetT()
categories, appErr := c.App.GetWorkTemplateCategories(t)
if appErr != nil {
c.Err = appErr
return
}
b, err := json.Marshal(categories)
if err != nil {
c.Err = model.NewAppError("getWorkTemplateCategories", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return
}
w.Write(b)
}
func getWorkTemplates(c *Context, w http.ResponseWriter, r *http.Request) {
appErr := areWorkTemplatesEnabled(c)
if appErr != nil {
c.Err = appErr
return
}
c.RequireCategory()
if c.Err != nil {
return
}
t := c.AppContext.GetT()
workTemplates, appErr := c.App.GetWorkTemplates(c.Params.Category, c.App.Config().FeatureFlags.ToMap(), t)
if appErr != nil {
c.Err = appErr
return
}
b, err := json.Marshal(workTemplates)
if err != nil {
c.Err = model.NewAppError("getWorkTemplates", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return
}
w.Write(b)
}
func executeWorkTemplate(c *Context, w http.ResponseWriter, r *http.Request) {
appErr := areWorkTemplatesEnabled(c)
if appErr != nil {
c.Err = appErr
return
}
wtcr := &worktemplates.ExecutionRequest{}
err := json.NewDecoder(r.Body).Decode(wtcr)
if err != nil {
c.Err = model.NewAppError("executeWorkTemplate", "api.unmarshal_error", nil, "", http.StatusBadRequest).Wrap(err)
return
}
canCreatePublicChannel := c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), wtcr.TeamID, model.PermissionCreatePublicChannel)
canCreatePrivateChannel := c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), wtcr.TeamID, model.PermissionCreatePrivateChannel)
// focalboard uses channel permissions for board creation
canCreatePublicBoard := canCreatePublicChannel
canCreatePrivateBoard := canCreatePrivateChannel
canCreatePublicPlaybook := c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), wtcr.TeamID, model.PermissionPublicPlaybookCreate)
canCreatePrivatePlaybook := c.App.SessionHasPermissionToTeam(*c.AppContext.Session(), wtcr.TeamID, model.PermissionPrivatePlaybookCreate)
appErr = wtcr.CanBeExecuted(worktemplates.PermissionSet{
CanCreatePublicChannel: canCreatePublicChannel,
CanCreatePrivateChannel: canCreatePrivateChannel,
CanCreatePublicBoard: canCreatePublicBoard,
CanCreatePrivateBoard: canCreatePrivateBoard,
CanCreatePublicPlaybook: canCreatePublicPlaybook,
CanCreatePrivatePlaybook: canCreatePrivatePlaybook,
})
if appErr != nil {
c.Err = appErr
return
}
canInstallPlugin := c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionSysconsoleWritePlugins)
if !*c.App.Config().PluginSettings.Enable || !*c.App.Config().PluginSettings.EnableMarketplace || *c.App.Config().PluginSettings.MarketplaceURL != model.PluginSettingsDefaultMarketplaceURL {
canInstallPlugin = false
}
res, appErr := c.App.ExecuteWorkTemplate(c.AppContext, wtcr, canInstallPlugin)
if appErr != nil {
c.Err = appErr
return
}
err = json.NewEncoder(w).Encode(res)
if err != nil {
c.Err = model.NewAppError("executeWorkTemplate", "api.marshal_error", nil, "", http.StatusInternalServerError).Wrap(err)
return
}
}

Просмотреть файл

@@ -1,67 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"encoding/json"
"net/http"
"github.com/mattermost/mattermost-server/v6/model"
)
func (api *API) InitWorkTemplate() {
api.BaseRoutes.WorkTemplates.Handle("/categories", api.APISessionRequired(needsWorkTemplateFeatureFlag(getWorkTemplateCategories))).Methods("GET")
api.BaseRoutes.WorkTemplates.Handle("/categories/{category}/templates", api.APISessionRequired(needsWorkTemplateFeatureFlag(getWorkTemplates))).Methods("GET")
}
func needsWorkTemplateFeatureFlag(h handlerFunc) handlerFunc {
return func(c *Context, w http.ResponseWriter, r *http.Request) {
if !c.App.Config().FeatureFlags.WorkTemplate {
http.NotFound(w, r)
return
}
h(c, w, r)
}
}
func getWorkTemplateCategories(c *Context, w http.ResponseWriter, r *http.Request) {
t := c.AppContext.GetT()
categories, appErr := c.App.GetWorkTemplateCategories(t)
if appErr != nil {
c.Err = appErr
return
}
b, err := json.Marshal(categories)
if err != nil {
c.Err = model.NewAppError("getWorkTemplateCategories", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
}
func getWorkTemplates(c *Context, w http.ResponseWriter, r *http.Request) {
c.RequireCategory()
if c.Err != nil {
return
}
t := c.AppContext.GetT()
workTemplates, appErr := c.App.GetWorkTemplates(c.Params.Category, c.App.Config().FeatureFlags.ToMap(), t)
if appErr != nil {
c.Err = appErr
return
}
b, err := json.Marshal(workTemplates)
if err != nil {
c.Err = model.NewAppError("getWorkTemplates", "api.marshal_error", nil, err.Error(), http.StatusInternalServerError)
return
}
w.Write(b)
}

Просмотреть файл

@@ -1,110 +0,0 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package api4
import (
"os"
"testing"
"github.com/mattermost/mattermost-server/v6/app/worktemplates"
"github.com/stretchr/testify/require"
)
func TestWorkTemplateCategories(t *testing.T) {
// Setup
cleanup := setupWorktemplateFeatureFlag(t)
defer cleanup()
th := Setup(t).InitBasic()
defer th.TearDown()
assert := require.New(t)
worktemplates.OrderedWorkTemplateCategories = []*worktemplates.WorkTemplateCategory{
{
ID: "test-category",
Name: "Test Category",
},
{
ID: "test-category-2",
Name: "Test Category 2",
},
}
// Act
categories, _, clientErr := th.Client.GetWorktemplateCategories()
// Assert
require.NoError(t, clientErr)
require.Len(t, categories, 2)
assert.Equal("test-category", categories[0].ID)
assert.Equal("test-category-2", categories[1].ID)
}
func TestGetWorkTemplatesByCategory(t *testing.T) {
// Setup
cleanup := setupWorktemplateFeatureFlag(t)
defer cleanup()
th := Setup(t).InitBasic()
defer th.TearDown()
assert := require.New(t)
worktemplates.OrderedWorkTemplateCategories = []*worktemplates.WorkTemplateCategory{
{
ID: "test-category",
Name: "Test Category",
},
{
ID: "test-category-2",
Name: "Test Category 2",
},
}
worktemplates.OrderedWorkTemplates = []*worktemplates.WorkTemplate{
{
ID: "test-template",
Category: "test-category",
UseCase: "Test Template",
},
{
ID: "test-template-2",
Category: "test-category",
UseCase: "Test Template 2",
},
{ // This one should not be returned because of the feature flag
ID: "test-template-3",
Category: "test-category",
UseCase: "Test Template 3",
FeatureFlag: &worktemplates.FeatureFlag{
Name: "random-nonexistant-feature-flag",
Value: "true",
},
},
{ // this one should not be returned because of the category
ID: "test-template-4",
Category: "test-category-2",
UseCase: "Test Template 4",
},
}
// Act
workTemplates, _, clientErr := th.Client.GetWorkTemplatesByCategory("test-category")
// Assert
assert.NoError(clientErr, "error while retrieve worktemplates list")
assert.Len(workTemplates, 2)
assert.Equal("test-template", workTemplates[0].ID)
assert.Equal("test-template-2", workTemplates[1].ID)
}
func setupWorktemplateFeatureFlag(t *testing.T) func() {
t.Helper()
oldFFValue := os.Getenv("MM_FEATUREFLAGS_WORKTEMPLATE")
os.Setenv("MM_FEATUREFLAGS_WORKTEMPLATE", "true")
return func() {
os.Setenv("MM_FEATUREFLAGS_WORKTEMPLATE", oldFFValue)
}
}