-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathrun_controller.ex
More file actions
157 lines (127 loc) · 5.15 KB
/
run_controller.ex
File metadata and controls
157 lines (127 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
defmodule LightningWeb.API.RunController do
@moduledoc """
API controller for managing runs.
## Query Parameters
- `page` - Page number (default: 1)
- `page_size` - Number of items per page (default: 10)
- `inserted_after` - Filter runs created after this ISO8601 datetime
- `inserted_before` - Filter runs created before this ISO8601 datetime
- `updated_after` - Filter runs updated after this ISO8601 datetime
- `updated_before` - Filter runs updated before this ISO8601 datetime
## Examples
GET /api/runs?page=1&page_size=20
GET /api/runs?inserted_after=2024-01-01T00:00:00Z
GET /api/runs?inserted_after=2024-01-01T00:00:00Z&inserted_before=2024-12-31T23:59:59Z
GET /api/projects/:project_id/runs?inserted_after=2024-01-01T00:00:00Z
"""
use LightningWeb, :controller
alias Lightning.Invocation
alias Lightning.Policies.Permissions
alias Lightning.Policies.ProjectUsers
alias Lightning.Runs
alias LightningWeb.API.Helpers
action_fallback LightningWeb.FallbackController
@doc """
Lists runs with optional project filtering.
This function has two variants:
- With `project_id`: Returns runs for a specific project
- Without `project_id`: Returns runs across all accessible projects
Returns a paginated list of runs with optional datetime filtering.
## Parameters
- `conn` - The Plug connection struct with the current resource assigned
- `params` - Map containing:
- `project_id` - Project UUID (optional, filters to specific project)
- `page` - Page number (optional, default: 1)
- `page_size` - Items per page (optional, default: 10)
- `inserted_after` - Filter runs created after ISO8601 datetime (optional)
- `inserted_before` - Filter runs created before ISO8601 datetime (optional)
- `updated_after` - Filter runs updated after ISO8601 datetime (optional)
- `updated_before` - Filter runs updated before ISO8601 datetime (optional)
## Returns
- Renders JSON with paginated list of runs
- `404 Not Found` if project doesn't exist (when project_id provided)
- `403 Forbidden` if user lacks project access (when project_id provided)
- `422 Unprocessable Entity` if datetime parameters are invalid
## Examples
# All runs accessible to user
GET /api/runs
GET /api/runs?page=2&page_size=50
GET /api/runs?inserted_after=2024-01-01T00:00:00Z
# Runs for specific project
GET /api/projects/a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d/runs
GET /api/projects/a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d/runs?page=2
GET /api/projects/a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d/runs?inserted_after=2024-01-01T00:00:00Z
"""
@spec index(Plug.Conn.t(), map()) :: Plug.Conn.t()
def index(conn, %{"project_id" => project_id} = params) do
pagination_attrs = Map.take(params, ["page_size", "page"])
with :ok <- Helpers.validate_uuid(project_id),
:ok <-
Invocation.Query.validate_datetime_params(params, [
"inserted_after",
"inserted_before",
"updated_after",
"updated_before"
]),
project <- Lightning.Projects.get_project(project_id),
:ok <-
ProjectUsers
|> Permissions.can(
:access_project,
conn.assigns.current_resource,
project
) do
page =
Runs.runs_for_project_query(project)
|> Invocation.Query.filter_runs(params)
|> Lightning.Repo.paginate(pagination_attrs)
render(conn, "index.json", page: page, conn: conn)
end
end
def index(conn, params) do
with :ok <-
Invocation.Query.validate_datetime_params(params, [
"inserted_after",
"inserted_before",
"updated_after",
"updated_before"
]) do
pagination_attrs = Map.take(params, ["page_size", "page"])
page =
Runs.runs_for_user_query(conn.assigns.current_resource)
|> Invocation.Query.filter_runs(params)
|> Lightning.Repo.paginate(pagination_attrs)
render(conn, "index.json", page: page, conn: conn)
end
end
@doc """
Retrieves a specific run by ID.
Returns detailed information about a single run including its associated
work order and workflow. Access is granted if the user has access to the
run's parent project.
## Parameters
- `conn` - The Plug connection struct with the current resource assigned
- `params` - Map containing:
- `id` - Run UUID (required)
## Returns
- `200 OK` with run JSON including work order and workflow
- `404 Not Found` if run doesn't exist
- `403 Forbidden` if user lacks project access
## Examples
GET /api/runs/a1b2c3d4-5e6f-7a8b-9c0d-1e2f3a4b5c6d
"""
@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t()
def show(conn, %{"id" => id}) do
with :ok <- Helpers.validate_uuid(id),
run <- Runs.get(id, include: [work_order: [workflow: :project]]),
:ok <-
ProjectUsers
|> Permissions.can(
:access_project,
conn.assigns.current_resource,
run.work_order.workflow.project
) do
render(conn, "show.json", run: run, conn: conn)
end
end
end