Skip to content

Commit cc5d6bf

Browse files
toil(front): Refactor all the front lint suggestions (#318)
## 📝 Description While working on renderedtext/project-tasks#2405, I could not get a green build due to linting errors. This PR fixes credo lint errors. ## ✅ Checklist - [x] I have tested this change - [ ] This change requires documentation update
1 parent b0b8ff7 commit cc5d6bf

File tree

13 files changed

+164
-154
lines changed

13 files changed

+164
-154
lines changed

front/lib/front/branch_page/model.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,9 @@ defmodule Front.BranchPage.Model do
4747

4848
@spec get(LoadParams.t()) :: {:ok, __MODULE__.t()} | {:error, String.t()}
4949
def get(params, opts \\ []) do
50-
with true <- first_page?(params) do
51-
fetch_from_cache(params, opts[:force_cold_boot])
52-
else
53-
false ->
54-
load_from_api(params)
50+
case first_page?(params) do
51+
true -> fetch_from_cache(params, opts[:force_cold_boot])
52+
false -> load_from_api(params)
5553
end
5654
end
5755

front/lib/front/models/secret_updater.ex

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ defmodule Front.Models.SecretUpdater do
1616
processed: [],
1717
new_by_old_key:
1818
new_ones
19-
|> Enum.filter(&valid_string?(&1[old_key_as_string(type)]))
20-
|> Enum.filter(&valid_string?(&1[key_as_string(type)]))
21-
|> Enum.filter(&(valid_string?(&1["md5"]) || valid_string?(&1[value_as_string(type)])))
19+
|> Enum.filter(fn item ->
20+
valid_string?(item[old_key_as_string(type)]) &&
21+
valid_string?(item[key_as_string(type)]) &&
22+
(valid_string?(item["md5"]) || valid_string?(item[value_as_string(type)]))
23+
end)
2224
|> Map.new(&{&1[old_key_as_string(type)], &1})
2325
}
2426

front/lib/front/models/support_request.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@ defmodule Front.Models.SupportRequest do
3838
changeset = changeset(input)
3939

4040
if changeset.valid? do
41-
with {:ok, response} <- Support.submit_request(changeset.changes) do
42-
{:ok, response}
43-
else
44-
{:error, "failed-to-submit"} ->
45-
{:error, "failed-to-submit", changeset}
41+
case Support.submit_request(changeset.changes) do
42+
{:ok, response} -> {:ok, response}
43+
{:error, "failed-to-submit"} -> {:error, "failed-to-submit", changeset}
4644
end
4745
else
4846
{:error, changeset}

front/lib/front/project_settings/deletion_validator.ex

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,9 @@ defmodule Front.ProjectSettings.DeletionValidator do
4242
end
4343

4444
defp set_feedback_if_missing(changeset) do
45-
with nil <- get_field(changeset, :feedback) do
46-
put_change(changeset, :feedback, "N/A")
47-
else
48-
_e ->
49-
changeset
45+
case get_field(changeset, :feedback) do
46+
nil -> put_change(changeset, :feedback, "N/A")
47+
_e -> changeset
5048
end
5149
end
5250

front/lib/front_web/controllers/artifacts_controller.ex

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,11 @@ defmodule FrontWeb.ArtifactsController do
166166
|> Audit.metadata(source_kind: source_kind, source_id: source_id, project_id: project_id)
167167
|> Audit.log()
168168

169-
with {:ok, url} <- Artifacthub.signed_url(project_id, source_kind, source_id, artifact_path) do
170-
conn
171-
|> redirect(external: url)
172-
else
169+
case Artifacthub.signed_url(project_id, source_kind, source_id, artifact_path) do
170+
{:ok, url} ->
171+
conn
172+
|> redirect(external: url)
173+
173174
_ ->
174175
conn
175176
|> put_flash(:alert, "Failed to fetch requested artifact.")
@@ -200,12 +201,12 @@ defmodule FrontWeb.ArtifactsController do
200201
|> Audit.metadata(source_kind: source_kind, source_id: source.id, project_id: project_id)
201202
|> Audit.log()
202203

