Skip to content

Commit

Permalink
show workflow names on other branchs also
Browse files Browse the repository at this point in the history
quick fix go-gitea#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>
  • Loading branch information
a1012112796 committed Dec 4, 2023
1 parent ec1feed commit ccd5b81
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
1 change: 1 addition & 0 deletions models/actions/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func TestMain(m *testing.M) {
unittest.MainTest(m, &unittest.TestOptions{
FixtureFiles: []string{
"action_runner_token.yml",
"action_run.yml",
},
})
}
13 changes: 13 additions & 0 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions models/actions/run_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
28 changes: 27 additions & 1 deletion routers/web/repo/actions/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -32,6 +33,7 @@ const (
type Workflow struct {
Entry git.TreeEntry
ErrMsg string
Name string
}

// MustEnableActions check if actions are enabled in settings
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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()

Expand Down
4 changes: 2 additions & 2 deletions templates/repo/actions/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
<div class="ui fluid vertical menu">
<a class="item{{if not $.CurWorkflow}} active{{end}}" href="{{$.Link}}?actor={{$.CurActor}}&status={{$.CurStatus}}">{{ctx.Locale.Tr "actions.runs.all_workflows"}}</a>
{{range .workflows}}
<a class="item{{if eq .Entry.Name $.CurWorkflow}} active{{end}}" href="{{$.Link}}?workflow={{.Entry.Name}}&actor={{$.CurActor}}&status={{$.CurStatus}}">{{.Entry.Name}}
<a class="item{{if eq .Name $.CurWorkflow}} active{{end}}" href="{{$.Link}}?workflow={{.Name}}&actor={{$.CurActor}}&status={{$.CurStatus}}">{{.Name}}
{{if .ErrMsg}}
<span data-tooltip-content="{{.ErrMsg}}">
{{svg "octicon-alert" 16 "text red"}}
</span>
{{end}}

{{if $.ActionsConfig.IsWorkflowDisabled .Entry.Name}}
{{if $.ActionsConfig.IsWorkflowDisabled .Name}}
<div class="ui red label">{{ctx.Locale.Tr "disabled"}}</div>
{{end}}
</a>
Expand Down

0 comments on commit ccd5b81

Please sign in to comment.