Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: HTTP endpoint to patch all tasks in an issue (#1944)

p0nyc4cd7da6a6d6
diff --git a/server/acl_casbin_policy_dba.csv b/server/acl_casbin_policy_dba.csv
index 9acc70e238..24b8298276 100644
--- a/server/acl_casbin_policy_dba.csv
+++ b/server/acl_casbin_policy_dba.csv
@@ -71,6 +71,7 @@ p, DBA, /bookmark, POST
p, DBA, /bookmark/user/{userID}, GET_SELF
p, DBA, /bookmark/{id}, DELETE_SELF
p, DBA, /pipeline/{pipelineID}/stage/{stageID}/status, PATCH
+p, DBA, /pipeline/{pipelineID}/task/all, PATCH
p, DBA, /pipeline/{pipelineID}/task/{taskID}, PATCH
p, DBA, /pipeline/{pipelineID}/task/{taskID}/status, PATCH
p, DBA, /pipeline/{pipelineID}/task/{taskID}/check, POST
diff --git a/server/acl_casbin_policy_developer.csv b/server/acl_casbin_policy_developer.csv
index 34c9f928de..320c35f0d1 100644
--- a/server/acl_casbin_policy_developer.csv
+++ b/server/acl_casbin_policy_developer.csv
@@ -65,6 +65,7 @@ p, DEVELOPER, /bookmark, POST
p, DEVELOPER, /bookmark/user/{userID}, GET_SELF
p, DEVELOPER, /bookmark/{id}, DELETE_SELF
p, DEVELOPER, /pipeline/{pipelineID}/stage/{stageID}/status, PATCH
+p, DEVELOPER, /pipeline/{pipelineID}/task/all, PATCH
p, DEVELOPER, /pipeline/{pipelineID}/task/{taskID}, PATCH
p, DEVELOPER, /pipeline/{pipelineID}/task/{taskID}/status, PATCH
p, DEVELOPER, /pipeline/{pipelineID}/task/{taskID}/check, POST
diff --git a/server/acl_casbin_policy_owner.csv b/server/acl_casbin_policy_owner.csv
index bdedf0a666..a04a77d381 100644
--- a/server/acl_casbin_policy_owner.csv
+++ b/server/acl_casbin_policy_owner.csv
@@ -75,6 +75,7 @@ p, OWNER, /bookmark, POST
p, OWNER, /bookmark/user/{userID}, GET_SELF
p, OWNER, /bookmark/{id}, DELETE_SELF
p, OWNER, /pipeline/{pipelineID}/stage/{stageID}/status, PATCH
+p, OWNER, /pipeline/{pipelineID}/task/all, PATCH
p, OWNER, /pipeline/{pipelineID}/task/{taskID}, PATCH
p, OWNER, /pipeline/{pipelineID}/task/{taskID}/status, PATCH
p, OWNER, /pipeline/{pipelineID}/task/{taskID}/check, POST
diff --git a/server/task.go b/server/task.go
index 1356cd2052..a770d76b25 100644
--- a/server/task.go
+++ b/server/task.go
@@ -57,6 +57,62 @@ func (s *Server) canUpdateTaskStatement(ctx context.Context, task *api.Task) *ec
}
func (s *Server) registerTaskRoutes(g *echo.Group) {
+ g.PATCH("/pipeline/:pipelineID/task/all", func(c echo.Context) error {
+ ctx := c.Request().Context()
+ pipelineID, err := strconv.Atoi(c.Param("pipelineID"))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Pipeline ID is not a number: %s", c.Param("pipelineID"))).SetInternal(err)
+ }
+
+ taskPatch := &api.TaskPatch{
+ UpdaterID: c.Get(getPrincipalIDContextKey()).(int),
+ }
+ if err := jsonapi.UnmarshalPayload(c.Request().Body, taskPatch); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, "Malformed update task request").SetInternal(err)
+ }
+
+ if taskPatch.EarliestAllowedTs != nil && !s.feature(api.FeatureTaskScheduleTime) {
+ return echo.NewHTTPError(http.StatusForbidden, api.FeatureTaskScheduleTime.AccessErrorMessage())
+ }
+
+ issue, err := s.store.GetIssueByPipelineID(ctx, pipelineID)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue with pipeline ID: %d", pipelineID)).SetInternal(err)
+ }
+ if issue == nil {
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Issue not found with pipelineID: %d", pipelineID))
+ }
+
+ if taskPatch.Statement != nil {
+ // check if all tasks can update statement
+ for _, stage := range issue.Pipeline.StageList {
+ for _, task := range stage.TaskList {
+ if httpErr := s.canUpdateTaskStatement(ctx, task); httpErr != nil {
+ return httpErr
+ }
+ }
+ }
+ }
+
+ var taskPatchedList []*api.Task
+ for _, stage := range issue.Pipeline.StageList {
+ for _, task := range stage.TaskList {
+ taskPatch := *taskPatch
+ taskPatch.ID = task.ID
+ taskPatched, httpErr := s.patchTask(ctx, task, &taskPatch, issue)
+ if httpErr != nil {
+ return httpErr
+ }
+ taskPatchedList = append(taskPatchedList, taskPatched)
+ }
+ }
+ c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
+ if err := jsonapi.MarshalPayload(c.Response().Writer, taskPatchedList); err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to marshal update pipeline %q tasks response", issue.Pipeline.Name)).SetInternal(err)
+ }
+ return nil
+ })
+
g.PATCH("/pipeline/:pipelineID/task/:taskID", func(c echo.Context) error {
ctx := c.Request().Context()
taskID, err := strconv.Atoi(c.Param("taskID"))
@@ -86,17 +142,17 @@ func (s *Server) registerTaskRoutes(g *echo.Group) {
issue, err := s.store.GetIssueByPipelineID(ctx, task.PipelineID)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue with pipeline ID %v", task.PipelineID)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch issue with pipeline ID: %d", task.PipelineID)).SetInternal(err)
}
if issue == nil {
- return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Issue not found, pipelineID: %d", task.PipelineID))
+ return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Issue not found with pipelineID: %d", task.PipelineID))
}
if taskPatch.Statement != nil {
// Tenant mode project don't allow updating SQL statement for a single task.
project, err := s.store.GetProjectByID(ctx, issue.ProjectID)
if err != nil {
- return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project with ID %d", issue.ProjectID)).SetInternal(err)
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch project with ID: %d", issue.ProjectID)).SetInternal(err)
}
if project.TenantMode == api.TenantModeTenant && task.Type == api.TaskDatabaseSchemaUpdate {
return echo.NewHTTPError(http.StatusBadRequest, "cannot update SQL statement of a single task for projects in tenant mode")