Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feishu 5 #1793

Merged
merged 16 commits into from
Sep 7, 2023
4 changes: 3 additions & 1 deletion spelling_dict.txt
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,6 @@ Tjwvz046g
slowlogs
tjwvz
ejwvz
wiru
wiru
larkapproval
ccer
3 changes: 3 additions & 0 deletions sqle/api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi
v1Router.GET("/configurations/feishu", v1.GetFeishuConfigurationV1, AdminUserAllowed())
v1Router.PATCH("/configurations/feishu", v1.UpdateFeishuConfigurationV1, AdminUserAllowed())
v1Router.POST("/configurations/feishu/test", v1.TestFeishuConfigV1, AdminUserAllowed())
v1Router.PATCH("/configurations/feishu_audit", v1.UpdateFeishuAuditConfigurationV1, AdminUserAllowed())
v1Router.GET("/configurations/feishu_audit", v1.GetFeishuAuditConfigurationV1, AdminUserAllowed())
v1Router.POST("/configurations/feishu_audit/test", v1.TestFeishuAuditConfigV1, AdminUserAllowed())
v1Router.GET("/configurations/system_variables", v1.GetSystemVariables, AdminUserAllowed())
v1Router.PATCH("/configurations/system_variables", v1.UpdateSystemVariables, AdminUserAllowed())
v1Router.GET("/configurations/license", v1.GetLicense, AdminUserAllowed())
Expand Down
9 changes: 5 additions & 4 deletions sqle/api/controller/v1/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/actiontech/sqle/sqle/pkg/im/feishu"

"github.com/labstack/echo/v4"
larkContact "github.com/larksuite/oapi-sdk-go/v3/service/contact/v3"
)

