From 2d0d4a1cdb55b03b802cdf243a873a16b6da509f Mon Sep 17 00:00:00 2001 From: cassiozareck Date: Tue, 1 Aug 2023 11:30:07 -0300 Subject: [PATCH 1/4] Added DeleteRunJobs and DeleteRun as well a Delete endpoint --- models/actions/run.go | 29 +++++++++++++++++++++++++++++ routers/web/repo/actions/view.go | 30 ++++++++++++++++++++++++++++++ routers/web/web.go | 1 + 3 files changed, 60 insertions(+) diff --git a/models/actions/run.go b/models/actions/run.go index ab6e319b1cc85..04ea1c9d22ccb 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -305,6 +305,35 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork return commiter.Commit() } +func DeleteRunJobs(ctx context.Context, jobs []*ActionRunJob) error { + for _, job := range jobs { + if err := db.DeleteBeans(ctx, job); err != nil { + return err + } + } + return nil +} + +func DeleteRun(ctx context.Context, run *ActionRun) error { + if run.Repo == nil { + repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID) + if err != nil { + return err + } + run.Repo = repo + } + + if err := db.DeleteBeans(ctx, run); err != nil { + return err + } + + if err := updateRepoRunsNumbers(ctx, run.Repo); err != nil { + return err + } + + return nil +} + func GetRunByID(ctx context.Context, id int64) (*ActionRun, error) { var run ActionRun has, err := db.GetEngine(ctx).Where("id=?", id).Get(&run) diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index abb1f6b66b979..5702078966633 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -405,6 +405,36 @@ func Cancel(ctx *context_module.Context) { ctx.JSON(http.StatusOK, struct{}{}) } +func Delete(ctx *context_module.Context) { + + runIndex := ctx.ParamsInt64("run") + + _, jobs := getRunJobs(ctx, runIndex, -1) + if ctx.Written() { + return + } + + run, err := actions_model.GetRunByID(ctx, runIndex) + if err != nil { + ctx.Error(http.StatusNotFound, err.Error()) + return + } + + if err := db.WithTx(ctx, func(ctx context.Context) error { + + if err := actions_model.DeleteRunJobs(ctx, jobs); err != nil { + return err + } + if err := actions_model.DeleteRun(ctx, run); err != nil { + return err + } + return nil + }); err != nil { + ctx.Error(http.StatusInternalServerError, err.Error()) + return + } +} + func Approve(ctx *context_module.Context) { runIndex := ctx.ParamsInt64("run") diff --git a/routers/web/web.go b/routers/web/web.go index ca75bd5967271..d571795e718f0 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1212,6 +1212,7 @@ func registerRoutes(m *web.Route) { m.Post("/artifacts", actions.ArtifactsView) m.Get("/artifacts/{artifact_name}", actions.ArtifactsDownloadView) m.Post("/rerun", reqRepoActionsWriter, actions.RerunAll) + m.Post("/delete", reqRepoActionsWriter, actions.Delete) }) }, reqRepoActionsReader, actions.MustEnableActions) From 0f4514794c6a9c545793d2cbf0244b8b22b2f0ee Mon Sep 17 00:00:00 2001 From: cassiozareck <121526696+cassiozareck@users.noreply.github.com> Date: Thu, 3 Aug 2023 09:33:34 -0300 Subject: [PATCH 2/4] Update models/actions/run.go unwrap array Co-authored-by: KN4CK3R --- models/actions/run.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 04ea1c9d22ccb..59c4b48067122 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -306,12 +306,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork } func DeleteRunJobs(ctx context.Context, jobs []*ActionRunJob) error { - for _, job := range jobs { - if err := db.DeleteBeans(ctx, job); err != nil { - return err - } - } - return nil + return db.DeleteBeans(ctx, jobs...) } func DeleteRun(ctx context.Context, run *ActionRun) error { From 002c15d6e65462b26602a2033066b8023b5e30d8 Mon Sep 17 00:00:00 2001 From: cassiozareck Date: Thu, 3 Aug 2023 09:53:28 -0300 Subject: [PATCH 3/4] Passing jobs directly --- models/actions/run.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/actions/run.go b/models/actions/run.go index 59c4b48067122..64531fb2e9d9c 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -306,7 +306,7 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork } func DeleteRunJobs(ctx context.Context, jobs []*ActionRunJob) error { - return db.DeleteBeans(ctx, jobs...) + return db.DeleteBeans(ctx, jobs) } func DeleteRun(ctx context.Context, run *ActionRun) error { From 71b6ec67602e2252c75bb09db807add055e4c69c Mon Sep 17 00:00:00 2001 From: cassiozareck Date: Sat, 5 Aug 2023 16:32:25 -0300 Subject: [PATCH 4/4] Added delete actions task and steps Signed-off-by: cassiozareck --- models/actions/run.go | 23 ++++++++++----- routers/web/repo/actions/view.go | 48 ++++++++++++++++++++++++++------ 2 files changed, 55 insertions(+), 16 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 64531fb2e9d9c..8d4cef022899e 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -305,8 +305,21 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork return commiter.Commit() } -func DeleteRunJobs(ctx context.Context, jobs []*ActionRunJob) error { - return db.DeleteBeans(ctx, jobs) +func DeleteTaskSteps(ctx context.Context, steps []*ActionTaskStep) error { + for _, step := range steps { + if err := db.DeleteBeans(ctx, step); err != nil { + return err + } + } + return nil +} + +func DeleteTask(ctx context.Context, actionTask *ActionTask) error { + return db.DeleteBeans(ctx, actionTask) +} + +func DeleteRunJob(ctx context.Context, job *ActionRunJob) error { + return db.DeleteBeans(ctx, job) } func DeleteRun(ctx context.Context, run *ActionRun) error { @@ -322,11 +335,7 @@ func DeleteRun(ctx context.Context, run *ActionRun) error { return err } - if err := updateRepoRunsNumbers(ctx, run.Repo); err != nil { - return err - } - - return nil + return updateRepoRunsNumbers(ctx, run.Repo) } func GetRunByID(ctx context.Context, id int64) (*ActionRun, error) { diff --git a/routers/web/repo/actions/view.go b/routers/web/repo/actions/view.go index 5702078966633..cc9d64cf5fcb0 100644 --- a/routers/web/repo/actions/view.go +++ b/routers/web/repo/actions/view.go @@ -405,28 +405,58 @@ func Cancel(ctx *context_module.Context) { ctx.JSON(http.StatusOK, struct{}{}) } +// Delete deletes a run and all its jobs. func Delete(ctx *context_module.Context) { - runIndex := ctx.ParamsInt64("run") - _, jobs := getRunJobs(ctx, runIndex, -1) - if ctx.Written() { - return - } - run, err := actions_model.GetRunByID(ctx, runIndex) if err != nil { ctx.Error(http.StatusNotFound, err.Error()) return } + _, jobs := getRunJobs(ctx, runIndex, -1) + if ctx.Written() { + return + } + + // Initializes a transaction where it will: + // 1. Get all the tasks associate with each job as well as the task steps + // 2. Delete all the task steps, tasks, jobs and run itself. if err := db.WithTx(ctx, func(ctx context.Context) error { + for _, job := range jobs { - if err := actions_model.DeleteRunJobs(ctx, jobs); err != nil { - return err + if job.TaskID == 0 { + return fmt.Errorf("job not associate with any task") + } + if job.Status == actions_model.StatusRunning { + return fmt.Errorf("job is running, can't delete") + } + + task, err := actions_model.GetTaskByID(ctx, job.TaskID) + if err != nil { + return fmt.Errorf("error while getting task: %v", err) + } + + taskSteps, err := actions_model.GetTaskStepsByTaskID(ctx, task.ID) + if err != nil { + return fmt.Errorf("error while getting task steps: %v", err) + } + + if err := actions_model.DeleteTaskSteps(ctx, taskSteps); err != nil { + return fmt.Errorf("error while deleting task steps: %v", err) + } + + if err := actions_model.DeleteTask(ctx, task); err != nil { + return fmt.Errorf("error while deleting task: %v", err) + } + + if err := actions_model.DeleteRunJob(ctx, job); err != nil { + return fmt.Errorf("error while deleting run job: %v", err) + } } if err := actions_model.DeleteRun(ctx, run); err != nil { - return err + return fmt.Errorf("error while deleting run: %v", err) } return nil }); err != nil {