From 028102ff4771b5ae6c88b14ccbd446630a9a7274 Mon Sep 17 00:00:00 2001 From: a1012112796 <1012112796@qq.com> Date: Mon, 4 Dec 2023 02:19:49 +0000 Subject: [PATCH 1/5] show workflow names on other branchs also quick fix #28332, do it by a quick check in `ActionRun` table. TODO: looks need more jobs about workflow lables check in future. Signed-off-by: a1012112796 <1012112796@qq.com> --- models/actions/main_test.go | 1 + models/actions/run.go | 13 +++++++++++++ models/actions/run_test.go | 22 ++++++++++++++++++++++ routers/web/repo/actions/actions.go | 28 +++++++++++++++++++++++++++- templates/repo/actions/list.tmpl | 4 ++-- 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 models/actions/run_test.go diff --git a/models/actions/main_test.go b/models/actions/main_test.go index 5d5089e3bba88..0730c283a8bba 100644 --- a/models/actions/main_test.go +++ b/models/actions/main_test.go @@ -13,6 +13,7 @@ func TestMain(m *testing.M) { unittest.MainTest(m, &unittest.TestOptions{ FixtureFiles: []string{ "action_runner_token.yml", + "action_run.yml", }, }) } diff --git a/models/actions/run.go b/models/actions/run.go index 4656aa22a2933..a8699e7bfb39c 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -132,6 +132,19 @@ func (run *ActionRun) GetPushEventPayload() (*api.PushPayload, error) { return nil, fmt.Errorf("event %s is not a push event", run.Event) } +func FindWorkflowIDsByRepoID(ctx context.Context, repoID int64) ([]string, error) { + ids := make([]string, 0, 10) + + err := db.GetEngine(ctx).Table(new(ActionRun)).Where("repo_id = ?", repoID). + Select("workflow_id").Distinct("workflow_id").Find(&ids) + + if err != nil { + return nil, err + } + + return ids, nil +} + func (run *ActionRun) GetPullRequestEventPayload() (*api.PullRequestPayload, error) { if run.Event == webhook_module.HookEventPullRequest || run.Event == webhook_module.HookEventPullRequestSync { var payload api.PullRequestPayload diff --git a/models/actions/run_test.go b/models/actions/run_test.go new file mode 100644 index 0000000000000..98274f6e7e310 --- /dev/null +++ b/models/actions/run_test.go @@ -0,0 +1,22 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package actions + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestFindWorkflowIDsByRepoID(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + ids, err := FindWorkflowIDsByRepoID(db.DefaultContext, 4) + assert.NoError(t, err) + + assert.EqualValues(t, []string{"artifact.yaml"}, ids) +} diff --git a/routers/web/repo/actions/actions.go b/routers/web/repo/actions/actions.go index fd541647b716e..994a2a2c12a11 100644 --- a/routers/web/repo/actions/actions.go +++ b/routers/web/repo/actions/actions.go @@ -17,6 +17,7 @@ import ( "code.gitea.io/gitea/modules/container" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/git" + "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/routers/web/repo" "code.gitea.io/gitea/services/convert" @@ -32,6 +33,7 @@ const ( type Workflow struct { Entry git.TreeEntry ErrMsg string + Name string } // MustEnableActions check if actions are enabled in settings @@ -90,7 +92,7 @@ func List(ctx *context.Context) { workflows = make([]Workflow, 0, len(entries)) for _, entry := range entries { - workflow := Workflow{Entry: *entry} + workflow := Workflow{Entry: *entry, Name: entry.Name()} content, err := actions.GetContentFromEntry(entry) if err != nil { ctx.Error(http.StatusInternalServerError, err.Error()) @@ -124,6 +126,30 @@ func List(ctx *context.Context) { workflows = append(workflows, workflow) } } + + // recheck workflow ids by runs because not all workflows in default branch + ids, err := actions_model.FindWorkflowIDsByRepoID(ctx, ctx.Repo.Repository.ID) + if err != nil { + log.Error("actions_model.FindWorkflowIDsByRepoID: %v", err) + } + + for _, id := range ids { + found := false + + for _, wf := range workflows { + if wf.Name == id { + found = true + break + } + } + + if found { + continue + } + + workflows = append(workflows, Workflow{Name: id}) + } + ctx.Data["workflows"] = workflows ctx.Data["RepoLink"] = ctx.Repo.Repository.Link() diff --git a/templates/repo/actions/list.tmpl b/templates/repo/actions/list.tmpl index 62d30305b3603..ad43bc6836fb9 100644 --- a/templates/repo/actions/list.tmpl +++ b/templates/repo/actions/list.tmpl @@ -10,14 +10,14 @@