Skip to content
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
5 changes: 5 additions & 0 deletions options/locale/locale_en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -3833,6 +3833,11 @@
"actions.workflow.enable_success": "Workflow '%s' enabled successfully.",
"actions.workflow.disabled": "Workflow is disabled.",
"actions.workflow.run": "Run Workflow",
"actions.workflow.create_status_badge": "Create Status Badge",
"actions.workflow.status_badge": "Status Badge",
"actions.workflow.status_badge_url": "Badge URL",
"actions.workflow.status_badge_markdown": "Markdown",
"actions.workflow.status_badge_html": "HTML",
"actions.workflow.not_found": "Workflow '%s' not found.",
"actions.workflow.run_success": "Workflow '%s' run successfully.",
"actions.workflow.from_ref": "Use workflow from",
Expand Down
5 changes: 5 additions & 0 deletions options/locale/locale_zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -3833,6 +3833,11 @@
"actions.workflow.enable_success": "工作流「%s」已成功启用。",
"actions.workflow.disabled": "工作流已禁用。",
"actions.workflow.run": "运行工作流",
"actions.workflow.create_status_badge": "创建状态徽章",
"actions.workflow.status_badge": "状态徽章",
"actions.workflow.status_badge_url": "徽章 URL",
"actions.workflow.status_badge_markdown": "Markdown",
"actions.workflow.status_badge_html": "HTML",
"actions.workflow.not_found": "未找到工作流「%s」。",
"actions.workflow.run_success": "工作流「%s」已成功运行。",
"actions.workflow.from_ref": "使用工作流从",
Expand Down
38 changes: 37 additions & 1 deletion routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
stdCtx "context"
"errors"
"fmt"
"html"
"net/http"
"net/url"
"slices"
Expand Down Expand Up @@ -47,6 +48,12 @@ type WorkflowInfo struct {
Workflow *act_model.Workflow
}

type workflowBadge struct {
URL string
Markdown string
HTML string
}

// DisplayName returns the workflow name from the YAML file if present, otherwise the filename.
func (w WorkflowInfo) DisplayName() string {
if w.Workflow != nil && w.Workflow.Name != "" {
Expand Down Expand Up @@ -403,10 +410,16 @@ func prepareWorkflowList(ctx *context.Context, workflows []WorkflowInfo, otherWo
ctx.Data["Runs"] = runs

workflowNames := make(map[string]string, len(workflows))
workflowDisplayName := workflowID
for _, wf := range workflows {
workflowNames[wf.Entry.Name()] = wf.DisplayName()
displayName := wf.DisplayName()
workflowNames[wf.Entry.Name()] = displayName
if wf.Entry.Name() == workflowID {
workflowDisplayName = displayName
}
}
ctx.Data["WorkflowNames"] = workflowNames
prepareWorkflowBadgeTemplate(ctx, workflowID, workflowDisplayName)

actors, err := actions_model.GetActors(ctx, ctx.Repo.Repository.ID)
if err != nil {
Expand All @@ -432,6 +445,29 @@ func prepareWorkflowList(ctx *context.Context, workflows []WorkflowInfo, otherWo
ctx.Data["CanWriteRepoUnitActions"] = ctx.Repo.Permission.CanWrite(unit.TypeActions)
}

func prepareWorkflowBadgeTemplate(ctx *context.Context, workflowID, displayName string) {
if workflowID == "" {
return
}
if displayName == "" {
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,
Markdown: fmt.Sprintf("[![%s](%s)](%s)", escapeMarkdownImageAltText(displayName), badgeURL, workflowURL),
HTML: fmt.Sprintf(`<a href="%s"><img src="%s" alt="%s"></a>`, html.EscapeString(workflowURL), html.EscapeString(badgeURL), html.EscapeString(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 {
Expand Down
35 changes: 35 additions & 0 deletions templates/repo/actions/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@
</div>
</div>

{{if .WorkflowBadge}}
<button class="ui mini button show-modal" data-modal="#workflow-status-badge-modal">
<span class="flex-text-inline">{{svg "octicon-file-badge" 14}}{{ctx.Locale.Tr "actions.workflow.create_status_badge"}}</span>
</button>
{{end}}

{{if and .AllowDisableOrEnableWorkflow .CurWorkflowIsListed $.CurWorkflow}}
<button class="ui jump dropdown btn interact-bg tw-p-2">
{{svg "octicon-kebab-horizontal"}}
Expand All @@ -126,6 +132,35 @@
<div class="ui attached segment">
{{template "repo/actions/runs_list" .}}
</div>
{{if .WorkflowBadge}}
<div id="workflow-status-badge-modal" class="ui tiny modal">
<div class="header">{{ctx.Locale.Tr "actions.workflow.status_badge"}}</div>
<div class="content">
<div class="ui form">
<div class="field">
<img src="{{.WorkflowBadge.URL}}" alt="{{.CurWorkflow}}">
</div>
<div class="field">
<label>{{ctx.Locale.Tr "actions.workflow.status_badge_url"}}</label>
<div class="ui action input">
<input id="workflow-badge-url" readonly value="{{.WorkflowBadge.URL}}">
<button class="ui icon button" data-tooltip-content="{{ctx.Locale.Tr "copy"}}" data-clipboard-target="#workflow-badge-url">{{svg "octicon-copy" 14}}</button>
</div>
</div>
<div class="field">
<label>{{ctx.Locale.Tr "actions.workflow.status_badge_markdown"}}</label>
<textarea id="workflow-badge-markdown" rows="2" readonly>{{.WorkflowBadge.Markdown}}</textarea>
<button class="ui icon button" data-tooltip-content="{{ctx.Locale.Tr "copy"}}" data-clipboard-target="#workflow-badge-markdown">{{svg "octicon-copy" 14}}</button>
</div>
<div class="field">
<label>{{ctx.Locale.Tr "actions.workflow.status_badge_html"}}</label>
<textarea id="workflow-badge-html" rows="2" readonly>{{.WorkflowBadge.HTML}}</textarea>
<button class="ui icon button" data-tooltip-content="{{ctx.Locale.Tr "copy"}}" data-clipboard-target="#workflow-badge-html">{{svg "octicon-copy" 14}}</button>
</div>
</div>
</div>
</div>
{{end}}
</div>
</div>
{{else}}
Expand Down
Loading