Skip to content

Commit f82178e

Browse files
committed
add more test and more level of runs / jobs access
1 parent 3f479a2 commit f82178e

File tree

13 files changed

+577
-157
lines changed

13 files changed

+577
-157
lines changed

models/actions/run_job_list.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ func (opts FindRunJobOptions) ToConds() builder.Cond {
8585
if opts.RepoID > 0 {
8686
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
8787
}
88-
if opts.OwnerID > 0 {
89-
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
90-
}
9188
if opts.CommitSHA != "" {
9289
cond = cond.And(builder.Eq{"commit_sha": opts.CommitSHA})
9390
}
@@ -99,3 +96,15 @@ func (opts FindRunJobOptions) ToConds() builder.Cond {
9996
}
10097
return cond
10198
}
99+
100+
func (opts FindRunJobOptions) ToJoins() []db.JoinFunc {
101+
if opts.OwnerID > 0 {
102+
return []db.JoinFunc{
103+
func(sess db.Engine) error {
104+
sess.Join("INNER", "repository", "repository.id = repo_id AND repository.owner_id = ?", opts.OwnerID)
105+
return nil
106+
},
107+
}
108+
}
109+
return nil
110+
}

models/actions/run_list.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ func (opts FindRunOptions) ToConds() builder.Cond {
7979
if opts.RepoID > 0 {
8080
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
8181
}
82-
if opts.OwnerID > 0 {
83-
cond = cond.And(builder.Eq{"owner_id": opts.OwnerID})
84-
}
8582
if opts.WorkflowID != "" {
8683
cond = cond.And(builder.Eq{"workflow_id": opts.WorkflowID})
8784
}
@@ -103,6 +100,16 @@ func (opts FindRunOptions) ToConds() builder.Cond {
103100
return cond
104101
}
105102

103+
func (opts FindRunOptions) ToJoins() []db.JoinFunc {
104+
if opts.OwnerID > 0 {
105+
return []db.JoinFunc{func(sess db.Engine) error {
106+
sess.Join("INNER", "repository", "repository.id = repo_id AND repository.owner_id = ?", opts.OwnerID)
107+
return nil
108+
}}
109+
}
110+
return nil
111+
}
112+
106113
func (opts FindRunOptions) ToOrders() string {
107114
return "`id` DESC"
108115
}

models/fixtures/action_run.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,22 @@
9393
updated: 1683636626
9494
need_approval: 0
9595
approved_by: 0
96+
-
97+
id: 803
98+
title: "workflow run list for user"
99+
repo_id: 2
100+
owner_id: 0
101+
workflow_id: "test.yaml"
102+
index: 191
103+
trigger_user_id: 1
104+
ref: "refs/heads/test"
105+
commit_sha: "c2d72f548424103f01ee1dc02889c1e2bff816b0"
106+
event: "push"
107+
is_fork_pull_request: 0
108+
status: 1
109+
started: 1683636528
110+
stopped: 1683636626
111+
created: 1683636108
112+
updated: 1683636626
113+
need_approval: 0
114+
approved_by: 0

models/fixtures/action_run_job.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,22 @@
7373
id: 203
7474
run_id: 802
7575
repo_id: 5
76-
owner_id: 3
76+
owner_id: 0
77+
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
78+
is_fork_pull_request: 0
79+
name: job2
80+
attempt: 1
81+
job_id: job2
82+
needs: '["job1"]'
83+
task_id: 51
84+
status: 5
85+
started: 1683636528
86+
stopped: 1683636626
87+
-
88+
id: 204
89+
run_id: 803
90+
repo_id: 2
91+
owner_id: 0
7792
commit_sha: c2d72f548424103f01ee1dc02889c1e2bff816b0
7893
is_fork_pull_request: 0
7994
name: job2

routers/api/v1/admin/runners.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,3 +102,39 @@ func DeleteRunner(ctx *context.APIContext) {
102102
// "$ref": "#/responses/notFound"
103103
shared.DeleteRunner(ctx, 0, 0, ctx.PathParamInt64("runner_id"))
104104
}
105+
106+
// ListWorkflowJobs Lists all jobs
107+
func ListWorkflowJobs(ctx *context.APIContext) {
108+
// swagger:operation GET /admin/actions/jobs admin listAdminWorkflowJobs
109+
// ---
110+
// summary: Lists all jobs
111+
// produces:
112+
// - application/json
113+
// responses:
114+
// "200":
115+
// "$ref": "#/responses/JobList"
116+
// "400":
117+
// "$ref": "#/responses/error"
118+
// "404":
119+
// "$ref": "#/responses/notFound"
120+
121+
shared.ListJobs(ctx, 0, 0, 0)
122+
}
123+
124+
// ListWorkflowRuns Lists all runs
125+
func ListWorkflowRuns(ctx *context.APIContext) {
126+
// swagger:operation GET /admin/actions/runs admin listAdminWorkflowRuns
127+
// ---
128+
// summary: Lists all runs
129+
// produces:
130+
// - application/json
131+
// responses:
132+
// "200":
133+
// "$ref": "#/responses/RunList"
134+
// "400":
135+
// "$ref": "#/responses/error"
136+
// "404":
137+
// "$ref": "#/responses/notFound"
138+
139+
shared.ListRuns(ctx, 0, 0)
140+
}

routers/api/v1/api.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -942,6 +942,8 @@ func Routes() *web.Router {
942942
m.Get("/{runner_id}", reqToken(), reqChecker, act.GetRunner)
943943
m.Delete("/{runner_id}", reqToken(), reqChecker, act.DeleteRunner)
944944
})
945+
m.Get("/runs", reqToken(), reqChecker, act.ListWorkflowRuns)
946+
m.Get("/jobs", reqToken(), reqChecker, act.ListWorkflowJobs)
945947
})
946948
}
947949

