Skip to content

Commit 6f1b199

Browse files
committed
adding more fields to the workflow list output
Signed-off-by: Bence Santha <[email protected]>
1 parent b525269 commit 6f1b199

File tree

1 file changed

+44
-13
lines changed

1 file changed

+44
-13
lines changed

services/actions/workflow.go

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package actions
66
import (
77
"fmt"
88
"net/http"
9+
"os"
910
"strconv"
1011
"strings"
1112

@@ -25,11 +26,28 @@ import (
2526
"github.com/nektos/act/pkg/model"
2627
)
2728

28-
func getActionWorkflowEntry(ctx *context.APIContext, entry *git.TreeEntry, commit *git.Commit) (*api.ActionWorkflow, error) {
29+
func getActionWorkflowPath(commit *git.Commit) string {
30+
_, err := commit.SubTree(".gitea/workflows")
31+
if err == nil {
32+
return ".gitea/workflows"
33+
}
34+
35+
if _, ok := err.(git.ErrNotExist); ok {
36+
_, err = commit.SubTree(".github/workflows")
37+
return ".github/workflows"
38+
}
39+
40+
return ""
41+
}
42+
43+
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *git.TreeEntry) (*api.ActionWorkflow, error) {
2944
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
3045
cfg := cfgUnit.ActionsConfig()
3146

47+
defaultBranch, _ := commit.GetBranchName()
48+
3249
URL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), entry.Name())
50+
HTMLURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), defaultBranch, getActionWorkflowPath(commit), entry.Name())
3351
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), entry.Name(), ctx.Repo.Repository.DefaultBranch)
3452

3553
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
@@ -44,19 +62,32 @@ func getActionWorkflowEntry(ctx *context.APIContext, entry *git.TreeEntry, commi
4462
state = "disabled_manually"
4563
}
4664

47-
// TODO: NodeID
48-
// TODO: CreatedAt
49-
// TODO: UpdatedAt
50-
// TODO: HTMLURL
51-
// TODO: DeletedAt
65+
// Currently, the NodeID returns the hostname of the server since, as far as I know, Gitea does not have a parameter
66+
// similar to an instance ID.
67+
hostname, err := os.Hostname()
68+
if err != nil {
69+
hostname = "unknown"
70+
}
71+
72+
// The CreatedAt and UpdatedAt fields currently reflect the timestamp of the latest commit, which can later be refined
73+
// by retrieving the first and last commits for the file history. The first commit would indicate the creation date,
74+
// while the last commit would represent the modification date. The DeletedAt could be determined by identifying
75+
// the last commit where the file existed. However, this implementation has not been done here yet, as it would likely
76+
// cause a significant performance degradation.
77+
createdAt := commit.Author.When
78+
updatedAt := commit.Author.When
5279

5380
return &api.ActionWorkflow{
54-
ID: entry.Name(),
55-
Name: entry.Name(),
56-
Path: entry.Name(),
57-
State: state,
58-
URL: URL,
59-
BadgeURL: badgeURL,
81+
ID: entry.Name(),
82+
NodeID: hostname,
83+
Name: entry.Name(),
84+
Path: entry.Name(),
85+
State: state,
86+
CreatedAt: createdAt,
87+
UpdatedAt: updatedAt,
88+
URL: URL,
89+
HTMLURL: HTMLURL,
90+
BadgeURL: badgeURL,
6091
}, nil
6192
}
6293

@@ -88,7 +119,7 @@ func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error)
88119

89120
workflows := make([]*api.ActionWorkflow, len(entries))
90121
for i, entry := range entries {
91-
workflows[i], err = getActionWorkflowEntry(ctx, entry, defaultBranchCommit)
122+
workflows[i], err = getActionWorkflowEntry(ctx, defaultBranchCommit, entry)
92123
if err != nil {
93124
ctx.Error(http.StatusInternalServerError, "WorkflowGetError", err.Error())
94125
return nil, err

0 commit comments

Comments
 (0)