Skip to content

Commit 856fb59

Browse files
committed
feat: badge support tag
1 parent 35bcd66 commit 856fb59

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

models/actions/run.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ func GetLatestRun(ctx context.Context, repoID int64) (*ActionRun, error) {
374374
return run, nil
375375
}
376376

377-
func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, branch, event string) (*ActionRun, error) {
377+
func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, ref, event string) (*ActionRun, error) {
378378
var run ActionRun
379-
q := db.GetEngine(ctx).Where("repo_id=?", repoID).
380-
And("ref = ?", branch).
379+
q := db.GetEngine(ctx).Where("repo_id = ?", repoID).
380+
And("ref = ?", ref).
381381
And("workflow_id = ?", workflowFile)
382382
if event != "" {
383383
q.And("event = ?", event)
@@ -386,7 +386,7 @@ func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, branc
386386
if err != nil {
387387
return nil, err
388388
} else if !has {
389-
return nil, util.NewNotExistErrorf("run with repo_id %d, ref %s, workflow_id %s", repoID, branch, workflowFile)
389+
return nil, util.NewNotExistErrorf("run with repo_id %d, ref %s, workflow_id %s", repoID, ref, workflowFile)
390390
}
391391
return &run, nil
392392
}

routers/web/repo/actions/badge.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,28 @@ import (
1919
func GetWorkflowBadge(ctx *context.Context) {
2020
workflowFile := ctx.PathParam("workflow_name")
2121
branch := ctx.Req.URL.Query().Get("branch")
22-
if branch == "" {
22+
tag := ctx.Req.URL.Query().Get("tag")
23+
useLatestTag := ctx.Req.URL.Query().Has("latest_tag")
24+
if branch == "" && tag == "" && !useLatestTag {
2325
branch = ctx.Repo.Repository.DefaultBranch
2426
}
25-
branchRef := fmt.Sprintf("refs/heads/%s", branch)
27+
ref := fmt.Sprintf("refs/heads/%s", branch)
28+
if branch == "" && tag != "" {
29+
if useLatestTag {
30+
tags, _, err := ctx.Repo.GitRepo.GetTagInfos(0, 1)
31+
if err != nil {
32+
ctx.ServerError("GetTagInfos", err)
33+
return
34+
}
35+
if len(tags) != 0 {
36+
tag = tags[0].Name
37+
}
38+
}
39+
ref = fmt.Sprintf("refs/tags/%s", tag)
40+
}
2641
event := ctx.Req.URL.Query().Get("event")
2742

28-
badge, err := getWorkflowBadge(ctx, workflowFile, branchRef, event)
43+
badge, err := getWorkflowBadge(ctx, workflowFile, ref, event)
2944
if err != nil {
3045
ctx.ServerError("GetWorkflowBadge", err)
3146
return
@@ -36,11 +51,11 @@ func GetWorkflowBadge(ctx *context.Context) {
3651
ctx.HTML(http.StatusOK, "shared/actions/runner_badge")
3752
}
3853

39-
func getWorkflowBadge(ctx *context.Context, workflowFile, branchName, event string) (badge.Badge, error) {
54+
func getWorkflowBadge(ctx *context.Context, workflowFile, ref, event string) (badge.Badge, error) {
4055
extension := filepath.Ext(workflowFile)
4156
workflowName := strings.TrimSuffix(workflowFile, extension)
4257

43-
run, err := actions_model.GetWorkflowLatestRun(ctx, ctx.Repo.Repository.ID, workflowFile, branchName, event)
58+
run, err := actions_model.GetWorkflowLatestRun(ctx, ctx.Repo.Repository.ID, workflowFile, ref, event)
4459
if err != nil {
4560
if errors.Is(err, util.ErrNotExist) {
4661
return badge.GenerateBadge(workflowName, "no status", badge.DefaultColor), nil

0 commit comments

Comments
 (0)