Skip to content

Commit cfdd251

Browse files
committed
fix list endpoint response / add TestWorkflowApi
* uses all endpoints added here * checks if disabled workflows cannot dispatch * remove node_id * fix path field * test that the URL field of the workflow result is usable * additional test for non default branch workflow * return 200 when no workflows found, with an empty list
1 parent 577ff3f commit cfdd251

File tree

4 files changed

+316
-26
lines changed

4 files changed

+316
-26
lines changed

modules/structs/repo_actions.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,10 @@ type CreateActionWorkflowDispatch struct {
4545

4646
// ActionWorkflow represents a ActionWorkflow
4747
type ActionWorkflow struct {
48-
ID string `json:"id"`
49-
NodeID string `json:"node_id"`
50-
Name string `json:"name"`
51-
Path string `json:"path"`
52-
State string `json:"state"`
48+
ID string `json:"id"`
49+
Name string `json:"name"`
50+
Path string `json:"path"`
51+
State string `json:"state"`
5352
// swagger:strfmt date-time
5453
CreatedAt time.Time `json:"created_at"`
5554
// swagger:strfmt date-time
@@ -58,7 +57,7 @@ type ActionWorkflow struct {
5857
HTMLURL string `json:"html_url"`
5958
BadgeURL string `json:"badge_url"`
6059
// swagger:strfmt date-time
61-
DeletedAt time.Time `json:"deleted_at"`
60+
DeletedAt time.Time `json:"deleted_at,omitempty"`
6261
}
6362

6463
// ActionWorkflowResponse returns a ActionWorkflow

routers/api/v1/repo/action.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -630,13 +630,7 @@ func (a ActionWorkflow) ListRepositoryWorkflows(ctx *context.APIContext) {
630630
return
631631
}
632632

633-
if len(workflows) == 0 {
634-
ctx.Error(http.StatusNotFound, "ListActionWorkflows", err)
635-
return
636-
}
637-
638-
ctx.SetTotalCountHeader(int64(len(workflows)))
639-
ctx.JSON(http.StatusOK, workflows)
633+
ctx.JSON(http.StatusOK, &api.ActionWorkflowResponse{Workflows: workflows, TotalCount: int64(len(workflows))})
640634
}
641635

642636
func (a ActionWorkflow) GetWorkflow(ctx *context.APIContext) {

services/actions/workflow.go

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

1212
actions_model "code.gitea.io/gitea/models/actions"
@@ -53,14 +53,14 @@ func getActionWorkflowPath(commit *git.Commit) string {
5353
return ""
5454
}
5555

56-
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *git.TreeEntry) *api.ActionWorkflow {
56+
func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, folder string, entry *git.TreeEntry) *api.ActionWorkflow {
5757
cfgUnit := ctx.Repo.Repository.MustGetUnit(ctx, unit.TypeActions)
5858
cfg := cfgUnit.ActionsConfig()
5959

6060
defaultBranch, _ := commit.GetBranchName()
6161

6262
URL := fmt.Sprintf("%s/actions/workflows/%s", ctx.Repo.Repository.APIURL(), entry.Name())
63-
HTMLURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), defaultBranch, getActionWorkflowPath(commit), entry.Name())
63+
HTMLURL := fmt.Sprintf("%s/src/branch/%s/%s/%s", ctx.Repo.Repository.HTMLURL(ctx), defaultBranch, folder, entry.Name())
6464
badgeURL := fmt.Sprintf("%s/actions/workflows/%s/badge.svg?branch=%s", ctx.Repo.Repository.HTMLURL(ctx), entry.Name(), ctx.Repo.Repository.DefaultBranch)
6565

6666
// See https://docs.github.com/en/rest/actions/workflows?apiVersion=2022-11-28#get-a-workflow
@@ -75,13 +75,6 @@ func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *
7575
state = "disabled_manually"
7676
}
7777

78-
// Currently, the NodeID returns the hostname of the server since, as far as I know, Gitea does not have a parameter
79-
// similar to an instance ID.
80-
hostname, err := os.Hostname()
81-
if err != nil {
82-
hostname = "unknown"
83-
}
84-
8578
// The CreatedAt and UpdatedAt fields currently reflect the timestamp of the latest commit, which can later be refined
8679
// by retrieving the first and last commits for the file history. The first commit would indicate the creation date,
8780
// while the last commit would represent the modification date. The DeletedAt could be determined by identifying
@@ -92,9 +85,8 @@ func getActionWorkflowEntry(ctx *context.APIContext, commit *git.Commit, entry *
9285

9386
return &api.ActionWorkflow{
9487
ID: entry.Name(),
95-
NodeID: hostname,
9688
Name: entry.Name(),
97-
Path: entry.Name(),
89+
Path: path.Join(folder, entry.Name()),
9890
State: state,
9991
CreatedAt: createdAt,
10092
UpdatedAt: updatedAt,
@@ -135,9 +127,11 @@ func ListActionWorkflows(ctx *context.APIContext) ([]*api.ActionWorkflow, error)
135127
return nil, err
136128
}
137129

130+
folder := getActionWorkflowPath(defaultBranchCommit)
131+
138132
workflows := make([]*api.ActionWorkflow, len(entries))
139133
for i, entry := range entries {
140-
workflows[i] = getActionWorkflowEntry(ctx, defaultBranchCommit, entry)
134+
workflows[i] = getActionWorkflowEntry(ctx, defaultBranchCommit, folder, entry)
141135
}
142136

143137
return workflows, nil

0 commit comments

Comments
 (0)