203-
with {:ok, _response} <-
204-
Artifacthub.destroy(project_id, source_kind, source.id, artifact_path) do
205-
conn
206-
|> put_flash(:notice, "Artifact resource deleted.")
207-
|> redirect(to: redirect_path(conn, action, source))
208-
else
204+
case Artifacthub.destroy(project_id, source_kind, source.id, artifact_path) do
205+
{:ok, _response} ->
206+
conn
207+
|> put_flash(:notice, "Artifact resource deleted.")
208+
|> redirect(to: redirect_path(conn, action, source))
209+
209210
_ ->
210211
conn
211212
|> put_flash(:alert, "Failed to delete the artifact.")

front/lib/front_web/controllers/notifications_controller.ex

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,27 @@ defmodule FrontWeb.NotificationsController do
222222
org_id = conn.assigns.organization_id
223223
user_id = conn.assigns.user_id
224224

225-
with {:ok, notification} <- Notification.find(id, user_id, org_id) do
226-
organization = Organization.find(org_id)
227-
228-
render(
229-
conn,
230-
"form.html",
231-
form_title: "Edit Notification",
232-
js: "notification",
233-
action: notifications_path(conn, :update, id),
234-
method: :put,
235-
notification: notification,
236-
title: "Editing #{notification.metadata.name}#{organization.name}",
237-
cancel_path: notifications_path(conn, :index),
238-
errors: %{},
239-
organization: organization,
240-
org_restricted: organization.restricted,
241-
permissions: conn.assigns.permissions,
242-
can_view_settings: true
243-
)
244-
else
225+
case Notification.find(id, user_id, org_id) do
226+
{:ok, notification} ->
227+
organization = Organization.find(org_id)
228+
229+
render(
230+
conn,
231+
"form.html",
232+
form_title: "Edit Notification",
233+
js: "notification",
234+
action: notifications_path(conn, :update, id),
235+
method: :put,
236+
notification: notification,
237+
title: "Editing #{notification.metadata.name}#{organization.name}",
238+
cancel_path: notifications_path(conn, :index),
239+
errors: %{},
240+
organization: organization,
241+
org_restricted: organization.restricted,
242+
permissions: conn.assigns.permissions,
243+
can_view_settings: true
244+
)
245+
245246
{:error, %{status: 5}} ->
246247
conn
247248
|> render_404
@@ -260,11 +261,12 @@ defmodule FrontWeb.NotificationsController do
260261
org_id = conn.assigns.organization_id
261262

262263
if conn.assigns.permissions["organization.notifications.manage"] || false do
263-
with {:ok, _} <- Notification.delete(id, user_id, org_id) do
264-
conn
265-
|> put_flash(:notice, "Notification deleted.")
266-
|> redirect(to: notifications_path(conn, :index))
267-
else
264+
case Notification.delete(id, user_id, org_id) do
265+
{:ok, _} ->
266+
conn
267+
|> put_flash(:notice, "Notification deleted.")
268+
|> redirect(to: notifications_path(conn, :index))
269+
268270
{:error, %{status: 5}} ->
269271
conn
270272
|> render_404

