Skip to content

Commit 48379a2

Browse files
committed
improve tests and fix bugs
1 parent 27c9c27 commit 48379a2

File tree

11 files changed

+548
-28
lines changed

11 files changed

+548
-28
lines changed

models/actions/run_job_list.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,19 +80,19 @@ type FindRunJobOptions struct {
8080
func (opts FindRunJobOptions) ToConds() builder.Cond {
8181
cond := builder.NewCond()
8282
if opts.RunID > 0 {
83-
cond = cond.And(builder.Eq{"run_id": opts.RunID})
83+
cond = cond.And(builder.Eq{"`action_run_job`.run_id": opts.RunID})
8484
}
8585
if opts.RepoID > 0 {
86-
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
86+
cond = cond.And(builder.Eq{"`action_run_job`.repo_id": opts.RepoID})
8787
}
8888
if opts.CommitSHA != "" {
89-
cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA})
89+
cond = cond.And(builder.Eq{"`action_run_job`.commit_sha": opts.CommitSHA})
9090
}
9191
if len(opts.Statuses) > 0 {
92-
cond = cond.And(builder.In("status", opts.Statuses))
92+
cond = cond.And(builder.In("`action_run_job`.status", opts.Statuses))
9393
}
9494
if opts.UpdatedBefore > 0 {
95-
cond = cond.And(builder.Lt{"updated": opts.UpdatedBefore})
95+
cond = cond.And(builder.Lt{"`action_run_job`.updated": opts.UpdatedBefore})
9696
}
9797
return cond
9898
}

