Skip to content

Commit c18e6b9

Browse files
Refactor link logic to live in workflows module
1 parent b1eadac commit c18e6b9

File tree

3 files changed

+40
-37
lines changed

3 files changed

+40
-37
lines changed

models/actions/run.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ func (run *ActionRun) Link() string {
7474
return fmt.Sprintf("%s/actions/runs/%d", run.Repo.Link(), run.Index)
7575
}
7676

77+
// WorkflowLink return the url to the actions list filtered for this runs workflow
7778
func (run *ActionRun) WorkflowLink() string {
7879
if run.Repo == nil {
7980
return ""

modules/actions/workflows.go

+38
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ package actions
55

66
import (
77
"bytes"
8+
"fmt"
89
"io"
910
"strings"
1011

12+
actions_model "code.gitea.io/gitea/models/actions"
1113
"code.gitea.io/gitea/modules/git"
1214
"code.gitea.io/gitea/modules/log"
1315
api "code.gitea.io/gitea/modules/structs"
@@ -69,6 +71,42 @@ func ListWorkflows(commit *git.Commit) (git.Entries, error) {
6971
return ret, nil
7072
}
7173

74+
// GetRunsWorkflowFileLink return url for the source of the workflow file for an ActionRun
75+
func GetRunsWorkflowFileLink(run *actions_model.ActionRun, gitRepo *git.Repository) string {
76+
if run.Repo == nil || run.CommitSHA == "" {
77+
return ""
78+
}
79+
80+
commit, err := gitRepo.GetCommit(run.CommitSHA)
81+
if err != nil {
82+
return ""
83+
}
84+
85+
entries, err := ListWorkflows(commit)
86+
if err != nil {
87+
return ""
88+
}
89+
90+
var workflowEntry *git.TreeEntry
91+
for _, entry := range entries {
92+
if entry.Name() == run.WorkflowID {
93+
workflowEntry = entry
94+
break
95+
}
96+
}
97+
98+
if workflowEntry == nil {
99+
return ""
100+
}
101+
102+
workflowFilePath := workflowEntry.GetPathInRepo()
103+
if workflowFilePath == "" {
104+
return ""
105+
}
106+
107+
return fmt.Sprintf("%s/src/commit/%s/%s", run.Repo.Link(), run.CommitSHA, workflowFilePath)
108+
}
109+
72110
func GetContentFromEntry(entry *git.TreeEntry) ([]byte, error) {
73111
f, err := entry.Blob().DataAsync()
74112
if err != nil {

routers/web/repo/actions/view.go

+1-37
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func ViewPost(ctx *context_module.Context) {
171171
resp.State.Run.CanRerun = run.Status.IsDone() && ctx.Repo.CanWrite(unit.TypeActions)
172172
resp.State.Run.CanDeleteArtifact = run.Status.IsDone() && ctx.Repo.CanWrite(unit.TypeActions)
173173
resp.State.Run.Done = run.Status.IsDone()
174-
resp.State.Run.WorkflowFileLink = getWorkflowFileLink(ctx, run)
174+
resp.State.Run.WorkflowFileLink = actions.GetRunsWorkflowFileLink(run, ctx.Repo.GitRepo)
175175
resp.State.Run.WorkflowID = run.WorkflowID
176176
resp.State.Run.WorkflowLink = run.WorkflowLink()
177177
resp.State.Run.IsSchedule = run.IsSchedule()
@@ -306,42 +306,6 @@ func ViewPost(ctx *context_module.Context) {
306306
ctx.JSON(http.StatusOK, resp)
307307
}
308308

309-
// getWorkflowFileLink return url for the source of the workflow file that was run
310-
func getWorkflowFileLink(ctx *context_module.Context, run *actions_model.ActionRun) string {
311-
if run.Repo == nil || run.CommitSHA == "" {
312-
return ""
313-
}
314-
315-
commit, err := ctx.Repo.GitRepo.GetCommit(run.CommitSHA)
316-
if err != nil {
317-
return ""
318-
}
319-
320-
entries, err := actions.ListWorkflows(commit)
321-
if err != nil {
322-
return ""
323-
}
324-
325-
var workflowEntry *git.TreeEntry
326-
for _, entry := range entries {
327-
if entry.Name() == run.WorkflowID {
328-
workflowEntry = entry
329-
break
330-
}
331-
}
332-
333-
if workflowEntry == nil {
334-
return ""
335-
}
336-
337-
workflowFilePath := workflowEntry.GetPathInRepo()
338-
if workflowFilePath == "" {
339-
return ""
340-
}
341-
342-
return fmt.Sprintf("%s/src/commit/%s/%s", run.Repo.Link(), run.CommitSHA, workflowFilePath)
343-
}
344-
345309
// Rerun will rerun jobs in the given run
346310
// If jobIndexStr is a blank string, it means rerun all jobs
347311
func Rerun(ctx *context_module.Context) {

0 commit comments

Comments
 (0)