type UpdateSMTPConfigurationReqV1 struct {
Expand Down Expand Up @@ -564,7 +565,7 @@ func TestFeishuConfigV1(c echo.Context) error {
}

client := feishu.NewFeishuClient(feishuCfg.AppKey, feishuCfg.AppSecret)
feishuUsers, err := client.GetUsersByEmailOrMobileWithLimitation(email, phone)
feishuUsers, err := client.GetUsersByEmailOrMobileWithLimitation(email, phone, larkContact.UserIdTypeGetUserUserId)
if err != nil {
return c.JSON(http.StatusOK, &TestFeishuConfigResV1{
BaseRes: controller.NewBaseReq(nil),
Expand Down Expand Up @@ -1374,7 +1375,7 @@ type GetFeishuAuditConfigurationResV1 struct {
// @Success 200 {object} v1.GetFeishuAuditConfigurationResV1
// @router /v1/configurations/feishu_audit [get]
func GetFeishuAuditConfigurationV1(c echo.Context) error {
return nil
return getFeishuAuditConfigurationV1(c)
}

// UpdateFeishuAuditConfigurationV1
Expand All @@ -1388,7 +1389,7 @@ func GetFeishuAuditConfigurationV1(c echo.Context) error {
// @Success 200 {object} controller.BaseRes
// @router /v1/configurations/feishu_audit [patch]
func UpdateFeishuAuditConfigurationV1(c echo.Context) error {
return nil
return updateFeishuAuditConfigurationV1(c)
}

// TestFeishuAuditConfigV1
Expand All @@ -1402,5 +1403,5 @@ func UpdateFeishuAuditConfigurationV1(c echo.Context) error {
// @Success 200 {object} v1.TestFeishuConfigResV1
// @router /v1/configurations/feishu_audit/test [post]
func TestFeishuAuditConfigV1(c echo.Context) error {
return nil
return testFeishuAuditConfigV1(c)
}
13 changes: 13 additions & 0 deletions sqle/api/controller/v1/configuration_ce.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
var (
errCommunityEditionNotSupportCostumeLogo = errors.New(errors.EnterpriseEditionFeatures, e.New("costume logo is enterprise version feature"))
errCommunityEditionNotSupportUpdatePersonaliseConfig = errors.New(errors.EnterpriseEditionFeatures, e.New("update personalise config is enterprise version feature"))
errCommunityEditionNotSupportFeishuAudit = errors.New(errors.EnterpriseEditionFeatures, e.New("feishu audit is enterprise version feature"))
)

const (
Expand Down Expand Up @@ -50,3 +51,15 @@ func getSQLEInfo(c echo.Context) error {
},
})
}

func updateFeishuAuditConfigurationV1(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionNotSupportFeishuAudit)
}

func getFeishuAuditConfigurationV1(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionNotSupportFeishuAudit)
}

func testFeishuAuditConfigV1(c echo.Context) error {
return controller.JSONBaseErrorReq(c, errCommunityEditionNotSupportFeishuAudit)
}
75 changes: 0 additions & 75 deletions sqle/api/controller/v1/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"net/http"
"strconv"
"strings"
"time"

"github.com/actiontech/sqle/sqle/api/controller"
Expand Down Expand Up @@ -317,24 +316,6 @@ type WorkflowStepResV1 struct {
Reason string `json:"reason,omitempty"`
}

func CheckUserCanOperateStep(user *model.User, workflow *model.Workflow, stepId int) error {
if workflow.Record.Status != model.WorkflowStatusWaitForAudit && workflow.Record.Status != model.WorkflowStatusWaitForExecution {
return fmt.Errorf("workflow status is %s, not allow operate it", workflow.Record.Status)
}
currentStep := workflow.CurrentStep()
if currentStep == nil {
return fmt.Errorf("workflow current step not found")
}
if uint(stepId) != workflow.CurrentStep().ID {
return fmt.Errorf("workflow current step is not %d", stepId)
}

if !workflow.IsOperationUser(user) {
return fmt.Errorf("you are not allow to operate the workflow")
}
return nil
}

// @Deprecated
// @Summary 审批通过
// @Description approve workflow
Expand Down Expand Up @@ -493,34 +474,6 @@ func IsTaskCanExecute(s *model.Storage, taskId string) (bool, error) {
return true, nil
}

func GetNeedExecTaskIds(s *model.Storage, workflow *model.Workflow, user *model.User) (taskIds map[uint] /*task id*/ uint /*user id*/, err error) {
instances, err := s.GetInstancesByWorkflowID(workflow.ID)
if err != nil {
return nil, err
}
// 有不在运维时间内的instances报错
var cannotExecuteInstanceNames []string
for _, inst := range instances {
if len(inst.MaintenancePeriod) != 0 && !inst.MaintenancePeriod.IsWithinScope(time.Now()) {
cannotExecuteInstanceNames = append(cannotExecuteInstanceNames, inst.Name)
}
}
if len(cannotExecuteInstanceNames) > 0 {
return nil, errors.New(errors.TaskActionInvalid,
fmt.Errorf("please go online during instance operation and maintenance time. these instances are not in maintenance time[%v]", strings.Join(cannotExecuteInstanceNames, ",")))
}

// 定时的instances和已上线的跳过
needExecTaskIds := make(map[uint]uint)
for _, instRecord := range workflow.Record.InstanceRecords {
if instRecord.ScheduledAt != nil || instRecord.IsSQLExecuted {
continue
}
needExecTaskIds[instRecord.TaskId] = user.ID
}
return needExecTaskIds, nil
}

func PrepareForTaskExecution(c echo.Context, projectName string, workflow *model.Workflow, user *model.User, TaskId int) error {
if workflow.Record.Status != model.WorkflowStatusWaitForExecution {
return errors.New(errors.DataInvalid, e.New("workflow need to be approved first"))
Expand All @@ -546,29 +499,6 @@ func PrepareForTaskExecution(c echo.Context, projectName string, workflow *model
return e.New("you are not allow to execute the task")
}

func PrepareForWorkflowExecution(c echo.Context, projectName string, workflow *model.Workflow, user *model.User) error {
err := CheckCurrentUserCanOperateWorkflow(c, &model.Project{Name: projectName}, workflow, []uint{})
if err != nil {
return err
}

currentStep := workflow.CurrentStep()
if currentStep == nil {
return errors.New(errors.DataInvalid, fmt.Errorf("workflow current step not found"))
}

if workflow.Record.Status != model.WorkflowStatusWaitForExecution {
return errors.New(errors.DataInvalid,
fmt.Errorf("workflow need to be approved first"))
}

err = CheckUserCanOperateStep(user, workflow, int(currentStep.ID))
if err != nil {
return errors.New(errors.DataInvalid, err)
}
return nil
}

type GetWorkflowTasksResV1 struct {
controller.BaseRes
Data []*GetWorkflowTasksItemV1 `json:"data"`
Expand Down Expand Up @@ -686,7 +616,6 @@ func CheckWorkflowCanCommit(template *model.WorkflowTemplate, tasks []*model.Tas
type GetWorkflowsReqV1 struct {
FilterSubject string `json:"filter_subject" query:"filter_subject"`
FilterWorkflowID string `json:"filter_workflow_id" query:"filter_workflow_id"`
FuzzySearchWorkflowDesc string `json:"fuzzy_search_workflow_desc" query:"fuzzy_search_workflow_desc"`
FilterCreateTimeFrom string `json:"filter_create_time_from" query:"filter_create_time_from"`
FilterCreateTimeTo string `json:"filter_create_time_to" query:"filter_create_time_to"`
FilterCreateUserName string `json:"filter_create_user_name" query:"filter_create_user_name"`
Expand Down Expand Up @@ -805,7 +734,6 @@ func GetGlobalWorkflowsV1(c echo.Context) error {
// @Security ApiKeyAuth
// @Param filter_subject query string false "filter subject"
// @Param filter_workflow_id query string false "filter by workflow_id"
// @Param fuzzy_search_workflow_desc query string false "fuzzy search by workflow description"
// @Param filter_create_time_from query string false "filter create time from"
// @Param filter_create_time_to query string false "filter create time to"
// @Param filter_task_execute_start_time_from query string false "filter_task_execute_start_time_from"
Expand Down Expand Up @@ -853,7 +781,6 @@ func GetWorkflowsV1(c echo.Context) error {

data := map[string]interface{}{
"filter_workflow_id": req.FilterWorkflowID,
"fuzzy_search_workflow_desc": req.FuzzySearchWorkflowDesc,
"filter_subject": req.FilterSubject,
"filter_create_time_from": req.FilterCreateTimeFrom,
"filter_create_time_to": req.FilterCreateTimeTo,
Expand Down Expand Up @@ -1001,7 +928,6 @@ func GetWorkflowV1(c echo.Context) error {

type ExportWorkflowReqV1 struct {
FilterSubject string `json:"filter_subject" query:"filter_subject"`
FuzzySearchWorkflowDesc string `json:"fuzzy_search_workflow_desc" query:"fuzzy_search_workflow_desc"`
FilterCreateTimeFrom string `json:"filter_create_time_from" query:"filter_create_time_from"`
FilterCreateTimeTo string `json:"filter_create_time_to" query:"filter_create_time_to"`
FilterCreateUserName string `json:"filter_create_user_name" query:"filter_create_user_name"`
Expand All @@ -1019,7 +945,6 @@ type ExportWorkflowReqV1 struct {
// @Tags workflow
// @Security ApiKeyAuth
// @Param filter_subject query string false "filter subject"
// @Param fuzzy_search_workflow_desc query string false "fuzzy search by workflow description"
// @Param filter_create_time_from query string false "filter create time from"
// @Param filter_create_time_to query string false "filter create time to"
// @Param filter_task_execute_start_time_from query string false "filter_task_execute_start_time_from"
Expand Down
45 changes: 18 additions & 27 deletions sqle/api/controller/v2/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func ApproveWorkflowV2(c echo.Context) error {

nextStep := workflow.NextStep()

err = v1.CheckUserCanOperateStep(user, workflow, stepId)
err = server.CheckUserCanOperateStep(user, workflow, stepId)
if err != nil {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err))
}
Expand All @@ -108,9 +108,9 @@ func ApproveWorkflowV2(c echo.Context) error {
return controller.JSONBaseErrorReq(c, err)
}

go im.UpdateApprove(workflow.ID, user.Phone, model.ApproveStatusAgree, "")
go im.UpdateApprove(workflow.ID, user, model.ApproveStatusAgree, "")

if nextStep.Template.Typ != model.WorkflowStepTypeSQLExecute {
if nextStep != nil {
go im.CreateApprove(strconv.Itoa(int(workflow.ID)))
}

Expand Down Expand Up @@ -184,7 +184,7 @@ func RejectWorkflowV2(c echo.Context) error {
return controller.JSONBaseErrorReq(c, v1.ErrWorkflowNoAccess)
}

err = v1.CheckUserCanOperateStep(user, workflow, stepId)
err = server.CheckUserCanOperateStep(user, workflow, stepId)
if err != nil {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err))
}
Expand All @@ -202,7 +202,7 @@ func RejectWorkflowV2(c echo.Context) error {
return controller.JSONBaseErrorReq(c, err)
}

go im.UpdateApprove(workflow.ID, user.Phone, model.ApproveStatusRefuse, req.Reason)
go im.UpdateApprove(workflow.ID, user, model.ApproveStatusRefuse, req.Reason)

return c.JSON(http.StatusOK, controller.NewBaseReq(nil))
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func CancelWorkflowV2(c echo.Context) error {
fmt.Errorf("you are not allow to operate the workflow")))
}

go im.CancelApprove(workflow.ID)
go im.BatchCancelApprove([]uint{workflow.ID}, user)

workflow.Record.Status = model.WorkflowStatusCancel
workflow.Record.CurrentWorkflowStepId = 0
Expand Down Expand Up @@ -313,8 +313,12 @@ func BatchCancelWorkflowsV2(c echo.Context) error {
}

projectName := c.Param("project_name")
userName := controller.GetUserName(c)
if err := v1.CheckIsProjectManager(userName, projectName); err != nil {
user, err := controller.GetCurrentUser(c)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

if err := v1.CheckIsProjectManager(user.Name, projectName); err != nil {
return controller.JSONBaseErrorReq(c, err)
}

Expand All @@ -331,7 +335,7 @@ func BatchCancelWorkflowsV2(c echo.Context) error {
workflow.Record.CurrentWorkflowStepId = 0
}

go im.BatchCancelApprove(workflowIds)
go im.BatchCancelApprove(workflowIds, user)

if err := model.GetStorage().BatchUpdateWorkflowStatus(workflows); err != nil {
return controller.JSONBaseErrorReq(c, err)
Expand Down Expand Up @@ -985,7 +989,7 @@ func UpdateWorkflowScheduleV2(c echo.Context) error {
fmt.Errorf("workflow need to be approved first")))
}

err = v1.CheckUserCanOperateStep(user, workflow, int(currentStep.ID))
err = server.CheckUserCanOperateStep(user, workflow, int(currentStep.ID))
if err != nil {
return controller.JSONBaseErrorReq(c, errors.New(errors.DataInvalid, err))
}
Expand Down Expand Up @@ -1052,33 +1056,20 @@ func ExecuteTasksOnWorkflowV2(c echo.Context) error {
return controller.JSONBaseErrorReq(c, v1.ErrWorkflowNoAccess)
}

workflowId = fmt.Sprintf("%v", workflow.ID)

workflow, exist, err = s.GetWorkflowDetailById(workflowId)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
if !exist {
return controller.JSONBaseErrorReq(c, v1.ErrWorkflowNoAccess)
}
user, err := controller.GetCurrentUser(c)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
if err := v1.PrepareForWorkflowExecution(c, projectName, workflow, user); err != nil {
return err
}

needExecTaskIds, err := v1.GetNeedExecTaskIds(s, workflow, user)
if err != nil {
return err
}
workflowId = fmt.Sprintf("%v", workflow.ID)

err = server.ExecuteWorkflow(workflow, needExecTaskIds)
err = server.ExecuteTasksProcess(workflowId, projectName, user)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}

im.UpdateApprove(workflow.ID, user, model.ApproveStatusAgree, "")

return c.JSON(http.StatusOK, controller.NewBaseReq(nil))
}

Expand Down
Loading