Skip to content

Commit a633f44

Browse files
committed
Refactor
1 parent 83f1bba commit a633f44

File tree

2 files changed

+72
-67
lines changed

2 files changed

+72
-67
lines changed

models/actions/run.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
package actions
55

66
import (
7+
actions_module "code.gitea.io/gitea/modules/actions"
8+
container_module "code.gitea.io/gitea/modules/container"
9+
log_module "code.gitea.io/gitea/modules/log"
10+
storage_module "code.gitea.io/gitea/modules/storage"
711
"context"
812
"errors"
913
"fmt"
@@ -439,4 +443,71 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
439443
return nil
440444
}
441445

446+
// TODO: When deleting a run, it should at lease delete artifacts, tasks logs, database record.
447+
func DeleteRun(ctx context.Context, repoID int64, run *ActionRun, jobs []*ActionRunJob) error {
448+
jobIDs := container_module.FilterSlice(jobs, func(j *ActionRunJob) (int64, bool) {
449+
return j.ID, j.ID != 0
450+
})
451+
452+
tasks := make(TaskList, 0)
453+
if err := db.GetEngine(ctx).Where("repo_id = ?", repoID).In("job_id", jobIDs).Find(&tasks); err != nil {
454+
return err
455+
}
456+
457+
artifacts, err := db.Find[ActionArtifact](ctx, FindArtifactsOptions{
458+
RepoID: repoID,
459+
RunID: run.ID,
460+
})
461+
if err != nil {
462+
return err
463+
}
464+
465+
var recordsToDelete []any
466+
467+
for _, task := range tasks {
468+
recordsToDelete = append(recordsToDelete, &ActionTask{
469+
RepoID: repoID,
470+
ID: task.ID,
471+
})
472+
recordsToDelete = append(recordsToDelete, &ActionTaskStep{
473+
RepoID: repoID,
474+
TaskID: task.ID,
475+
})
476+
}
477+
recordsToDelete = append(recordsToDelete, &ActionRunJob{
478+
RepoID: repoID,
479+
RunID: run.ID,
480+
})
481+
recordsToDelete = append(recordsToDelete, &ActionRun{
482+
RepoID: repoID,
483+
ID: run.ID,
484+
})
485+
recordsToDelete = append(recordsToDelete, &ActionArtifact{
486+
RepoID: repoID,
487+
RunID: run.ID,
488+
})
489+
490+
if err := db.WithTx(ctx, func(ctx context.Context) error {
491+
return db.DeleteBeans(ctx, recordsToDelete...)
492+
}); err != nil {
493+
return err
494+
}
495+
496+
// Delete files on storage
497+
for _, tas := range tasks {
498+
err := actions_module.RemoveLogs(ctx, tas.LogInStorage, tas.LogFilename)
499+
if err != nil {
500+
log_module.Error("remove log file %q: %v", tas.LogFilename, err)
501+
}
502+
}
503+
for _, art := range artifacts {
504+
if err := storage_module.ActionsArtifacts.Delete(art.StoragePath); err != nil {
505+
log_module.Error("remove artifact file %q: %v", art.StoragePath, err)
506+
}
507+
}
508+
509+
// TODO: Delete commit status? Looks like it has no direct reference to a run/task/job. Not quite feasible without modifying db models (Dangerous).
510+
return nil
511+
}
512+
442513
type ActionRunIndex db.ResourceIndex

routers/web/repo/actions/view.go

Lines changed: 1 addition & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,6 @@ func Approve(ctx *context_module.Context) {
581581
ctx.JSON(http.StatusOK, struct{}{})
582582
}
583583

584-
// TODO: When deleting a run, it should at lease delete artifacts, tasks logs, database record.
585584
func Delete(ctx *context_module.Context) {
586585
runIndex := getRunIndex(ctx)
587586

@@ -599,76 +598,11 @@ func Delete(ctx *context_module.Context) {
599598

600599
repoID := ctx.Repo.Repository.ID
601600

602-
tasks := []*actions_model.ActionTask{}
603-
604-
for _, job := range jobs {
605-
tasks0, err := db.Find[actions_model.ActionTask](ctx, actions_model.FindTaskOptions{
606-
RepoID: repoID,
607-
JobID: job.ID,
608-
})
609-
if err != nil {
610-
ctx.HTTPError(http.StatusInternalServerError, err.Error())
611-
return
612-
}
613-
tasks = append(tasks, tasks0...)
614-
}
615-
616-
artifacts, err := db.Find[actions_model.ActionArtifact](ctx, actions_model.FindArtifactsOptions{
617-
RepoID: repoID,
618-
RunID: run.ID,
619-
})
620-
if err != nil {
601+
if err := actions_model.DeleteRun(ctx, repoID, run, jobs); err != nil {
621602
ctx.HTTPError(http.StatusInternalServerError, err.Error())
622603
return
623604
}
624605

625-
recordsToDelete := []any{}
626-
627-
for _, task := range tasks {
628-
recordsToDelete = append(recordsToDelete, &actions_model.ActionTask{
629-
RepoID: repoID,
630-
ID: task.ID,
631-
})
632-
recordsToDelete = append(recordsToDelete, &actions_model.ActionTaskStep{
633-
RepoID: repoID,
634-
TaskID: task.ID,
635-
})
636-
}
637-
recordsToDelete = append(recordsToDelete, &actions_model.ActionRunJob{
638-
RepoID: repoID,
639-
RunID: run.ID,
640-
})
641-
recordsToDelete = append(recordsToDelete, &actions_model.ActionRun{
642-
RepoID: repoID,
643-
ID: run.ID,
644-
})
645-
recordsToDelete = append(recordsToDelete, &actions_model.ActionArtifact{
646-
RepoID: repoID,
647-
RunID: run.ID,
648-
})
649-
650-
if err := db.WithTx(ctx, func(ctx context.Context) error {
651-
return db.DeleteBeans(ctx, recordsToDelete...)
652-
}); err != nil {
653-
ctx.HTTPError(http.StatusInternalServerError, err.Error())
654-
return
655-
}
656-
657-
// Delete files on storage
658-
for _, task := range tasks {
659-
err := actions.RemoveLogs(ctx, task.LogInStorage, task.LogFilename)
660-
if err != nil {
661-
log.Error("remove log file %q: %v", task.LogFilename, err)
662-
}
663-
}
664-
for _, art := range artifacts {
665-
if err := storage.ActionsArtifacts.Delete(art.StoragePath); err != nil {
666-
log.Error("remove artifact file %q: %v", art.StoragePath, err)
667-
}
668-
}
669-
670-
// TODO: Delete commit status? Looks like it has no direct reference to a run/task/job. Not quite feasible without modifying db models (Dangerous).
671-
672606
ctx.JSON(http.StatusOK, struct{}{})
673607
}
674608

0 commit comments

Comments
 (0)