Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: badge support tag #32451

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
30 changes: 25 additions & 5 deletions routers/web/repo/actions/badge.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,33 @@ 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")
var ref string
switch {
case useLatestTag:
tags, _, err := ctx.Repo.GitRepo.GetTagInfos(0, 1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use a function like GetLatestReleaseByRepoID to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we need the tag of the latest release here? Do we need to rename it to latest_release_tag?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. All tags have been synced into databases. They shared the same table with releases.

if err != nil {
ctx.ServerError("GetTagInfos", err)
return
}
if len(tags) != 0 {
tag = tags[0].Name
} else {
tag = "" // return empty result on no tag
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's empty, we should return error.

}
ref = fmt.Sprintf("refs/tags/%s", tag)
BoYanZh marked this conversation as resolved.
Show resolved Hide resolved
case tag != "":
ref = fmt.Sprintf("refs/tags/%s", tag)
case branch != "":
ref = fmt.Sprintf("refs/heads/%s", branch)
default:
branch = ctx.Repo.Repository.DefaultBranch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assign statement is unnecessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I

if useLatestTag {
	tag = "xxx"
}
switch {
  // ...
}

or add a fallthrough?

ref = fmt.Sprintf("refs/heads/%s", branch)
}
branchRef := fmt.Sprintf("refs/heads/%s", branch)
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
Expand All @@ -36,11 +56,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
Expand Down
Loading