front/lib/front_web/controllers/organization_okta_controller.ex

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,22 @@ defmodule FrontWeb.OrganizationOktaController do
1616
Watchman.benchmark(watchman_name(:show, :duration), fn ->
1717
organization_id = conn.assigns.organization_id
1818

19-
with {:ok, params} <- fetch_params(organization_id) do
20-
case OktaIntegration.find_for_org(organization_id) do
21-
{:ok, integration} ->
22-
Watchman.increment(watchman_name(:show, :success))
23-
render_show(conn, integration, params)
24-
25-
{:error, :not_found} ->
26-
Watchman.increment(watchman_name(:show, :success))
27-
render_show(conn, nil, params)
19+
case fetch_params(organization_id) do
20+
{:ok, params} ->
21+
case OktaIntegration.find_for_org(organization_id) do
22+
{:ok, integration} ->
23+
Watchman.increment(watchman_name(:show, :success))
24+
render_show(conn, integration, params)
25+
26+
{:error, :not_found} ->
27+
Watchman.increment(watchman_name(:show, :success))
28+
render_show(conn, nil, params)
29+
30+
e ->
31+
Watchman.increment(watchman_name(:show, :failure))
32+
raise e
33+
end
2834

29-
e ->
30-
Watchman.increment(watchman_name(:show, :failure))
31-
raise e
32-
end
33-
else
3435
{:error, reason} ->
3536
Watchman.increment(watchman_name(:show, :failure))
3637
{:error, reason}
@@ -57,11 +58,11 @@ defmodule FrontWeb.OrganizationOktaController do
5758
integration |> OktaIntegration.changeset()
5859
end
5960

60-
with {:ok, params} <- fetch_params(organization_id) do
61-
Watchman.increment(watchman_name(:form, :success))
61+
case fetch_params(organization_id) do
62+
{:ok, params} ->
63+
Watchman.increment(watchman_name(:form, :success))
64+
render_form(conn, changeset, params)
6265

63-
render_form(conn, changeset, params)
64-
else
6566
{:error, reason} ->
6667
Watchman.increment(watchman_name(:form, :failure))
6768
{:error, reason}
@@ -182,9 +183,10 @@ defmodule FrontWeb.OrganizationOktaController do
182183

183184
organization_id = conn.assigns.organization_id
184185

185-
with {:ok, params} <- fetch_params(organization_id) do
186-
render_discnonnect_notice(conn, integration_id, params)
187-
else
186+
case fetch_params(organization_id) do
187+
{:ok, params} ->
188+
render_discnonnect_notice(conn, integration_id, params)
189+
188190
{:error, reason} ->
189191
Watchman.increment(watchman_name(:discnonnect_notice, :failure))
190192
{:error, reason}

front/lib/front_web/controllers/project_pfc_controller.ex

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,14 @@ defmodule FrontWeb.ProjectPFCController do
100100
log_data = log_data_closure(organization_id, project_id, user_id, :delete)
101101
audit_log(conn)
102102

103-
with :ok <- ProjectPFC.destroy(project_id, user_id) do
104-
Watchman.increment(watchman_name(:delete, :success))
103+
case ProjectPFC.destroy(project_id, user_id) do
104+
:ok ->
105+
Watchman.increment(watchman_name(:delete, :success))
106+
107+
conn
108+
|> put_flash(:notice, "Success: pre-flight checks deleted")
109+
|> redirect(to: project_pfc_path(conn, :show, project_name_or_id))
105110

106-
conn
107-
|> put_flash(:notice, "Success: pre-flight checks deleted")
108-
|> redirect(to: project_pfc_path(conn, :show, project_name_or_id))
109-
else
110111
{:error, reason} ->
111112
Watchman.increment(watchman_name(:delete, :failure))
112113
Logger.error(log_data.(reason))

front/lib/front_web/controllers/project_settings/secrets_controller.ex

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -133,23 +133,23 @@ defmodule FrontWeb.ProjectSettings.SecretsController do
133133
|> Audit.metadata(project_name: conn.assigns.project.name)
134134
|> Audit.log()
135135

136-
with {:ok, secret} <-
137-
Models.Secret.create(
138-
params["name"],
139-
Params.parse_params(params),
140-
%{project_id: project_id},
141-
:PROJECT,
142-
user_id,
143-
org_id
144-
) do
145-
audit
146-
|> Audit.add(:resource_id, secret.id)
147-
|> Audit.log()
136+
case Models.Secret.create(
137+
params["name"],
138+
Params.parse_params(params),
139+
%{project_id: project_id},
140+
:PROJECT,
141+
user_id,
142+
org_id
143+
) do
144+
{:ok, secret} ->
145+
audit
146+
|> Audit.add(:resource_id, secret.id)
147+
|> Audit.log()
148+
149+
conn
150+
|> put_flash(:notice, "Secret created.")
151+
|> redirect(to: secrets_path(conn, :index, conn.params["name_or_id"]))
148152

149-
conn
150-
|> put_flash(:notice, "Secret created.")
151-
|> redirect(to: secrets_path(conn, :index, conn.params["name_or_id"]))
152-
else
153153
{:error, validation_errors} ->
154154
maybe_permissions =
155155
Async.run(fn ->
@@ -235,31 +235,33 @@ defmodule FrontWeb.ProjectSettings.SecretsController do
235235
org_id = conn.assigns.organization_id
236236
project = conn.assigns.project
237237

238-
with {:ok, _secret} <-
239-
Models.Secret.update(
240-
secret_id,
241-
params["name"],
242-
params["description"],
243-
Params.parse_env_vars(params),
244-
Params.parse_files(params),
245-
user_id,
246-
org_id,
247-
secret_level: :PROJECT,
248-
project_config: %{project_id: project.id},
249-
project_id: project.id
250-
) do
251-
conn
252-
|> Audit.new(:Secret, :Modified)
253-
|> Audit.add(description: "Updated secret #{params["name"]} in the project #{project.id}")
254-
|> Audit.add(resource_name: params["name"])
255-
|> Audit.metadata(project_id: project.id)
256-
|> Audit.metadata(project_name: project.name)
257-
|> Audit.log()
238+
case Models.Secret.update(
239+
secret_id,
240+
params["name"],
241+
params["description"],
242+
Params.parse_env_vars(params),
243+
Params.parse_files(params),
244+
user_id,
245+
org_id,
246+
secret_level: :PROJECT,
247+
project_config: %{project_id: project.id},
248+
project_id: project.id
249+
) do
250+
{:ok, _secret} ->
251+
conn
252+
|> Audit.new(:Secret, :Modified)
253+
|> Audit.add(
254+
description: "Updated secret #{params["name"]} in the project #{project.id}"
255+
)
256+
|> Audit.add(resource_name: params["name"])
257+
|> Audit.metadata(project_id: project.id)
258+
|> Audit.metadata(project_name: project.name)
259+
|> Audit.log()
260+
261+
conn
262+
|> put_flash(:notice, "Secret updated.")
263+
|> redirect(to: secrets_path(conn, :index, project.name))
258264

259-
conn
260-
|> put_flash(:notice, "Secret updated.")
261-
|> redirect(to: secrets_path(conn, :index, project.name))
262-
else
263265
{:error, :not_found} ->
264266
conn
265267
|> render_404

front/lib/front_web/controllers/secrets_controller.ex

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,25 @@ defmodule FrontWeb.SecretsController do
204204
|> Audit.log()
205205

206206
if conn.assigns.permissions["organization.secrets.manage"] || false do
207-
with {:ok, _secret} <-
208-
Models.Secret.update(
209-
secret_id,
210-
params["name"],
211-
params["description"],
212-
Params.parse_env_vars(params),
213-
Params.parse_files(params),
214-
user_id,
215-
org_id,
216-
org_config:
217-
Params.parse_org_config(
218-
params,
219-
conn.assigns.permissions["organization.secrets_policy_settings.manage"]
220-
)
221-
) do
222-
conn
223-
|> put_flash(:notice, "Secret updated.")
224-
|> redirect(to: secrets_path(conn, :index))
225-
else
207+
case Models.Secret.update(
208+
secret_id,
209+
params["name"],
210+
params["description"],
211+
Params.parse_env_vars(params),
212+
Params.parse_files(params),
213+
user_id,
214+
org_id,
215+
org_config:
216+
Params.parse_org_config(
217+
params,
218+
conn.assigns.permissions["organization.secrets_policy_settings.manage"]
219+
)
220+
) do
221+
{:ok, _secret} ->
222+
conn
223+
|> put_flash(:notice, "Secret updated.")
224+
|> redirect(to: secrets_path(conn, :index))
225+
226226
{:error, :not_found} ->
227227
conn
228228
|> render_404

0 commit comments

Comments
 (0)