@@ -1077,6 +1079,9 @@ func Routes() *web.Router {
10771079
m.Get("/{runner_id}", reqToken(), user.GetRunner)
10781080
m.Delete("/{runner_id}", reqToken(), user.DeleteRunner)
10791081
})
1082+
1083+
m.Get("/runs", reqToken(), user.ListWorkflowRuns)
1084+
m.Get("/jobs", reqToken(), user.ListWorkflowJobs)
10801085
})
10811086

10821087
m.Get("/followers", user.ListMyFollowers)
@@ -1281,10 +1286,9 @@ func Routes() *web.Router {
12811286
m.Group("/actions", func() {
12821287
m.Get("/tasks", repo.ListActionTasks)
12831288
m.Group("/runs", func() {
1284-
m.Get("", repo.GetWorkflowRuns)
12851289
m.Group("/{run}", func() {
12861290
m.Get("", repo.GetWorkflowRun)
1287-
m.Get("/jobs", repo.GetWorkflowJobs)
1291+
m.Get("/jobs", repo.ListWorkflowRunJobs)
12881292
m.Get("/artifacts", repo.GetArtifactsOfRun)
12891293
})
12901294
})
@@ -1737,11 +1741,15 @@ func Routes() *web.Router {
17371741
Patch(bind(api.EditHookOption{}), admin.EditHook).
17381742
Delete(admin.DeleteHook)
17391743
})
1740-
m.Group("/actions/runners", func() {
1741-
m.Get("", admin.ListRunners)
1742-
m.Post("/registration-token", admin.CreateRegistrationToken)
1743-
m.Get("/{runner_id}", admin.GetRunner)
1744-
m.Delete("/{runner_id}", admin.DeleteRunner)
1744+
m.Group("/actions", func() {
1745+
m.Group("/runners", func() {
1746+
m.Get("", admin.ListRunners)
1747+
m.Post("/registration-token", admin.CreateRegistrationToken)
1748+
m.Get("/{runner_id}", admin.GetRunner)
1749+
m.Delete("/{runner_id}", admin.DeleteRunner)
1750+
})
1751+
m.Get("/runs", admin.ListWorkflowRuns)
1752+
m.Get("/jobs", admin.ListWorkflowJobs)
17451753
})
17461754
m.Group("/runners", func() {
17471755
m.Get("/registration-token", admin.GetRegistrationToken)

routers/api/v1/org/action.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,36 @@ func (Action) DeleteRunner(ctx *context.APIContext) {
570570
shared.DeleteRunner(ctx, ctx.Org.Organization.ID, 0, ctx.PathParamInt64("runner_id"))
571571
}
572572

573+
func (Action) ListWorkflowJobs(ctx *context.APIContext) {
574+
// swagger:operation GET /orgs/{org}/actions/jobs organization getOrgWorkflowJobs
575+
// ---
576+
// summary: Get org-level workflow jobs
577+
// produces:
578+
// - application/json
579+
// parameters:
580+
// - name: org
581+
// in: path
582+
// description: name of the organization
583+
// type: string
584+
// required: true
585+
shared.ListJobs(ctx, ctx.Org.Organization.ID, 0, 0)
586+
}
587+
588+
func (Action) ListWorkflowRuns(ctx *context.APIContext) {
589+
// swagger:operation GET /orgs/{org}/actions/runs organization getOrgWorkflowRuns
590+
// ---
591+
// summary: Get org-level workflow runs
592+
// produces:
593+
// - application/json
594+
// parameters:
595+
// - name: org
596+
// in: path
597+
// description: name of the organization
598+
// type: string
599+
// required: true
600+
shared.ListRuns(ctx, ctx.Org.Organization.ID, 0)
601+
}
602+
573603
var _ actions_service.API = new(Action)
574604

575605
// Action implements actions_service.API

0 commit comments

Comments
 (0)