[GITEA] Add support for shields.io-based badges
Adds a new `/{username}/{repo}/badges` family of routes, which redirect
to various shields.io badges. The goal is to not reimplement badge
generation, and delegate it to shields.io (or a similar service), which
are already used by many. This way, we get all the goodies that come
with it: different styles, colors, logos, you name it.
So these routes are just thin wrappers around shields.io that make it
easier to display the information we want. The URL is configurable via
`app.ini`, and is templatable, allowing to use alternative badge
generator services with slightly different URL patterns.
Additionally, for compatibility with GitHub, there's an
`/{username}/{repo}/actions/workflows/{workflow_file}/badge.svg` route
that works much the same way as on GitHub. Change the hostname in the
URL, and done.
Fixes gitea#5633, gitea#23688, and also fixes #126.
Work sponsored by Codeberg e.V.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit fcd0f61212d8febd4bdfc27e61a4e13cbdd16d49)
(cherry picked from commit 20d14f784490a880c51ca0f0a6a5988a01887635)
(cherry picked from commit 4359741431bb39de4cf24de8b0cfb513f5233f55)
(cherry picked from commit 35cff45eb86177e750cd22e82a201880a5efe045)
(cherry picked from commit 2fc0d0b8a302d24177a00ab48b42ce083b52e506)
2024-01-01 07:38:49 -05:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
package badges
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
actions_model "code.gitea.io/gitea/models/actions"
|
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
|
|
|
"code.gitea.io/gitea/models/unit"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2024-02-27 02:12:22 -05:00
|
|
|
context_module "code.gitea.io/gitea/services/context"
|
[GITEA] Add support for shields.io-based badges
Adds a new `/{username}/{repo}/badges` family of routes, which redirect
to various shields.io badges. The goal is to not reimplement badge
generation, and delegate it to shields.io (or a similar service), which
are already used by many. This way, we get all the goodies that come
with it: different styles, colors, logos, you name it.
So these routes are just thin wrappers around shields.io that make it
easier to display the information we want. The URL is configurable via
`app.ini`, and is templatable, allowing to use alternative badge
generator services with slightly different URL patterns.
Additionally, for compatibility with GitHub, there's an
`/{username}/{repo}/actions/workflows/{workflow_file}/badge.svg` route
that works much the same way as on GitHub. Change the hostname in the
URL, and done.
Fixes gitea#5633, gitea#23688, and also fixes #126.
Work sponsored by Codeberg e.V.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit fcd0f61212d8febd4bdfc27e61a4e13cbdd16d49)
(cherry picked from commit 20d14f784490a880c51ca0f0a6a5988a01887635)
(cherry picked from commit 4359741431bb39de4cf24de8b0cfb513f5233f55)
(cherry picked from commit 35cff45eb86177e750cd22e82a201880a5efe045)
(cherry picked from commit 2fc0d0b8a302d24177a00ab48b42ce083b52e506)
2024-01-01 07:38:49 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func getBadgeURL(ctx *context_module.Context, label, text, color string) string {
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
_ = setting.Badges.GeneratorURLTemplateTemplate.Execute(sb, map[string]string{
|
2024-03-20 16:41:20 -05:00
|
|
|
"label": url.PathEscape(strings.ReplaceAll(label, "-", "--")),
|
|
|
|
"text": url.PathEscape(strings.ReplaceAll(text, "-", "--")),
|
[GITEA] Add support for shields.io-based badges
Adds a new `/{username}/{repo}/badges` family of routes, which redirect
to various shields.io badges. The goal is to not reimplement badge
generation, and delegate it to shields.io (or a similar service), which
are already used by many. This way, we get all the goodies that come
with it: different styles, colors, logos, you name it.
So these routes are just thin wrappers around shields.io that make it
easier to display the information we want. The URL is configurable via
`app.ini`, and is templatable, allowing to use alternative badge
generator services with slightly different URL patterns.
Additionally, for compatibility with GitHub, there's an
`/{username}/{repo}/actions/workflows/{workflow_file}/badge.svg` route
that works much the same way as on GitHub. Change the hostname in the
URL, and done.
Fixes gitea#5633, gitea#23688, and also fixes #126.
Work sponsored by Codeberg e.V.
Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu>
(cherry picked from commit fcd0f61212d8febd4bdfc27e61a4e13cbdd16d49)
(cherry picked from commit 20d14f784490a880c51ca0f0a6a5988a01887635)
(cherry picked from commit 4359741431bb39de4cf24de8b0cfb513f5233f55)
(cherry picked from commit 35cff45eb86177e750cd22e82a201880a5efe045)
(cherry picked from commit 2fc0d0b8a302d24177a00ab48b42ce083b52e506)
2024-01-01 07:38:49 -05:00
|
|
|
"color": url.PathEscape(color),
|
|
|
|
})
|
|
|
|
|
|
|
|
badgeURL := sb.String()
|
|
|
|
q := ctx.Req.URL.Query()
|
|
|
|
// Remove any `branch` or `event` query parameters. They're used by the
|
|
|
|
// workflow badge route, and do not need forwarding to the badge generator.
|
|
|
|
delete(q, "branch")
|
|
|
|
delete(q, "event")
|
|
|
|
if len(q) > 0 {
|
|
|
|
return fmt.Sprintf("%s?%s", badgeURL, q.Encode())
|
|
|
|
}
|
|
|
|
return badgeURL
|
|
|
|
}
|
|
|
|
|
|
|
|
func redirectToBadge(ctx *context_module.Context, label, text, color string) {
|
|
|
|
ctx.Redirect(getBadgeURL(ctx, label, text, color))
|
|
|
|
}
|
|
|
|
|
|
|
|
func errorBadge(ctx *context_module.Context, label, text string) {
|
|
|
|
ctx.Redirect(getBadgeURL(ctx, label, text, "crimson"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetWorkflowBadge(ctx *context_module.Context) {
|
|
|
|
branch := ctx.Req.URL.Query().Get("branch")
|
|
|
|
if branch == "" {
|
|
|
|
branch = ctx.Repo.Repository.DefaultBranch
|
|
|
|
}
|
|
|
|
branch = fmt.Sprintf("refs/heads/%s", branch)
|
|
|
|
event := ctx.Req.URL.Query().Get("event")
|
|
|
|
|
|
|
|
workflowFile := ctx.Params("workflow_name")
|
|
|
|
run, err := actions_model.GetLatestRunForBranchAndWorkflow(ctx, ctx.Repo.Repository.ID, branch, workflowFile, event)
|
|
|
|
if err != nil {
|
|
|
|
errorBadge(ctx, workflowFile, "Not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var color string
|
|
|
|
switch run.Status {
|
|
|
|
case actions_model.StatusUnknown:
|
|
|
|
color = "lightgrey"
|
|
|
|
case actions_model.StatusWaiting:
|
|
|
|
color = "lightgrey"
|
|
|
|
case actions_model.StatusRunning:
|
|
|
|
color = "gold"
|
|
|
|
case actions_model.StatusSuccess:
|
|
|
|
color = "brightgreen"
|
|
|
|
case actions_model.StatusFailure:
|
|
|
|
color = "crimson"
|
|
|
|
case actions_model.StatusCancelled:
|
|
|
|
color = "orange"
|
|
|
|
case actions_model.StatusSkipped:
|
|
|
|
color = "blue"
|
|
|
|
case actions_model.StatusBlocked:
|
|
|
|
color = "yellow"
|
|
|
|
default:
|
|
|
|
color = "lightgrey"
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectToBadge(ctx, workflowFile, run.Status.String(), color)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIssueOrPullBadge(ctx *context_module.Context, label, variant string, num int) {
|
|
|
|
var text string
|
|
|
|
if len(variant) > 0 {
|
|
|
|
text = fmt.Sprintf("%d %s", num, variant)
|
|
|
|
} else {
|
|
|
|
text = fmt.Sprintf("%d", num)
|
|
|
|
}
|
|
|
|
redirectToBadge(ctx, label, text, "blue")
|
|
|
|
}
|
|
|
|
|
|
|
|
func getIssueBadge(ctx *context_module.Context, variant string, num int) {
|
|
|
|
if !ctx.Repo.CanRead(unit.TypeIssues) &&
|
|
|
|
!ctx.Repo.CanRead(unit.TypeExternalTracker) {
|
|
|
|
errorBadge(ctx, "issues", "Not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := ctx.Repo.Repository.GetUnit(ctx, unit.TypeExternalTracker)
|
|
|
|
if err == nil {
|
|
|
|
errorBadge(ctx, "issues", "Not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
getIssueOrPullBadge(ctx, "issues", variant, num)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPullBadge(ctx *context_module.Context, variant string, num int) {
|
|
|
|
if !ctx.Repo.Repository.CanEnablePulls() || !ctx.Repo.CanRead(unit.TypePullRequests) {
|
|
|
|
errorBadge(ctx, "pulls", "Not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
getIssueOrPullBadge(ctx, "pulls", variant, num)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOpenIssuesBadge(ctx *context_module.Context) {
|
|
|
|
getIssueBadge(ctx, "open", ctx.Repo.Repository.NumOpenIssues)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetClosedIssuesBadge(ctx *context_module.Context) {
|
|
|
|
getIssueBadge(ctx, "closed", ctx.Repo.Repository.NumClosedIssues)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTotalIssuesBadge(ctx *context_module.Context) {
|
|
|
|
getIssueBadge(ctx, "", ctx.Repo.Repository.NumIssues)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetOpenPullsBadge(ctx *context_module.Context) {
|
|
|
|
getPullBadge(ctx, "open", ctx.Repo.Repository.NumOpenPulls)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetClosedPullsBadge(ctx *context_module.Context) {
|
|
|
|
getPullBadge(ctx, "closed", ctx.Repo.Repository.NumClosedPulls)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetTotalPullsBadge(ctx *context_module.Context) {
|
|
|
|
getPullBadge(ctx, "", ctx.Repo.Repository.NumPulls)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetStarsBadge(ctx *context_module.Context) {
|
|
|
|
redirectToBadge(ctx, "stars", fmt.Sprintf("%d", ctx.Repo.Repository.NumStars), "blue")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLatestReleaseBadge(ctx *context_module.Context) {
|
|
|
|
release, err := repo_model.GetLatestReleaseByRepoID(ctx, ctx.Repo.Repository.ID)
|
|
|
|
if err != nil {
|
|
|
|
if repo_model.IsErrReleaseNotExist(err) {
|
|
|
|
errorBadge(ctx, "release", "Not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ctx.ServerError("GetLatestReleaseByRepoID", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := release.LoadAttributes(ctx); err != nil {
|
|
|
|
ctx.ServerError("LoadAttributes", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectToBadge(ctx, "release", release.TagName, "blue")
|
|
|
|
}
|