Skip to content

Commit 4cb0c64

Browse files
Add "View workflow file" to Actions list page (#34538)
This PR adds "View workflow file" to Actions list page, and replaces the redundant link. Related #34530 --------- Co-authored-by: wxiaoguang <[email protected]>
1 parent b0936f4 commit 4cb0c64

File tree

7 files changed

+47
-28
lines changed

7 files changed

+47
-28
lines changed

modules/actions/workflows.go

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,23 @@ func IsWorkflow(path string) bool {
4343
return strings.HasPrefix(path, ".gitea/workflows") || strings.HasPrefix(path, ".github/workflows")
4444
}
4545

46-
func ListWorkflows(commit *git.Commit) (git.Entries, error) {
47-
tree, err := commit.SubTree(".gitea/workflows")
46+
func ListWorkflows(commit *git.Commit) (string, git.Entries, error) {
47+
rpath := ".gitea/workflows"
48+
tree, err := commit.SubTree(rpath)
4849
if _, ok := err.(git.ErrNotExist); ok {
49-
tree, err = commit.SubTree(".github/workflows")
50+
rpath = ".github/workflows"
51+
tree, err = commit.SubTree(rpath)
5052
}
5153
if _, ok := err.(git.ErrNotExist); ok {
52-
return nil, nil
54+
return "", nil, nil
5355
}
5456
if err != nil {
55-
return nil, err
57+
return "", nil, err
5658
}
5759

5860
entries, err := tree.ListEntriesRecursiveFast()
5961
if err != nil {
60-
return nil, err
62+
return "", nil, err
6163
}
6264

6365
ret := make(git.Entries, 0, len(entries))
@@ -66,7 +68,7 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
6668
ret = append(ret, entry)
6769
}
6870
}
69-
return ret, nil
71+
return rpath, ret, nil
7072
}
7173

