mirror of
https://github.com/go-gitea/gitea
synced 2025-07-03 09:07:19 +00:00
Implement actions badge svgs (#28102)
replace #27187 close #23688 The badge has two parts: label(workflow name) and message(action status). 5 colors are provided with 7 statuses. Color mapping: ```go var statusColorMap = map[actions_model.Status]string{ actions_model.StatusSuccess: "#4c1", // Green actions_model.StatusSkipped: "#dfb317", // Yellow actions_model.StatusUnknown: "#97ca00", // Light Green actions_model.StatusFailure: "#e05d44", // Red actions_model.StatusCancelled: "#fe7d37", // Orange actions_model.StatusWaiting: "#dfb317", // Yellow actions_model.StatusRunning: "#dfb317", // Yellow actions_model.StatusBlocked: "#dfb317", // Yellow } ``` preview:      --------- Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io> Co-authored-by: delvh <dev.lh@web.de>
This commit is contained in:
56
routers/web/repo/actions/badge.go
Normal file
56
routers/web/repo/actions/badge.go
Normal file
@ -0,0 +1,56 @@
|
||||
// Copyright 2024 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package actions
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
actions_model "code.gitea.io/gitea/models/actions"
|
||||
"code.gitea.io/gitea/modules/badge"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
)
|
||||
|
||||
func GetWorkflowBadge(ctx *context.Context) {
|
||||
workflowFile := ctx.Params("workflow_name")
|
||||
branch := ctx.Req.URL.Query().Get("branch")
|
||||
if branch == "" {
|
||||
branch = ctx.Repo.Repository.DefaultBranch
|
||||
}
|
||||
branchRef := fmt.Sprintf("refs/heads/%s", branch)
|
||||
event := ctx.Req.URL.Query().Get("event")
|
||||
|
||||
badge, err := getWorkflowBadge(ctx, workflowFile, branchRef, event)
|
||||
if err != nil {
|
||||
ctx.ServerError("GetWorkflowBadge", err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx.Data["Badge"] = badge
|
||||
ctx.RespHeader().Set("Content-Type", "image/svg+xml")
|
||||
ctx.HTML(http.StatusOK, "shared/actions/runner_badge")
|
||||
}
|
||||
|
||||
func getWorkflowBadge(ctx *context.Context, workflowFile, branchName, 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)
|
||||
if err != nil {
|
||||
if errors.Is(err, util.ErrNotExist) {
|
||||
return badge.GenerateBadge(workflowName, "no status", badge.DefaultColor), nil
|
||||
}
|
||||
return badge.Badge{}, err
|
||||
}
|
||||
|
||||
color, ok := badge.StatusColorMap[run.Status]
|
||||
if !ok {
|
||||
return badge.GenerateBadge(workflowName, "unknown status", badge.DefaultColor), nil
|
||||
}
|
||||
return badge.GenerateBadge(workflowName, run.Status.String(), color), nil
|
||||
}
|
Reference in New Issue
Block a user