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..a1970d822ea43 100644 --- a/models/actions/run.go +++ b/models/actions/run.go @@ -132,6 +132,18 @@ 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 3b10f0b9571eb..e5449115bc857 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 ede4c82602da2..c1e703ed141c0 100644 --- a/templates/repo/actions/list.tmpl +++ b/templates/repo/actions/list.tmpl @@ -10,14 +10,14 @@