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

Delete run feature #26275

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,39 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
return commiter.Commit()
}

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 {
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
}

return updateRepoRunsNumbers(ctx, run.Repo)
}

func GetRunByID(ctx context.Context, id int64) (*ActionRun, error) {
var run ActionRun
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&run)
Expand Down
60 changes: 60 additions & 0 deletions routers/web/repo/actions/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,66 @@ 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")

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 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 fmt.Errorf("error while deleting run: %v", err)
}
return nil
}); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error())
return
}
}

func Approve(ctx *context_module.Context) {
runIndex := ctx.ParamsInt64("run")

Expand Down
1 change: 1 addition & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down