badges: Relax the default workflow badge conditions
Previously, if no branch was explicitly specified for a workflow, it defaulted to the default branch of the repo. This worked fine for workflows that were triggered on push, but it prevented showing badges for workflows that only run on tags, or on schedule - since they do not run on a specific branch. Thus, relax the conditions, and if no branch is specified, just return the latest run of the given workflow. If one is specified, *then* restrict it to said branch. Fixes #3487. Signed-off-by: Gergely Nagy <forgejo@gergo.csillger.hu> (cherry picked from commit d6915f4d5f81805f3a9e2ba38ecf512c15cfd360)
This commit is contained in:
parent
487b2129aa
commit
ef4c6abbb9
4 changed files with 35 additions and 7 deletions
|
@ -326,15 +326,18 @@ func GetLatestRun(ctx context.Context, repoID int64) (*ActionRun, error) {
|
||||||
|
|
||||||
func GetLatestRunForBranchAndWorkflow(ctx context.Context, repoID int64, branch, workflowFile, event string) (*ActionRun, error) {
|
func GetLatestRunForBranchAndWorkflow(ctx context.Context, repoID int64, branch, workflowFile, event string) (*ActionRun, error) {
|
||||||
var run ActionRun
|
var run ActionRun
|
||||||
q := db.GetEngine(ctx).Where("repo_id=?", repoID).And("ref=?", branch).And("workflow_id=?", workflowFile)
|
q := db.GetEngine(ctx).Where("repo_id=?", repoID).And("workflow_id=?", workflowFile)
|
||||||
if event != "" {
|
if event != "" {
|
||||||
q = q.And("event=?", event)
|
q = q.And("event=?", event)
|
||||||
}
|
}
|
||||||
|
if branch != "" {
|
||||||
|
q = q.And("ref=?", branch)
|
||||||
|
}
|
||||||
has, err := q.Desc("id").Get(&run)
|
has, err := q.Desc("id").Get(&run)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
return nil, util.NewNotExistErrorf("run with repo_id %d, ref %s, workflow_id %s", repoID, branch, workflowFile)
|
return nil, util.NewNotExistErrorf("run with repo_id %d, ref %s, event %s, workflow_id %s", repoID, branch, event, workflowFile)
|
||||||
}
|
}
|
||||||
return &run, nil
|
return &run, nil
|
||||||
}
|
}
|
||||||
|
|
1
release-notes/8.0.0/fix/3843.md
Normal file
1
release-notes/8.0.0/fix/3843.md
Normal file
|
@ -0,0 +1 @@
|
||||||
|
- Fixed a bug that resulted in workflow badges not working for workflows that weren't running on push (such as scheduled workflows, and ones that run on tags and for prs)
|
|
@ -45,10 +45,9 @@ func errorBadge(ctx *context_module.Context, label, text string) {
|
||||||
|
|
||||||
func GetWorkflowBadge(ctx *context_module.Context) {
|
func GetWorkflowBadge(ctx *context_module.Context) {
|
||||||
branch := ctx.Req.URL.Query().Get("branch")
|
branch := ctx.Req.URL.Query().Get("branch")
|
||||||
if branch == "" {
|
if branch != "" {
|
||||||
branch = ctx.Repo.Repository.DefaultBranch
|
|
||||||
}
|
|
||||||
branch = fmt.Sprintf("refs/heads/%s", branch)
|
branch = fmt.Sprintf("refs/heads/%s", branch)
|
||||||
|
}
|
||||||
event := ctx.Req.URL.Query().Get("event")
|
event := ctx.Req.URL.Query().Get("event")
|
||||||
|
|
||||||
workflowFile := ctx.Params("workflow_name")
|
workflowFile := ctx.Params("workflow_name")
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||||
|
// Copyright 2024 The Forgejo Authors c/o Codeberg e.V.. All rights reserved.
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
package integration
|
package integration
|
||||||
|
@ -40,12 +41,17 @@ func TestBadges(t *testing.T) {
|
||||||
{
|
{
|
||||||
Operation: "create",
|
Operation: "create",
|
||||||
TreePath: ".gitea/workflows/pr.yml",
|
TreePath: ".gitea/workflows/pr.yml",
|
||||||
ContentReader: strings.NewReader("name: test\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
ContentReader: strings.NewReader("name: pr\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Operation: "create",
|
Operation: "create",
|
||||||
TreePath: ".gitea/workflows/self-test.yaml",
|
TreePath: ".gitea/workflows/self-test.yaml",
|
||||||
ContentReader: strings.NewReader("name: test\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
ContentReader: strings.NewReader("name: self-test\non:\n push:\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Operation: "create",
|
||||||
|
TreePath: ".gitea/workflows/tag-test.yaml",
|
||||||
|
ContentReader: strings.NewReader("name: tags\non:\n push:\n tags: '*'\njobs:\n test:\n runs-on: ubuntu-latest\n steps:\n - run: echo helloworld\n"),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
@ -112,6 +118,25 @@ func TestBadges(t *testing.T) {
|
||||||
req = NewRequestf(t, "GET", "/user2/%s/actions/workflows/pr.yml/badge.svg?event=cron", repo.Name)
|
req = NewRequestf(t, "GET", "/user2/%s/actions/workflows/pr.yml/badge.svg?event=cron", repo.Name)
|
||||||
resp = MakeRequest(t, req, http.StatusSeeOther)
|
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||||
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
assertBadge(t, resp, "pr.yml-Not%20found-crimson")
|
||||||
|
|
||||||
|
t.Run("tagged", func(t *testing.T) {
|
||||||
|
defer tests.PrintCurrentTest(t)()
|
||||||
|
|
||||||
|
// With no tags, the workflow has no runs, and isn't found
|
||||||
|
req := NewRequestf(t, "GET", "/user2/%s/actions/workflows/tag-test.yaml/badge.svg", repo.Name)
|
||||||
|
resp := MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
assertBadge(t, resp, "tag--test.yaml-Not%20found-crimson")
|
||||||
|
|
||||||
|
// Lets create a tag!
|
||||||
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
||||||
|
err := release.CreateNewTag(git.DefaultContext, owner, repo, "main", "v1", "message")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
// Now the workflow is wating
|
||||||
|
req = NewRequestf(t, "GET", "/user2/%s/actions/workflows/tag-test.yaml/badge.svg", repo.Name)
|
||||||
|
resp = MakeRequest(t, req, http.StatusSeeOther)
|
||||||
|
assertBadge(t, resp, "tag--test.yaml-waiting-lightgrey")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
t.Run("Stars", func(t *testing.T) {
|
t.Run("Stars", func(t *testing.T) {
|
||||||
|
|
Loading…
Reference in a new issue