Skip to content

Commit cb05cf7

Browse files
committed
FIX
1 parent b92b3c5 commit cb05cf7

File tree

4 files changed

+82
-76
lines changed

4 files changed

+82
-76
lines changed

models/actions/run.go

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
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"
117
"context"
128
"errors"
139
"fmt"
@@ -443,74 +439,4 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
443439
return nil
444440
}
445441

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

modules/actions/run.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package actions
2+
3+
import (
4+
"code.gitea.io/gitea/models/actions"
5+
"code.gitea.io/gitea/models/db"
6+
"code.gitea.io/gitea/modules/container"
7+
"code.gitea.io/gitea/modules/log"
8+
"code.gitea.io/gitea/modules/storage"
9+
"context"
10+
)
11+
12+
// TODO: When deleting a run, it should at lease delete artifacts, tasks logs, database record.
13+
func DeleteRun(ctx context.Context, repoID int64, run *actions.ActionRun, jobs []*actions.ActionRunJob) error {
14+
tasks := make(actions.TaskList, 0)
15+
16+
jobIDs := container.FilterSlice(jobs, func(j *actions.ActionRunJob) (int64, bool) {
17+
return j.ID, j.ID != 0
18+
})
19+
20+
if len(jobIDs) > 0 {
21+
if err := db.GetEngine(ctx).Where("repo_id = ?", repoID).In("job_id", jobIDs).Find(&tasks); err != nil {
22+
return err
23+
}
24+
}
25+
26+
artifacts, err := db.Find[actions.ActionArtifact](ctx, actions.FindArtifactsOptions{
27+
RepoID: repoID,
28+
RunID: run.ID,
29+
})
30+
if err != nil {
31+
return err
32+
}
33+
34+
var recordsToDelete []any
35+
36+
for _, task := range tasks {
37+
recordsToDelete = append(recordsToDelete, &actions.ActionTask{
38+
RepoID: repoID,
39+
ID: task.ID,
40+
})
41+
recordsToDelete = append(recordsToDelete, &actions.ActionTaskStep{
42+
RepoID: repoID,
43+
TaskID: task.ID,
44+
})
45+
}
46+
recordsToDelete = append(recordsToDelete, &actions.ActionRunJob{
47+
RepoID: repoID,
48+
RunID: run.ID,
49+
})
50+
recordsToDelete = append(recordsToDelete, &actions.ActionRun{
51+
RepoID: repoID,
52+
ID: run.ID,
53+
})
54+
recordsToDelete = append(recordsToDelete, &actions.ActionArtifact{
55+
RepoID: repoID,
56+
RunID: run.ID,
57+
})
58+
59+
if err := db.WithTx(ctx, func(ctx context.Context) error {
60+
return db.DeleteBeans(ctx, recordsToDelete...)
61+
}); err != nil {
62+
return err
63+
}
64+
65+
// Delete files on storage
66+
for _, tas := range tasks {
67+
err := RemoveLogs(ctx, tas.LogInStorage, tas.LogFilename)
68+
if err != nil {
69+
log.Error("remove log file %q: %v", tas.LogFilename, err)
70+
}
71+
}
72+
for _, art := range artifacts {
73+
if err := storage.ActionsArtifacts.Delete(art.StoragePath); err != nil {
74+
log.Error("remove artifact file %q: %v", art.StoragePath, err)
75+
}
76+
}
77+
78+
// TODO: Delete commit status? Looks like it has no direct reference to a run/task/job. Not quite feasible without modifying db models (Dangerous).
79+
return nil
80+
}

routers/api/v1/repo/action.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ func DeleteActionRun(ctx *context.APIContext) {
11211121
return
11221122
}
11231123

1124-
if err := actions_model.DeleteRun(ctx, repoID, run, jobs); err != nil {
1124+
if err := actions.DeleteRun(ctx, repoID, run, jobs); err != nil {
11251125
ctx.APIErrorInternal(err)
11261126
return
11271127
}

routers/web/repo/actions/view.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ func Delete(ctx *context_module.Context) {
598598

599599
repoID := ctx.Repo.Repository.ID
600600

601-
if err := actions_model.DeleteRun(ctx, repoID, run, jobs); err != nil {
601+
if err := actions.DeleteRun(ctx, repoID, run, jobs); err != nil {
602602
ctx.HTTPError(http.StatusInternalServerError, err.Error())
603603
return
604604
}

0 commit comments

Comments
 (0)