Skip to content

Commit 71b6ec6

Browse files
committed
Added delete actions task and steps
Signed-off-by: cassiozareck <[email protected]>
1 parent 002c15d commit 71b6ec6

File tree

2 files changed

+55
-16
lines changed

2 files changed

+55
-16
lines changed

models/actions/run.go

+16-7
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,21 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
305305
return commiter.Commit()
306306
}
307307

308-
func DeleteRunJobs(ctx context.Context, jobs []*ActionRunJob) error {
309-
return db.DeleteBeans(ctx, jobs)
308+
func DeleteTaskSteps(ctx context.Context, steps []*ActionTaskStep) error {
309+
for _, step := range steps {
310+
if err := db.DeleteBeans(ctx, step); err != nil {
311+
return err
312+
}
313+
}
314+
return nil
315+
}
316+
317+
func DeleteTask(ctx context.Context, actionTask *ActionTask) error {
318+
return db.DeleteBeans(ctx, actionTask)
319+
}
320+
321+
func DeleteRunJob(ctx context.Context, job *ActionRunJob) error {
322+
return db.DeleteBeans(ctx, job)
310323
}
311324

312325
func DeleteRun(ctx context.Context, run *ActionRun) error {
@@ -322,11 +335,7 @@ func DeleteRun(ctx context.Context, run *ActionRun) error {
322335
return err
323336
}
324337

325-
if err := updateRepoRunsNumbers(ctx, run.Repo); err != nil {
326-
return err
327-
}
328-
329-
return nil
338+
return updateRepoRunsNumbers(ctx, run.Repo)
330339
}
331340

332341
func GetRunByID(ctx context.Context, id int64) (*ActionRun, error) {

routers/web/repo/actions/view.go

+39-9
Original file line numberDiff line numberDiff line change
@@ -405,28 +405,58 @@ func Cancel(ctx *context_module.Context) {
405405
ctx.JSON(http.StatusOK, struct{}{})
406406
}
407407

408+
// Delete deletes a run and all its jobs.
408409
func Delete(ctx *context_module.Context) {
409-
410410
runIndex := ctx.ParamsInt64("run")
411411

412-
_, jobs := getRunJobs(ctx, runIndex, -1)
413-
if ctx.Written() {
414-
return
415-
}
416-
417412
run, err := actions_model.GetRunByID(ctx, runIndex)
418413
if err != nil {
419414
ctx.Error(http.StatusNotFound, err.Error())
420415
return
421416
}
422417

418+
_, jobs := getRunJobs(ctx, runIndex, -1)
419+
if ctx.Written() {
420+
return
421+
}
422+
423+
// Initializes a transaction where it will:
424+
// 1. Get all the tasks associate with each job as well as the task steps
425+
// 2. Delete all the task steps, tasks, jobs and run itself.
423426
if err := db.WithTx(ctx, func(ctx context.Context) error {
427+
for _, job := range jobs {
424428

425-
if err := actions_model.DeleteRunJobs(ctx, jobs); err != nil {
426-
return err
429+
if job.TaskID == 0 {
430+
return fmt.Errorf("job not associate with any task")
431+
}
432+
if job.Status == actions_model.StatusRunning {
433+
return fmt.Errorf("job is running, can't delete")
434+
}
435+
436+
task, err := actions_model.GetTaskByID(ctx, job.TaskID)
437+
if err != nil {
438+
return fmt.Errorf("error while getting task: %v", err)
439+
}
440+
441+
taskSteps, err := actions_model.GetTaskStepsByTaskID(ctx, task.ID)
442+
if err != nil {
443+
return fmt.Errorf("error while getting task steps: %v", err)
444+
}
445+
446+
if err := actions_model.DeleteTaskSteps(ctx, taskSteps); err != nil {
447+
return fmt.Errorf("error while deleting task steps: %v", err)
448+
}
449+
450+
if err := actions_model.DeleteTask(ctx, task); err != nil {
451+
return fmt.Errorf("error while deleting task: %v", err)
452+
}
453+
454+
if err := actions_model.DeleteRunJob(ctx, job); err != nil {
455+
return fmt.Errorf("error while deleting run job: %v", err)
456+
}
427457
}
428458
if err := actions_model.DeleteRun(ctx, run); err != nil {
429-
return err
459+
return fmt.Errorf("error while deleting run: %v", err)
430460
}
431461
return nil
432462
}); err != nil {

0 commit comments

Comments
 (0)