7274
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
@@ -102,7 +104,7 @@ func DetectWorkflows(
102104
payload api.Payloader,
103105
detectSchedule bool,
104106
) ([]*DetectedWorkflow, []*DetectedWorkflow, error) {
105-
entries, err := ListWorkflows(commit)
107+
_, entries, err := ListWorkflows(commit)
106108
if err != nil {
107109
return nil, nil, err
108110
}
@@ -147,7 +149,7 @@ func DetectWorkflows(
147149
}
148150

149151
func DetectScheduledWorkflows(gitRepo *git.Repository, commit *git.Commit) ([]*DetectedWorkflow, error) {
150-
entries, err := ListWorkflows(commit)
152+
_, entries, err := ListWorkflows(commit)
151153
if err != nil {
152154
return nil, err
153155
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3815,6 +3815,7 @@ runs.expire_log_message = Logs have been purged because they were too old.
38153815
runs.delete = Delete workflow run
38163816
runs.delete.description = Are you sure you want to permanently delete this workflow run? This action cannot be undone.
38173817
runs.not_done = This workflow run is not done.
3818+
runs.view_workflow_file = View workflow file
38183819

38193820
workflow.disable = Disable Workflow
38203821
workflow.disable_success = Workflow '%s' disabled successfully.

routers/web/repo/actions/actions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func prepareWorkflowDispatchTemplate(ctx *context.Context, commit *git.Commit) (
126126

127127
var curWorkflow *model.Workflow
128128

129-
entries, err := actions.ListWorkflows(commit)
129+
_, entries, err := actions.ListWorkflows(commit)
130130
if err != nil {
131131
ctx.ServerError("ListWorkflows", err)
132132
return nil

routers/web/repo/actions/view.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,36 @@ func View(ctx *context_module.Context) {
6464
ctx.HTML(http.StatusOK, tplViewActions)
6565
}
6666

67+
func ViewWorkflowFile(ctx *context_module.Context) {
68+
runIndex := getRunIndex(ctx)
69+
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
70+
if err != nil {
71+
ctx.NotFoundOrServerError("GetRunByIndex", func(err error) bool {
72+
return errors.Is(err, util.ErrNotExist)
73+
}, err)
74+
return
75+
}
76+
commit, err := ctx.Repo.GitRepo.GetCommit(run.CommitSHA)
77+
if err != nil {
78+
ctx.NotFoundOrServerError("GetCommit", func(err error) bool {
79+
return errors.Is(err, util.ErrNotExist)
80+
}, err)
81+
return
82+
}
83+
rpath, entries, err := actions.ListWorkflows(commit)
84+
if err != nil {
85+
ctx.ServerError("ListWorkflows", err)
86+
return
87+
}
88+
for _, entry := range entries {
89+
if entry.Name() == run.WorkflowID {
90+
ctx.Redirect(fmt.Sprintf("%s/src/commit/%s/%s/%s", ctx.Repo.RepoLink, url.PathEscape(run.CommitSHA), util.PathEscapeSegments(rpath), util.PathEscapeSegments(run.WorkflowID)))
91+
return
92+
}
93+
}
94+
ctx.NotFound(nil)
95+
}
96+
6797
type LogCursor struct {
6898
Step int `json:"step"`
6999
Cursor int64 `json:"cursor"`

routers/web/web.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ func registerWebRoutes(m *web.Router) {
14451445
m.Post("/rerun", reqRepoActionsWriter, actions.Rerun)
14461446
m.Get("/logs", actions.Logs)
14471447
})
1448+
m.Get("/workflow", actions.ViewWorkflowFile)
14481449
m.Post("/cancel", reqRepoActionsWriter, actions.Cancel)
14491450
m.Post("/approve", reqRepoActionsWriter, actions.Approve)
14501451
m.Post("/delete", reqRepoActionsWriter, actions.Delete)

services/actions/workflow.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ import (
3131
"github.com/nektos/act/pkg/model"
3232
)
3333

34-
func getActionWorkflowPath(commit *git.Commit) string {
35-
paths := []string{".gitea/workflows", ".github/workflows"}
36-
for _, treePath := range paths {
37-
if _, err := commit.SubTree(treePath); err == nil {
38-
return treePath
39-
}
40-
}
41-
return ""
42-
}
43-
4434
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, folder string, entry *git.TreeEntry) *api.ActionWorkflow {
4535
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
4636
cfg := cfgUnit.ActionsConfig()
@@ -109,14 +99,12 @@ func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error)
10999
return nil, err
110100
}
111101

112-
entries, err := actions.ListWorkflows(defaultBranchCommit)
102+
folder, entries, err := actions.ListWorkflows(defaultBranchCommit)
113103
if err != nil {
114104
ctx.APIError(http.StatusNotFound, err.Error())
115105
return nil, err
116106
}
117107

118-
folder := getActionWorkflowPath(defaultBranchCommit)
119-
120108
workflows := make([]*api.ActionWorkflow, len(entries))
121109
for i, entry := range entries {
122110
workflows[i] = getActionWorkflowEntry(ctx, defaultBranchCommit, folder, entry)
@@ -185,7 +173,7 @@ func DispatchActionWorkflow(ctx reqctx.RequestContext, doer *user_model.User, re
185173
}
186174

187175
// get workflow entry from runTargetCommit
188-
entries, err := actions.ListWorkflows(runTargetCommit)
176+
_, entries, err := actions.ListWorkflows(runTargetCommit)
189177
if err != nil {
190178
return err
191179
}

templates/repo/actions/runs_list.tmpl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@
3939
<div class="ui dropdown jump tw-p-2">
4040
{{svg "octicon-kebab-horizontal"}}
4141
<div class="menu flex-items-menu">
42-
<!-- TODO: This redundant link should be replaced by something else in future,
43-
because have not figured out how to add "View Workflow" or anything similar to GitHub.
44-
Related: https://github.com/go-gitea/gitea/pull/34530 -->
45-
<a class="item" href="{{$run.Link}}">{{svg "octicon-play"}}{{ctx.Locale.Tr "view"}}</a>
42+
<a class="item" href="{{$run.Link}}/workflow">{{svg "octicon-play"}}{{ctx.Locale.Tr "actions.runs.view_workflow_file"}}</a>
4643
{{if and $.AllowDeleteWorkflowRuns $run.Status.IsDone}}
4744
<a class="item link-action"
4845
data-url="{{$run.Link}}/delete"

0 commit comments

Comments
 (0)