feat: badge support tag

This commit is contained in:
BoYanZh 2024-11-08 00:25:09 -05:00
parent 9880c1372e
commit cbd4fb8a52
No known key found for this signature in database
GPG Key ID: CA088E6D9284F870
2 changed files with 24 additions and 9 deletions

View File

@ -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
}

View File

@ -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