Skip to content

Commit 866656b

Browse files
committed
use GetRunByRepoAndID to simplify logic
1 parent 3822673 commit 866656b

File tree

3 files changed

+13
-23
lines changed

3 files changed

+13
-23
lines changed

models/actions/run.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,13 @@ func InsertRun(ctx context.Context, run *ActionRun, jobs []*jobparser.SingleWork
343343
return committer.Commit()
344344
}
345345

346-
func GetRunByID(ctx context.Context, id int64) (*ActionRun, error) {
346+
func GetRunByRepoAndID(ctx context.Context, repoID, runID int64) (*ActionRun, error) {
347347
var run ActionRun
348-
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&run)
348+
has, err := db.GetEngine(ctx).Where("id=? AND repo_id=?", runID, repoID).Get(&run)
349349
if err != nil {
350350
return nil, err
351351
} else if !has {
352-
return nil, fmt.Errorf("run with id %d: %w", id, util.ErrNotExist)
352+
return nil, fmt.Errorf("run with id %d: %w", runID, util.ErrNotExist)
353353
}
354354

355355
return &run, nil
@@ -420,17 +420,13 @@ func UpdateRun(ctx context.Context, run *ActionRun, cols ...string) error {
420420

421421
if run.Status != 0 || slices.Contains(cols, "status") {
422422
if run.RepoID == 0 {
423-
run, err = GetRunByID(ctx, run.ID)
423+
run, err = GetRunByRepoAndID(ctx, run.RepoID, run.ID)
424424
if err != nil {
425425
return err
426426
}
427427
}
428-
if run.Repo == nil {
429-
repo, err := repo_model.GetRepositoryByID(ctx, run.RepoID)
430-
if err != nil {
431-
return err
432-
}
433-
run.Repo = repo
428+
if err = run.LoadRepo(ctx); err != nil {
429+
return err
434430
}
435431
if err := updateRepoRunsNumbers(ctx, run.Repo); err != nil {
436432
return err

models/actions/run_job.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (job *ActionRunJob) Duration() time.Duration {
5151

5252
func (job *ActionRunJob) LoadRun(ctx context.Context) error {
5353
if job.Run == nil {
54-
run, err := GetRunByID(ctx, job.RunID)
54+
run, err := GetRunByRepoAndID(ctx, job.RepoID, job.RunID)
5555
if err != nil {
5656
return err
5757
}
@@ -142,7 +142,7 @@ func UpdateRunJob(ctx context.Context, job *ActionRunJob, cond builder.Cond, col
142142
{
143143
// Other goroutines may aggregate the status of the run and update it too.
144144
// So we need load the run and its jobs before updating the run.
145-
run, err := GetRunByID(ctx, job.RunID)
145+
run, err := GetRunByRepoAndID(ctx, job.RepoID, job.RunID)
146146
if err != nil {
147147
return 0, err
148148
}

routers/api/v1/repo/action.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,18 +1093,12 @@ func DeleteActionRun(ctx *context.APIContext) {
10931093
// "$ref": "#/responses/notFound"
10941094

10951095
runID := ctx.PathParamInt64("run")
1096-
1097-
run, err := actions_model.GetRunByID(ctx, runID)
1098-
if err != nil {
1099-
if errors.Is(err, util.ErrNotExist) {
1100-
ctx.APIError(http.StatusNotFound, err)
1101-
return
1102-
}
1103-
ctx.APIErrorInternal(err)
1096+
run, err := actions_model.GetRunByRepoAndID(ctx, ctx.Repo.Repository.ID, runID)
1097+
if errors.Is(err, util.ErrNotExist) {
1098+
ctx.APIError(http.StatusNotFound, err)
11041099
return
1105-
}
1106-
if run.RepoID != ctx.Repo.Repository.ID {
1107-
ctx.APIError(http.StatusNotFound, fmt.Errorf("run with id %d: %w", runID, util.ErrNotExist))
1100+
} else if err != nil {
1101+
ctx.APIErrorInternal(err)
11081102
return
11091103
}
11101104
if !run.Status.IsDone() {

0 commit comments

Comments
 (0)