Skip to content

Provide actions badge svgs #27187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 15 additions & 0 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,21 @@ func GetRunByIndex(ctx context.Context, repoID, index int64) (*ActionRun, error)
return run, nil
}

func GetRepoBranchLastRun(ctx context.Context, repoID int64, branch, workflowFile string) (*ActionRun, error) {
var run ActionRun
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).
And("ref = ?", branch).
And("workflow_id = ?", workflowFile).
Desc("id").
Get(&run)
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 &run, nil
}

// UpdateRun updates a run.
// It requires the inputted run has Version set.
// It will return error if the version is not matched (it means the run has been changed after loaded).
Expand Down
20 changes: 20 additions & 0 deletions public/assets/img/svg/gitea_actions_failed.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions public/assets/img/svg/gitea_actions_pass.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions public/assets/img/svg/gitea_actions_pending.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 48 additions & 0 deletions routers/web/repo/actions/badge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package actions

import (
"errors"
"fmt"
"strings"

actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/modules/context"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/util"
)

func GetWorkflowBadge(ctx *context.Context) {
workflowFile := ctx.PathParamRaw("*")
if !strings.HasSuffix(workflowFile, "/badge.svg") {
ctx.NotFound("Not found", fmt.Errorf("%s not a badge request", ctx.Req.URL.Path))
return
}

workflowFile = strings.TrimSuffix(workflowFile, "/badge.svg")
run, err := actions_model.GetRepoBranchLastRun(ctx, ctx.Repo.Repository.ID,
git.RefNameFromBranch(ctx.Repo.Repository.DefaultBranch).String(),
workflowFile)
if err != nil {
if errors.Is(err, util.ErrNotExist) {
ctx.NotFound("Not found", fmt.Errorf("%s not found", workflowFile))
return
}
ctx.ServerError("GetWorkflowBadge", err)
return
}

switch run.Status {
case actions_model.StatusSuccess, actions_model.StatusSkipped:
ctx.Redirect(setting.AbsoluteAssetURL + "/assets/img/svg/gitea_actions_pass.svg")
case actions_model.StatusUnknown, actions_model.StatusFailure, actions_model.StatusCancelled:
ctx.Redirect(setting.AbsoluteAssetURL + "/assets/img/svg/gitea_actions_failed.svg")
case actions_model.StatusWaiting, actions_model.StatusRunning, actions_model.StatusBlocked:
ctx.Redirect(setting.AbsoluteAssetURL + "/assets/img/svg/gitea_actions_pending.svg")
default:
ctx.NotFound("Not found", fmt.Errorf("unknown status %d", run.Status))
}
}
3 changes: 3 additions & 0 deletions routers/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -1330,6 +1330,9 @@ func registerRoutes(m *web.Route) {
m.Get("/artifacts/{artifact_name}", actions.ArtifactsDownloadView)
m.Post("/rerun", reqRepoActionsWriter, actions.Rerun)
})
m.Group("/workflows", func() {
m.Get("/*", actions.GetWorkflowBadge)
})
}, reqRepoActionsReader, actions.MustEnableActions)

m.Group("/wiki", func() {
Expand Down