Skip to content

Commit 4209084

Browse files
lunnywxiaoguang
andauthored
Fix bug on action list deleted branch (#32848)
Fix #32761 (comment) --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent c8ea41b commit 4209084

File tree

5 files changed

+72
-4
lines changed

5 files changed

+72
-4
lines changed

models/fixtures/action_run.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,22 @@
5555
updated: 1683636626
5656
need_approval: 0
5757
approved_by: 0
58+
-
59+
id: 794
60+
title: "job output"
61+
repo_id: 4
62+
owner_id: 1
63+
workflow_id: "test.yaml"
64+
index: 190
65+
trigger_user_id: 1
66+
ref: "refs/heads/test"
67+
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
68+
event: "push"
69+
is_fork_pull_request: 0
70+
status: 1
71+
started: 1683636528
72+
stopped: 1683636626
73+
created: 1683636108
74+
updated: 1683636626
75+
need_approval: 0
76+
approved_by: 0

models/fixtures/branch.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,15 @@
8181
is_deleted: false
8282
deleted_by_id: 0
8383
deleted_unix: 0
84+
85+
-
86+
id: 15
87+
repo_id: 4
88+
name: 'master'
89+
commit_id: 'c7cd3cd144e6d23c9d6f3d07e52b2c1a956e0338'
90+
commit_message: 'add Readme'
91+
commit_time: 1588147171
92+
pusher_id: 13
93+
is_deleted: false
94+
deleted_by_id: 0
95+
deleted_unix: 0

routers/web/repo/actions/actions.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package actions
55

66
import (
77
"bytes"
8+
stdCtx "context"
89
"fmt"
910
"net/http"
1011
"slices"
@@ -245,7 +246,7 @@ func List(ctx *context.Context) {
245246
return
246247
}
247248

248-
if err := loadIsRefDeleted(ctx, runs); err != nil {
249+
if err := loadIsRefDeleted(ctx, ctx.Repo.Repository.ID, runs); err != nil {
249250
log.Error("LoadIsRefDeleted", err)
250251
}
251252

@@ -273,7 +274,7 @@ func List(ctx *context.Context) {
273274

274275
// loadIsRefDeleted loads the IsRefDeleted field for each run in the list.
275276
// TODO: move this function to models/actions/run_list.go but now it will result in a circular import.
276-
func loadIsRefDeleted(ctx *context.Context, runs actions_model.RunList) error {
277+
func loadIsRefDeleted(ctx stdCtx.Context, repoID int64, runs actions_model.RunList) error {
277278
branches := make(container.Set[string], len(runs))
278279
for _, run := range runs {
279280
refName := git.RefName(run.Ref)
@@ -285,14 +286,14 @@ func loadIsRefDeleted(ctx *context.Context, runs actions_model.RunList) error {
285286
return nil
286287
}
287288

288-
branchInfos, err := git_model.GetBranches(ctx, ctx.Repo.Repository.ID, branches.Values(), false)
289+
branchInfos, err := git_model.GetBranches(ctx, repoID, branches.Values(), false)
289290
if err != nil {
290291
return err
291292
}
292293
branchSet := git_model.BranchesToNamesSet(branchInfos)
293294
for _, run := range runs {
294295
refName := git.RefName(run.Ref)
295-
if refName.IsBranch() && !branchSet.Contains(run.Ref) {
296+
if refName.IsBranch() && !branchSet.Contains(refName.ShortName()) {
296297
run.IsRefDeleted = true
297298
}
298299
}

routers/web/repo/actions/actions_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ import (
77
"strings"
88
"testing"
99

10+
actions_model "code.gitea.io/gitea/models/actions"
11+
"code.gitea.io/gitea/models/db"
12+
unittest "code.gitea.io/gitea/models/unittest"
13+
1014
act_model "github.com/nektos/act/pkg/model"
1115
"github.com/stretchr/testify/assert"
1216
)
@@ -154,3 +158,21 @@ func TestReadWorkflow_WorkflowDispatchConfig(t *testing.T) {
154158
Type: "boolean",
155159
}, workflowDispatch.Inputs[2])
156160
}
161+
162+
func Test_loadIsRefDeleted(t *testing.T) {
163+
unittest.PrepareTestEnv(t)
164+
165+
runs, total, err := db.FindAndCount[actions_model.ActionRun](db.DefaultContext,
166+
actions_model.FindRunOptions{RepoID: 4, Ref: "refs/heads/test"})
167+
assert.NoError(t, err)
168+
assert.Len(t, runs, 1)
169+
assert.EqualValues(t, 1, total)
170+
for _, run := range runs {
171+
assert.False(t, run.IsRefDeleted)
172+
}
173+
174+
assert.NoError(t, loadIsRefDeleted(db.DefaultContext, 4, runs))
175+
for _, run := range runs {
176+
assert.True(t, run.IsRefDeleted)
177+
}
178+
}

routers/web/repo/actions/main_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2024 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package actions
5+
6+
import (
7+
"testing"
8+
9+
"code.gitea.io/gitea/models/unittest"
10+
)
11+
12+
func TestMain(m *testing.M) {
13+
unittest.MainTest(m)
14+
}

0 commit comments

Comments
 (0)