Skip to content

Commit 6370d2f

Browse files
authored
Detect whether action view branch was deleted (#32764)
Fix #32761 ![图片](https://github.com/user-attachments/assets/a5a7eef8-0fea-4242-b199-1b0b73d9bbdb)
1 parent c9487a7 commit 6370d2f

File tree

7 files changed

+69
-9
lines changed

7 files changed

+69
-9
lines changed

models/actions/run.go

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type ActionRun struct {
3737
TriggerUser *user_model.User `xorm:"-"`
3838
ScheduleID int64
3939
Ref string `xorm:"index"` // the commit/tag/… that caused the run
40+
IsRefDeleted bool `xorm:"-"`
4041
CommitSHA string
4142
IsForkPullRequest bool // If this is triggered by a PR from a forked repository or an untrusted user, we need to check if it is approved and limit permissions when running the workflow.
4243
NeedApproval bool // may need approval if it's a fork pull request

models/git/branch.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
repo_model "code.gitea.io/gitea/models/repo"
1313
"code.gitea.io/gitea/models/unit"
1414
user_model "code.gitea.io/gitea/models/user"
15+
"code.gitea.io/gitea/modules/container"
1516
"code.gitea.io/gitea/modules/git"
1617
"code.gitea.io/gitea/modules/log"
1718
"code.gitea.io/gitea/modules/optional"
@@ -169,9 +170,22 @@ func GetBranch(ctx context.Context, repoID int64, branchName string) (*Branch, e
169170
return &branch, nil
170171
}
171172

172-
func GetBranches(ctx context.Context, repoID int64, branchNames []string) ([]*Branch, error) {
173+
func GetBranches(ctx context.Context, repoID int64, branchNames []string, includeDeleted bool) ([]*Branch, error) {
173174
branches := make([]*Branch, 0, len(branchNames))
174-
return branches, db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames).Find(&branches)
175+
176+
sess := db.GetEngine(ctx).Where("repo_id=?", repoID).In("name", branchNames)
177+
if !includeDeleted {
178+
sess.And("is_deleted=?", false)
179+
}
180+
return branches, sess.Find(&branches)
181+
}
182+
183+
func BranchesToNamesSet(branches []*Branch) container.Set[string] {
184+
names := make(container.Set[string], len(branches))
185+
for _, branch := range branches {
186+
names.Add(branch.Name)
187+
}
188+
return names
175189
}
176190

177191
func AddBranches(ctx context.Context, branches []*Branch) error {

routers/web/repo/actions/actions.go

+32
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ func List(ctx *context.Context) {
245245
return
246246
}
247247

248+
if err := loadIsRefDeleted(ctx, runs); err != nil {
249+
log.Error("LoadIsRefDeleted", err)
250+
}
251+
248252
ctx.Data["Runs"] = runs
249253

250254
actors, err := actions_model.GetActors(ctx, ctx.Repo.Repository.ID)
@@ -267,6 +271,34 @@ func List(ctx *context.Context) {
267271
ctx.HTML(http.StatusOK, tplListActions)
268272
}
269273

274+
// loadIsRefDeleted loads the IsRefDeleted field for each run in the list.
275+
// 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+
branches := make(container.Set[string], len(runs))
278+
for _, run := range runs {
279+
refName := git.RefName(run.Ref)
280+
if refName.IsBranch() {
281+
branches.Add(refName.ShortName())
282+
}
283+
}
284+
if len(branches) == 0 {
285+
return nil
286+
}
287+
288+
branchInfos, err := git_model.GetBranches(ctx, ctx.Repo.Repository.ID, branches.Values(), false)
289+
if err != nil {
290+
return err
291+
}
292+
branchSet := git_model.BranchesToNamesSet(branchInfos)
293+
for _, run := range runs {
294+
refName := git.RefName(run.Ref)
295+
if refName.IsBranch() && !branchSet.Contains(run.Ref) {
296+
run.IsRefDeleted = true
297+
}
298+
}
299+
return nil
300+
}
301+
270302
type WorkflowDispatchInput struct {
271303
Name string `yaml:"name"`
272304
Description string `yaml:"description"`

routers/web/repo/actions/view.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919

2020
actions_model "code.gitea.io/gitea/models/actions"
2121
"code.gitea.io/gitea/models/db"
22+
git_model "code.gitea.io/gitea/models/git"
2223
"code.gitea.io/gitea/models/perm"
2324
access_model "code.gitea.io/gitea/models/perm/access"
2425
repo_model "code.gitea.io/gitea/models/repo"
@@ -136,8 +137,9 @@ type ViewUser struct {
136137
}
137138

138139
type ViewBranch struct {
139-
Name string `json:"name"`
140-
Link string `json:"link"`
140+
Name string `json:"name"`
141+
Link string `json:"link"`
142+
IsDeleted bool `json:"isDeleted"`
141143
}
142144

143145
type ViewJobStep struct {
@@ -236,6 +238,16 @@ func ViewPost(ctx *context_module.Context) {
236238
Name: run.PrettyRef(),
237239
Link: run.RefLink(),
238240
}
241+
refName := git.RefName(run.Ref)
242+
if refName.IsBranch() {
243+
b, err := git_model.GetBranch(ctx, ctx.Repo.Repository.ID, refName.ShortName())
244+
if err != nil && !git_model.IsErrBranchNotExist(err) {
245+
log.Error("GetBranch: %v", err)
246+
} else if git_model.IsErrBranchNotExist(err) || (b != nil && b.IsDeleted) {
247+
branch.IsDeleted = true
248+
}
249+
}
250+
239251
resp.State.Run.Commit = ViewCommit{
240252
ShortSha: base.ShortSha(run.CommitSHA),
241253
Link: fmt.Sprintf("%s/commit/%s", run.Repo.Link(), run.CommitSHA),

services/repository/branch.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func SyncBranchesToDB(ctx context.Context, repoID, pusherID int64, branchNames,
305305
}
306306

307307
return db.WithTx(ctx, func(ctx context.Context) error {
308-
branches, err := git_model.GetBranches(ctx, repoID, branchNames)
308+
branches, err := git_model.GetBranches(ctx, repoID, branchNames, true)
309309
if err != nil {
310310
return fmt.Errorf("git_model.GetBranches: %v", err)
311311
}

templates/repo/actions/runs_list.tmpl

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
</div>
2828
</div>
2929
<div class="flex-item-trailing">
30-
{{if .RefLink}}
31-
<a class="ui label run-list-ref gt-ellipsis" href="{{.RefLink}}">{{.PrettyRef}}</a>
30+
{{if .IsRefDeleted}}
31+
<span class="ui label run-list-ref gt-ellipsis tw-line-through" data-tooltip-content="{{.PrettyRef}}">{{.PrettyRef}}</span>
3232
{{else}}
33-
<span class="ui label run-list-ref gt-ellipsis">{{.PrettyRef}}</span>
33+
<a class="ui label run-list-ref gt-ellipsis" href="{{.RefLink}}" data-tooltip-content="{{.PrettyRef}}">{{.PrettyRef}}</a>
3434
{{end}}
3535
<div class="run-list-item-right">
3636
<div class="run-list-meta">{{svg "octicon-calendar" 16}}{{DateUtils.TimeSince .Updated}}</div>

web_src/js/components/RepoActionView.vue

+2-1
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,8 @@ export function initRepositoryActionView() {
429429
<a class="muted" :href="run.commit.pusher.link">{{ run.commit.pusher.displayName }}</a>
430430
</template>
431431
<span class="ui label tw-max-w-full" v-if="run.commit.shortSHA">
432-
<a class="gt-ellipsis" :href="run.commit.branch.link">{{ run.commit.branch.name }}</a>
432+
<span v-if="run.commit.branch.isDeleted" class="gt-ellipsis tw-line-through" :data-tooltip-content="run.commit.branch.name">{{ run.commit.branch.name }}</span>
433+
<a v-else class="gt-ellipsis" :href="run.commit.branch.link" :data-tooltip-content="run.commit.branch.name">{{ run.commit.branch.name }}</a>
433434
</span>
434435
</div>
435436
</div>

0 commit comments

Comments
 (0)