From cbd4fb8a526303bf99a713920bcbb15548f73626 Mon Sep 17 00:00:00 2001 From: BoYanZh Date: Fri, 8 Nov 2024 00:25:09 -0500 Subject: [PATCH 1/3] feat: badge support tag --- models/actions/run.go | 8 ++++---- routers/web/repo/actions/badge.go | 25 ++++++++++++++++++++----- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/models/actions/run.go b/models/actions/run.go index 37064520a213a..f5e5d9afb0936 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -374,10 +374,10 @@ func GetLatestRun(ctx context.Context, repoID int64) (*ActionRun, error) { return run, nil } -func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, branch, event string) (*ActionRun, error) { +func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, ref, event string) (*ActionRun, error) { var run ActionRun - q := db.GetEngine(ctx).Where("repo_id=?", repoID). - And("ref = ?", branch). + q := db.GetEngine(ctx).Where("repo_id = ?", repoID). + And("ref = ?", ref). And("workflow_id = ?", workflowFile) if event != "" { q.And("event = ?", event) @@ -386,7 +386,7 @@ func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, branc if err != nil { return nil, err } else if !has { - return nil, util.NewNotExistErrorf("run with repo_id %d, ref %s, workflow_id %s", repoID, branch, workflowFile) + return nil, util.NewNotExistErrorf("run with repo_id %d, ref %s, workflow_id %s", repoID, ref, workflowFile) } return &run, nil } diff --git a/routers/web/repo/actions/badge.go b/routers/web/repo/actions/badge.go index e920ecaf58063..56debcc1f14fc 100644 --- a/routers/web/repo/actions/badge.go +++ b/routers/web/repo/actions/badge.go @@ -19,13 +19,28 @@ import ( func GetWorkflowBadge(ctx *context.Context) { workflowFile := ctx.PathParam("workflow_name") branch := ctx.Req.URL.Query().Get("branch") - if 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 } - branchRef := fmt.Sprintf("refs/heads/%s", branch) + 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 + } + } + ref = fmt.Sprintf("refs/tags/%s", tag) + } event := ctx.Req.URL.Query().Get("event") - badge, err := getWorkflowBadge(ctx, workflowFile, branchRef, event) + badge, err := getWorkflowBadge(ctx, workflowFile, ref, event) if err != nil { ctx.ServerError("GetWorkflowBadge", err) return @@ -36,11 +51,11 @@ func GetWorkflowBadge(ctx *context.Context) { ctx.HTML(http.StatusOK, "shared/actions/runner_badge") } -func getWorkflowBadge(ctx *context.Context, workflowFile, branchName, event string) (badge.Badge, error) { +func getWorkflowBadge(ctx *context.Context, workflowFile, ref, event string) (badge.Badge, error) { extension := filepath.Ext(workflowFile) workflowName := strings.TrimSuffix(workflowFile, extension) - run, err := actions_model.GetWorkflowLatestRun(ctx, ctx.Repo.Repository.ID, workflowFile, branchName, event) + run, err := actions_model.GetWorkflowLatestRun(ctx, ctx.Repo.Repository.ID, workflowFile, ref, event) if err != nil { if errors.Is(err, util.ErrNotExist) { return badge.GenerateBadge(workflowName, "no status", badge.DefaultColor), nil From 3dea5ee2fb7ade5d040951acdddb72f9ecdcc8ff Mon Sep 17 00:00:00 2001 From: BoYanZh Date: Sun, 10 Nov 2024 19:26:52 -0500 Subject: [PATCH 2/3] 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 56debcc1f14fc..30a5bb3b24dd9 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") From 1637d129b9c3fd525a702ec60de9407636d2c5e3 Mon Sep 17 00:00:00 2001 From: BoYanZh Date: Wed, 13 Nov 2024 19:10:26 -0500 Subject: [PATCH 3/3] feat: return empty result on no tag --- routers/web/repo/actions/badge.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/routers/web/repo/actions/badge.go b/routers/web/repo/actions/badge.go index 30a5bb3b24dd9..2a95ed66eb2f7 100644 --- a/routers/web/repo/actions/badge.go +++ b/routers/web/repo/actions/badge.go @@ -31,6 +31,8 @@ func GetWorkflowBadge(ctx *context.Context) { } if len(tags) != 0 { tag = tags[0].Name + } else { + tag = "" // return empty result on no tag } ref = fmt.Sprintf("refs/tags/%s", tag) case tag != "":