models/actions/run_list.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,25 @@ type FindRunOptions struct {
7777
func (opts FindRunOptions) ToConds() builder.Cond {
7878
cond := builder.NewCond()
7979
if opts.RepoID > 0 {
80-
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
80+
cond = cond.And(builder.Eq{"`action_run`.repo_id": opts.RepoID})
8181
}
8282
if opts.WorkflowID != "" {
83-
cond = cond.And(builder.Eq{"workflow_id": opts.WorkflowID})
83+
cond = cond.And(builder.Eq{"`action_run`.workflow_id": opts.WorkflowID})
8484
}
8585
if opts.TriggerUserID > 0 {
86-
cond = cond.And(builder.Eq{"trigger_user_id": opts.TriggerUserID})
86+
cond = cond.And(builder.Eq{"`action_run`.trigger_user_id": opts.TriggerUserID})
8787
}
8888
if opts.Approved {
89-
cond = cond.And(builder.Gt{"approved_by": 0})
89+
cond = cond.And(builder.Gt{"`action_run`.approved_by": 0})
9090
}
9191
if len(opts.Status) > 0 {
92-
cond = cond.And(builder.In("status", opts.Status))
92+
cond = cond.And(builder.In("`action_run`.status", opts.Status))
9393
}
9494
if opts.Ref != "" {
95-
cond = cond.And(builder.Eq{"ref": opts.Ref})
95+
cond = cond.And(builder.Eq{"`action_run`.ref": opts.Ref})
9696
}
9797
if opts.TriggerEvent != "" {
98-
cond = cond.And(builder.Eq{"trigger_event": opts.TriggerEvent})
98+
cond = cond.And(builder.Eq{"`action_run`.trigger_event": opts.TriggerEvent})
9999
}
100100
return cond
101101
}

models/fixtures/action_run.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
ref: "refs/heads/master"
1010
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
1111
event: "push"
12+
trigger_event: "push"
1213
is_fork_pull_request: 0
1314
status: 1
1415
started: 1683636528
@@ -28,6 +29,7 @@
2829
ref: "refs/heads/master"
2930
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
3031
event: "push"
32+
trigger_event: "push"
3133
is_fork_pull_request: 0
3234
status: 1
3335
started: 1683636528
@@ -47,6 +49,7 @@
4749
ref: "refs/heads/master"
4850
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
4951
event: "push"
52+
trigger_event: "push"
5053
is_fork_pull_request: 0
5154
status: 1
5255
started: 1683636528
@@ -66,6 +69,7 @@
6669
ref: "refs/heads/test"
6770
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
6871
event: "push"
72+
trigger_event: "push"
6973
is_fork_pull_request: 0
7074
status: 1
7175
started: 1683636528
@@ -85,6 +89,7 @@
8589
ref: "refs/heads/test"
8690
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
8791
event: "push"
92+
trigger_event: "push"
8893
is_fork_pull_request: 0
8994
status: 1
9095
started: 1683636528
@@ -99,11 +104,12 @@
99104
repo_id: 2
100105
owner_id: 0
101106
workflow_id: "test.yaml"
102-
index: 191
107+
index: 192
103108
trigger_user_id: 1
104109
ref: "refs/heads/test"
105110
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
106111
event: "push"
112+
trigger_event: "push"
107113
is_fork_pull_request: 0
108114
status: 1
109115
started: 1683636528

routers/api/v1/admin/runners.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ func ListWorkflowJobs(ctx *context.APIContext) {
110110
// summary: Lists all jobs
111111
// produces:
112112
// - application/json
113+
// parameters:
114+
// - name: status
115+
// in: query
116+
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
117+
// type: string
118+
// required: false
119+
// - name: page
120+
// in: query
121+
// description: page number of results to return (1-based)
122+
// type: integer
123+
// - name: limit
124+
// in: query
125+
// description: page size of results
126+
// type: integer
113127
// responses:
114128
// "200":
115129
// "$ref": "#/responses/WorkflowJobsList"
@@ -128,6 +142,35 @@ func ListWorkflowRuns(ctx *context.APIContext) {
128142
// summary: Lists all runs
129143
// produces:
130144
// - application/json
145+
// parameters:
146+
// - name: event
147+
// in: query
148+
// description: workflow event name
149+
// type: string
150+
// required: false
151+
// - name: branch
152+
// in: query
153+
// description: workflow branch
154+
// type: string
155+
// required: false
156+
// - name: status
157+
// in: query
158+
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
159+
// type: string
160+
// required: false
161+
// - name: actor
162+
// in: query
163+
// description: triggered by user
164+
// type: string
165+
// required: false
166+
// - name: page
167+
// in: query
168+
// description: page number of results to return (1-based)
169+
// type: integer
170+
// - name: limit
171+
// in: query
172+
// description: page size of results
173+
// type: integer
131174
// responses:
132175
// "200":
133176
// "$ref": "#/responses/WorkflowRunsList"

routers/api/v1/org/action.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,19 @@ func (Action) ListWorkflowJobs(ctx *context.APIContext) {
582582
// description: name of the organization
583583
// type: string
584584
// required: true
585+
// - name: status
586+
// in: query
587+
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
588+
// type: string
589+
// required: false
590+
// - name: page
591+
// in: query
592+
// description: page number of results to return (1-based)
593+
// type: integer
594+
// - name: limit
595+
// in: query
596+
// description: page size of results
597+
// type: integer
585598
// responses:
586599
// "200":
587600
// "$ref": "#/responses/WorkflowJobsList"
@@ -604,6 +617,34 @@ func (Action) ListWorkflowRuns(ctx *context.APIContext) {
604617
// description: name of the organization
605618
// type: string
606619
// required: true
620+
// - name: event
621+
// in: query
622+
// description: workflow event name
623+
// type: string
624+
// required: false
625+
// - name: branch
626+
// in: query
627+
// description: workflow branch
628+
// type: string
629+
// required: false
630+
// - name: status
631+
// in: query
632+
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
633+
// type: string
634+
// required: false
635+
// - name: actor
636+
// in: query
637+
// description: triggered by user
638+
// type: string
639+
// required: false
640+
// - name: page
641+
// in: query
642+
// description: page number of results to return (1-based)
643+
// type: integer
644+
// - name: limit
645+
// in: query
646+
// description: page size of results
647+
// type: integer
607648
// responses:
608649
// "200":
609650
// "$ref": "#/responses/WorkflowRunsList"

routers/api/v1/repo/action.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,19 @@ func (Action) ListWorkflowJobs(ctx *context.APIContext) {
668668
// description: name of the repository
669669
// type: string
670670
// required: true
671+
// - name: status
672+
// in: query
673+
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
674+
// type: string
675+
// required: false
676+
// - name: page
677+
// in: query
678+
// description: page number of results to return (1-based)
679+
// type: integer
680+
// - name: limit
681+
// in: query
682+
// description: page size of results
683+
// type: integer
671684
// responses:
672685
// "200":
673686
// "$ref": "#/responses/WorkflowJobsList"
@@ -714,6 +727,19 @@ func (Action) ListWorkflowRuns(ctx *context.APIContext) {
714727
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
715728
// type: string
716729
// required: false
730+
// - name: actor
731+
// in: query
732+
// description: triggered by user
733+
// type: string
734+
// required: false
735+
// - name: page
736+
// in: query
737+
// description: page number of results to return (1-based)
738+
// type: integer
739+
// - name: limit
740+
// in: query
741+
// description: page size of results
742+
// type: integer
717743
// responses:
718744
// "200":
719745
// "$ref": "#/responses/ArtifactsList"
@@ -1138,6 +1164,19 @@ func ListWorkflowRunJobs(ctx *context.APIContext) {
11381164
// description: runid of the workflow run
11391165
// type: integer
11401166
// required: true
1167+
// - name: status
1168+
// in: query
1169+
// description: workflow status (pending, queued, in_progress, failure, success, skipped)
1170+
// type: string
1171+
// required: false
1172+
// - name: page
1173+
// in: query
1174+
// description: page number of results to return (1-based)
1175+
// type: integer
1176+
// - name: limit
1177+
// in: query
1178+
// description: page size of results
1179+
// type: integer
11411180
// responses:
11421181
// "200":
11431182
// "$ref": "#/responses/WorkflowJobsList"

routers/api/v1/shared/runners.go

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package shared
55

66
import (
77
"errors"
8+
"fmt"
89
"net/http"
910

1011
actions_model "code.gitea.io/gitea/models/actions"
@@ -132,12 +133,24 @@ func ListJobs(ctx *context.APIContext, ownerID, repoID, runID int64) {
132133
if ownerID != 0 && repoID != 0 {
133134
setting.PanicInDevOrTesting("ownerID and repoID should not be both set")
134135
}
135-
jobs, total, err := db.FindAndCount[actions_model.ActionRunJob](ctx, actions_model.FindRunJobOptions{
136+
opts := actions_model.FindRunJobOptions{
136137
OwnerID: ownerID,
137138
RepoID: repoID,
138139
RunID: runID,
139140
ListOptions: utils.GetListOptions(ctx),
140-
})
141+
}
142+
if statuses, ok := ctx.Req.URL.Query()["status"]; ok {
143+
for _, status := range statuses {
144+
values, err := convertToInternal(status)
145+
if err != nil {
146+
ctx.APIError(http.StatusBadRequest, fmt.Errorf("Invalid status %s", status))
147+
return
148+
}
149+
opts.Statuses = append(opts.Statuses, values...)
150+
}
151+
}
152+
153+
jobs, total, err := db.FindAndCount[actions_model.ActionRunJob](ctx, opts)
141154
if err != nil {
142155
ctx.APIErrorInternal(err)
143156
return
@@ -172,22 +185,31 @@ func ListJobs(ctx *context.APIContext, ownerID, repoID, runID int64) {
172185
ctx.JSON(http.StatusOK, &res)
173186
}
174187

175-
func convertToInternal(s string) actions_model.Status {
188+
func convertToInternal(s string) ([]actions_model.Status, error) {
176189
switch s {
177-
case "pending":
178-
return actions_model.StatusBlocked
190+
case "pending", "waiting", "requested", "action_required":
191+
return []actions_model.Status{actions_model.StatusBlocked}, nil
179192
case "queued":
180-
return actions_model.StatusWaiting
193+
return []actions_model.Status{actions_model.StatusWaiting}, nil
181194
case "in_progress":
182-
return actions_model.StatusRunning
195+
return []actions_model.Status{actions_model.StatusRunning}, nil
196+
case "completed":
197+
return []actions_model.Status{
198+
actions_model.StatusSuccess,
199+
actions_model.StatusFailure,
200+
actions_model.StatusSkipped,
201+
actions_model.StatusCancelled,
202+
}, nil
183203
case "failure":
184-
return actions_model.StatusFailure
204+
return []actions_model.Status{actions_model.StatusFailure}, nil
185205
case "success":
186-
return actions_model.StatusSuccess
187-
case "skipped":
188-
return actions_model.StatusSkipped
206+
return []actions_model.Status{actions_model.StatusSuccess}, nil
207+
case "skipped", "neutral":
208+
return []actions_model.Status{actions_model.StatusSkipped}, nil
209+
case "cancelled", "timed_out":
210+
return []actions_model.Status{actions_model.StatusCancelled}, nil
189211
default:
190-
return actions_model.StatusUnknown
212+
return nil, fmt.Errorf("invalid status %s", s)
191213
}
192214
}
193215

@@ -213,8 +235,15 @@ func ListRuns(ctx *context.APIContext, ownerID, repoID int64) {
213235
if branch := ctx.Req.URL.Query().Get("branch"); branch != "" {
214236
opts.Ref = string(git.RefNameFromBranch(branch))
215237
}
216-
if status := ctx.Req.URL.Query().Get("status"); status != "" {
217-
opts.Status = []actions_model.Status{convertToInternal(status)}
238+
if statuses, ok := ctx.Req.URL.Query()["status"]; ok {
239+
for _, status := range statuses {
240+
values, err := convertToInternal(status)
241+
if err != nil {
242+
ctx.APIError(http.StatusBadRequest, fmt.Errorf("Invalid status %s", status))
243+
return
244+
}
245+
opts.Status = append(opts.Status, values...)
246+
}
218247
}
219248
if actor := ctx.Req.URL.Query().Get("actor"); actor != "" {
220249
user, err := user_model.GetUserByName(ctx, actor)

0 commit comments

Comments
 (0)