From 3dea5ee2fb7ade5d040951acdddb72f9ecdcc8ff Mon Sep 17 00:00:00 2001 From: BoYanZh Date: Sun, 10 Nov 2024 19:26:52 -0500 Subject: [PATCH] fix: clearer logic on getting ref --- routers/web/repo/actions/badge.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/routers/web/repo/actions/badge.go b/routers/web/repo/actions/badge.go index 56debcc1f1..30a5bb3b24 100644 --- a/routers/web/repo/actions/badge.go +++ b/routers/web/repo/actions/badge.go @@ -21,22 +21,25 @@ func GetWorkflowBadge(ctx *context.Context) { branch := ctx.Req.URL.Query().Get("branch") tag := ctx.Req.URL.Query().Get("tag") useLatestTag := ctx.Req.URL.Query().Has("latest_tag") - if branch == "" && tag == "" && !useLatestTag { - branch = ctx.Repo.Repository.DefaultBranch - } - ref := fmt.Sprintf("refs/heads/%s", branch) - if branch == "" && tag != "" { - if useLatestTag { - tags, _, err := ctx.Repo.GitRepo.GetTagInfos(0, 1) - if err != nil { - ctx.ServerError("GetTagInfos", err) - return - } - if len(tags) != 0 { - tag = tags[0].Name - } + var ref string + switch { + case useLatestTag: + tags, _, err := ctx.Repo.GitRepo.GetTagInfos(0, 1) + if err != nil { + ctx.ServerError("GetTagInfos", err) + return + } + if len(tags) != 0 { + tag = tags[0].Name } ref = fmt.Sprintf("refs/tags/%s", tag) + case tag != "": + ref = fmt.Sprintf("refs/tags/%s", tag) + case branch != "": + ref = fmt.Sprintf("refs/heads/%s", branch) + default: + branch = ctx.Repo.Repository.DefaultBranch + ref = fmt.Sprintf("refs/heads/%s", branch) } event := ctx.Req.URL.Query().Get("event")