adding more fields to the workflow list output

Signed-off-by: Bence Santha <git@santha.eu>
This commit is contained in:
Bence Santha 2024-09-20 14:10:50 +02:00
parent b525269c8d
commit 6f1b19976f

View File

@ -6,6 +6,7 @@ package actions
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"os"
"strconv" "strconv"
"strings" "strings"
@ -25,11 +26,28 @@ import (
"github.com/nektos/act/pkg/model" "github.com/nektos/act/pkg/model"
) )
func getActionWorkflowEntry(ctx *context.APIContext, entry *git.TreeEntry, commit *git.Commit) (*api.ActionWorkflow, error) { func getActionWorkflowPath(commit *git.Commit) string {
_, err := commit.SubTree(".gitea/workflows")
if err == nil {
return ".gitea/workflows"
}
if _, ok := err.(git.ErrNotExist); ok {
_, err = commit.SubTree(".github/workflows")
return ".github/workflows"
}
return ""
}
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *git.TreeEntry) (*api.ActionWorkflow, error) {
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions) cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
cfg := cfgUnit.ActionsConfig() cfg := cfgUnit.ActionsConfig()
defaultBranch, _ := commit.GetBranchName()
URL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), entry.Name()) URL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), entry.Name())
HTMLURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), defaultBranch, getActionWorkflowPath(commit), entry.Name())
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), entry.Name(), ctx.Repo.Repository.DefaultBranch) badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), entry.Name(), ctx.Repo.Repository.DefaultBranch)
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow // See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
@ -44,18 +62,31 @@ func getActionWorkflowEntry(ctx *context.APIContext, entry *git.TreeEntry, commi
state = "disabled_manually" state = "disabled_manually"
} }
// TODO: NodeID // Currently, the NodeID returns the hostname of the server since, as far as I know, Gitea does not have a parameter
// TODO: CreatedAt // similar to an instance ID.
// TODO: UpdatedAt hostname, err := os.Hostname()
// TODO: HTMLURL if err != nil {
// TODO: DeletedAt hostname = "unknown"
}
// The CreatedAt and UpdatedAt fields currently reflect the timestamp of the latest commit, which can later be refined
// by retrieving the first and last commits for the file history. The first commit would indicate the creation date,
// while the last commit would represent the modification date. The DeletedAt could be determined by identifying
// the last commit where the file existed. However, this implementation has not been done here yet, as it would likely
// cause a significant performance degradation.
createdAt := commit.Author.When
updatedAt := commit.Author.When
return &api.ActionWorkflow{ return &api.ActionWorkflow{
ID: entry.Name(), ID: entry.Name(),
NodeID: hostname,
Name: entry.Name(), Name: entry.Name(),
Path: entry.Name(), Path: entry.Name(),
State: state, State: state,
CreatedAt: createdAt,
UpdatedAt: updatedAt,
URL: URL, URL: URL,
HTMLURL: HTMLURL,
BadgeURL: badgeURL, BadgeURL: badgeURL,
}, nil }, nil
} }
@ -88,7 +119,7 @@ func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error)
workflows := make([]*api.ActionWorkflow, len(entries)) workflows := make([]*api.ActionWorkflow, len(entries))
for i, entry := range entries { for i, entry := range entries {
workflows[i], err = getActionWorkflowEntry(ctx, entry, defaultBranchCommit) workflows[i], err = getActionWorkflowEntry(ctx, defaultBranchCommit, entry)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, "WorkflowGetError", err.Error()) ctx.Error(http.StatusInternalServerError, "WorkflowGetError", err.Error())
return nil, err return nil, err