fix(actions): address workflow status badge review feedback (#38241)

Follow
https://github.com/go-gitea/gitea/pull/38196#discussion_r3487219492

---------

Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
Co-authored-by: bircni <bircni@icloud.com>
This commit is contained in:
Lunny Xiao
2026-06-28 03:53:01 -07:00
committed by GitHub
parent 1d43b736b5
commit 5b9251150c
7 changed files with 88 additions and 102 deletions
+7 -20
View File
@@ -8,7 +8,6 @@ import (
stdCtx "context"
"errors"
"fmt"
"html"
"net/http"
"net/url"
"slices"
@@ -49,12 +48,9 @@ type WorkflowInfo struct {
}
type workflowBadge struct {
URL string
WorkflowURL string
Markdown string
MarkdownAltText string
HTML string
HTMLAltText string
DisplayName string
BadgeURL string
WorkflowURL string
}
// DisplayName returns the workflow name from the YAML file if present, otherwise the filename.
@@ -616,28 +612,19 @@ func prepareWorkflowBadgeTemplate(ctx *context.Context, workflowID, displayName
if workflowID == "" {
return
}
if displayName == "" {
displayName = workflowID
}
displayName = util.IfZero(displayName, workflowID)
repoURL := ctx.Repo.Repository.HTMLURL(ctx)
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", repoURL, util.PathEscapeSegments(workflowID), url.QueryEscape(ctx.Repo.Repository.DefaultBranch))
workflowURL := fmt.Sprintf("%s/actions?workflow=%s", repoURL, url.QueryEscape(workflowID))
ctx.Data["WorkflowBadge"] = workflowBadge{
URL: badgeURL,
WorkflowURL: workflowURL,
Markdown: fmt.Sprintf("[![%s](%s)](%s)", escapeMarkdownImageAltText(displayName), badgeURL, workflowURL),
MarkdownAltText: escapeMarkdownImageAltText(displayName),
HTML: fmt.Sprintf(`<a href="%s"><img src="%s" alt="%s"></a>`, html.EscapeString(workflowURL), html.EscapeString(badgeURL), html.EscapeString(displayName)),
HTMLAltText: displayName,
BadgeURL: badgeURL,
WorkflowURL: workflowURL,
DisplayName: displayName,
}
}
func escapeMarkdownImageAltText(s string) string {
return strings.NewReplacer(`\`, `\\`, `[`, `\[`, `]`, `\]`).Replace(s)
}
// loadIsRefDeleted loads the IsRefDeleted field for each run in the list.
// TODO: move this function to models/actions/run_list.go but now it will result in a circular import.
func loadIsRefDeleted(ctx stdCtx.Context, repoID int64, runs actions_model.RunList) error {
+2 -9
View File
@@ -184,10 +184,7 @@ func Test_loadIsRefDeleted(t *testing.T) {
}
func TestPrepareWorkflowBadgeTemplate(t *testing.T) {
defer test.MockVariableValue(&setting.IsInTesting, true)()
defer test.MockVariableValue(&setting.AppURL, "https://gitea.example.com/")()
defer test.MockVariableValue(&setting.AppSubURL, "")()
defer test.MockVariableValue(&setting.PublicURLDetection, setting.PublicURLNever)()
t.Run("no workflow selected", func(t *testing.T) {
ctx := newWorkflowBadgeTestContext(t)
@@ -203,13 +200,9 @@ func TestPrepareWorkflowBadgeTemplate(t *testing.T) {
prepareWorkflowBadgeTemplate(ctx, "build/test workflow.yml", `CI [prod]\build "fast" <ok>`)
assert.Equal(t, workflowBadge{
URL: "https://gitea.example.com/user1/repo1/actions/workflows/build/test%20workflow.yml/badge.svg?branch=release%2F1.0+%26+hotfix",
BadgeURL: "https://gitea.example.com/user1/repo1/actions/workflows/build/test%20workflow.yml/badge.svg?branch=release%2F1.0+%26+hotfix",
WorkflowURL: "https://gitea.example.com/user1/repo1/actions?workflow=build%2Ftest+workflow.yml",
Markdown: `[![CI \[prod\]\\build "fast" <ok>](https://gitea.example.com/user1/repo1/actions/workflows/build/test%20workflow.yml/badge.svg?branch=release%2F1.0+%26+hotfix)]` +
`(https://gitea.example.com/user1/repo1/actions?workflow=build%2Ftest+workflow.yml)`,
MarkdownAltText: `CI \[prod\]\\build "fast" <ok>`,
HTML: `<a href="https://gitea.example.com/user1/repo1/actions?workflow=build%2Ftest+workflow.yml"><img src="https://gitea.example.com/user1/repo1/actions/workflows/build/test%20workflow.yml/badge.svg?branch=release%2F1.0+%26+hotfix" alt="CI [prod]\build &#34;fast&#34; &lt;ok&gt;"></a>`,
HTMLAltText: `CI [prod]\build "fast" <ok>`,
DisplayName: `CI [prod]\build "fast" <ok>`,
}, ctx.Data["WorkflowBadge"])
})
}