Skip to content

Commit e846469

Browse files
author
Taras Tyshko
committed
Refactor complex macros and enforce Credo in CI
1 parent e16265d commit e846469

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+336
-321
lines changed

.credo.exs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,6 @@
8484
{Credo.Check.Design.AliasUsage,
8585
[priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]},
8686
{Credo.Check.Design.TagFIXME, []},
87-
# You can also customize the exit_status of each check.
88-
# If you don't want TODO comments to cause `mix credo` to fail, just
89-
# set this value to 0 (zero).
90-
#
91-
{Credo.Check.Design.TagTODO, [exit_status: 0]},
92-
9387
#
9488
## Readability Checks
9589
#
@@ -99,7 +93,6 @@
9993
{Credo.Check.Readability.LargeNumbers, []},
10094
{Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]},
10195
{Credo.Check.Readability.ModuleAttributeNames, []},
102-
{Credo.Check.Readability.ModuleDoc, []},
10396
{Credo.Check.Readability.ModuleNames, []},
10497
{Credo.Check.Readability.MultiAlias, []},
10598
{Credo.Check.Readability.ParenthesesInCondition, []},
@@ -170,6 +163,14 @@
170163
{Credo.Check.Warning.WrongTestFileExtension, []}
171164
],
172165
disabled: [
166+
#
167+
# TODO comments are acceptable in our codebase
168+
{Credo.Check.Design.TagTODO, []},
169+
170+
#
171+
# ModuleDoc is not required in our codebase
172+
{Credo.Check.Readability.ModuleDoc, []},
173+
173174
#
174175
# Checks scheduled for next check update (opt-in for now)
175176
{Credo.Check.Refactor.UtcNowTruncate, []},

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ jobs:
112112
- name: Check spelling
113113
run: mix spellweaver.check
114114

115-
- name: Run Credo (won't fail the build)
116-
run: mix credo --mute-exit-status
115+
- name: Run Credo
116+
run: mix credo
117117

118118
- name: DB Setup
119119
run: mix ecto.migrate.reset

lib/nerves_hub/accounts.ex

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@ defmodule NervesHub.Accounts do
33

44
alias Ecto.Changeset
55
alias Ecto.Multi
6-
76
alias NervesHub.Accounts.Invite
87
alias NervesHub.Accounts.Org
98
alias NervesHub.Accounts.OrgKey
109
alias NervesHub.Accounts.OrgMetric
1110
alias NervesHub.Accounts.OrgUser
1211
alias NervesHub.Accounts.RemoveAccount
1312
alias NervesHub.Accounts.User
13+
alias NervesHub.Accounts.UserNotifier
1414
alias NervesHub.Accounts.UserToken
1515
alias NervesHub.Devices
1616
alias NervesHub.Devices.Device
1717
alias NervesHub.Products.Product
18-
alias NervesHub.Accounts.UserNotifier
1918

2019
alias NervesHub.Repo
2120

lib/nerves_hub/accounts/user_notifier.ex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ defmodule NervesHub.Accounts.UserNotifier do
77
alias NervesHub.Emails.ConfirmationTemplate
88
alias NervesHub.Emails.LoginWithGoogleReminderTemplate
99
alias NervesHub.Emails.OrgUserAddedTemplate
10-
alias NervesHub.Emails.PasswordResetTemplate
1110
alias NervesHub.Emails.PasswordResetConfirmationTemplate
11+
alias NervesHub.Emails.PasswordResetTemplate
1212
alias NervesHub.Emails.PasswordUpdatedTemplate
1313
alias NervesHub.Emails.TellOrgUserAddedTemplate
1414
alias NervesHub.Emails.TellOrgUserInvitedTemplate
@@ -17,6 +17,7 @@ defmodule NervesHub.Accounts.UserNotifier do
1717
alias NervesHub.Emails.WelcomeTemplate
1818

1919
alias NervesHub.SwooshMailer, as: Mailer
20+
alias Phoenix.HTML
2021

2122
def deliver_confirmation_instructions(user, confirmation_url) do
2223
assigns = %{
@@ -207,7 +208,7 @@ defmodule NervesHub.Accounts.UserNotifier do
207208

208209
text =
209210
module.text_render(assigns)
210-
|> Phoenix.HTML.Safe.to_iodata()
211+
|> HTML.Safe.to_iodata()
211212
|> IO.iodata_to_binary()
212213

213214
{html, text}

lib/nerves_hub/application.ex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule NervesHub.Application do
22
use Application
3-
43
require Logger
4+
alias NervesHub.Telemetry.Customizations
55

66
def start(_type, _args) do
77
case System.cmd("fwup", ["--version"], env: []) do
@@ -53,8 +53,7 @@ defmodule NervesHub.Application do
5353
:ok = :httpc.set_option(:ipfamily, :inet6fb4)
5454
end
5555

56-
:ok = NervesHub.Telemetry.Customizations.setup()
57-
56+
:ok = Customizations.setup()
5857
:ok = OpentelemetryBandit.setup()
5958
:ok = OpentelemetryPhoenix.setup(adapter: :bandit)
6059
:ok = OpentelemetryOban.setup(trace: [:jobs])

lib/nerves_hub/certificate.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
defmodule NervesHub.Certificate do
2+
alias X509.Certificate.Extension
3+
24
import X509.ASN1,
35
only: [
46
extension: 1,
@@ -17,7 +19,7 @@ defmodule NervesHub.Certificate do
1719
def get_aki(otp_certificate) do
1820
otp_certificate
1921
|> X509.Certificate.extensions()
20-
|> X509.Certificate.Extension.find(:authority_key_identifier)
22+
|> Extension.find(:authority_key_identifier)
2123
|> extension()
2224
|> Keyword.get(:extnValue)
2325
|> authority_key_identifier()
@@ -27,7 +29,7 @@ defmodule NervesHub.Certificate do
2729
def get_ski(otp_certificate) do
2830
otp_certificate
2931
|> X509.Certificate.extensions()
30-
|> X509.Certificate.Extension.find(:subject_key_identifier)
32+
|> Extension.find(:subject_key_identifier)
3133
|> case do
3234
nil ->
3335
nil

lib/nerves_hub/device_ssl_transport.ex

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@ defmodule NervesHub.DeviceSSLTransport do
1313

1414
@behaviour ThousandIsland.Transport
1515

16+
alias ThousandIsland.Transports.SSL
17+
1618
@impl ThousandIsland.Transport
17-
defdelegate listen(port, user_options), to: ThousandIsland.Transports.SSL
19+
defdelegate listen(port, user_options), to: SSL
1820

1921
@impl ThousandIsland.Transport
20-
defdelegate accept(listener_socket), to: ThousandIsland.Transports.SSL
22+
defdelegate accept(listener_socket), to: SSL
2123

2224
@impl ThousandIsland.Transport
2325
def handshake(socket) do
2426
if NervesHub.RateLimit.increment() do
2527
:telemetry.execute([:nerves_hub, :rate_limit, :accepted], %{count: 1})
2628

27-
ThousandIsland.Transports.SSL.handshake(socket)
29+
SSL.handshake(socket)
2830
else
2931
:telemetry.execute([:nerves_hub, :rate_limit, :rejected], %{count: 1})
3032

@@ -33,50 +35,50 @@ defmodule NervesHub.DeviceSSLTransport do
3335
end
3436

3537
@impl ThousandIsland.Transport
36-
defdelegate upgrade(socket, opts), to: ThousandIsland.Transports.SSL
38+
defdelegate upgrade(socket, opts), to: SSL
3739

3840
@impl ThousandIsland.Transport
39-
defdelegate controlling_process(socket, pid), to: ThousandIsland.Transports.SSL
41+
defdelegate controlling_process(socket, pid), to: SSL
4042

4143
@impl ThousandIsland.Transport
42-
defdelegate recv(socket, length, timeout), to: ThousandIsland.Transports.SSL
44+
defdelegate recv(socket, length, timeout), to: SSL
4345

4446
@impl ThousandIsland.Transport
45-
defdelegate send(socket, data), to: ThousandIsland.Transports.SSL
47+
defdelegate send(socket, data), to: SSL
4648

4749
@impl ThousandIsland.Transport
48-
defdelegate sendfile(socket, filename, offset, length), to: ThousandIsland.Transports.SSL
50+
defdelegate sendfile(socket, filename, offset, length), to: SSL
4951

5052
@impl ThousandIsland.Transport
51-
defdelegate getopts(socket, options), to: ThousandIsland.Transports.SSL
53+
defdelegate getopts(socket, options), to: SSL
5254

5355
@impl ThousandIsland.Transport
54-
defdelegate setopts(socket, options), to: ThousandIsland.Transports.SSL
56+
defdelegate setopts(socket, options), to: SSL
5557

5658
@impl ThousandIsland.Transport
57-
defdelegate shutdown(socket, way), to: ThousandIsland.Transports.SSL
59+
defdelegate shutdown(socket, way), to: SSL
5860

5961
@impl ThousandIsland.Transport
60-
defdelegate close(socket), to: ThousandIsland.Transports.SSL
62+
defdelegate close(socket), to: SSL
6163

6264
@impl ThousandIsland.Transport
63-
defdelegate sockname(socket), to: ThousandIsland.Transports.SSL
65+
defdelegate sockname(socket), to: SSL
6466

6567
@impl ThousandIsland.Transport
66-
defdelegate peername(socket), to: ThousandIsland.Transports.SSL
68+
defdelegate peername(socket), to: SSL
6769

6870
@impl ThousandIsland.Transport
69-
defdelegate peercert(socket), to: ThousandIsland.Transports.SSL
71+
defdelegate peercert(socket), to: SSL
7072

7173
@impl ThousandIsland.Transport
72-
defdelegate secure?(), to: ThousandIsland.Transports.SSL
74+
defdelegate secure?(), to: SSL
7375

7476
@impl ThousandIsland.Transport
75-
defdelegate getstat(socket), to: ThousandIsland.Transports.SSL
77+
defdelegate getstat(socket), to: SSL
7678

7779
@impl ThousandIsland.Transport
78-
defdelegate negotiated_protocol(socket), to: ThousandIsland.Transports.SSL
80+
defdelegate negotiated_protocol(socket), to: SSL
7981

8082
@impl ThousandIsland.Transport
81-
defdelegate connection_information(socket), to: ThousandIsland.Transports.SSL
83+
defdelegate connection_information(socket), to: SSL
8284
end

lib/nerves_hub/devices.ex

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ defmodule NervesHub.Devices do
3434
alias NervesHub.Products.Product
3535
alias NervesHub.Repo
3636
alias NervesHub.TaskSupervisor, as: Tasks
37+
alias Phoenix.Channel.Server
3738

3839
def get_device(device_id) when is_integer(device_id) do
3940
Repo.get(Device, device_id)
@@ -995,13 +996,8 @@ defmodule NervesHub.Devices do
995996
firmware_uuid: firmware_uuid
996997
}
997998

998-
_ =
999-
Phoenix.Channel.Server.broadcast(
1000-
NervesHub.PubSub,
1001-
"orchestrator:deployment:#{device.deployment_id}",
1002-
"device-online",
1003-
payload
1004-
)
999+
topic = "orchestrator:deployment:#{device.deployment_id}"
1000+
_ = Server.broadcast(NervesHub.PubSub, topic, "device-online", payload)
10051001

10061002
:ok
10071003
end
@@ -1011,13 +1007,8 @@ defmodule NervesHub.Devices do
10111007
end
10121008

10131009
def deployment_device_updated(device) do
1014-
_ =
1015-
Phoenix.Channel.Server.broadcast(
1016-
NervesHub.PubSub,
1017-
"orchestrator:deployment:#{device.deployment_id}",
1018-
"device-updated",
1019-
%{}
1020-
)
1010+
topic = "orchestrator:deployment:#{device.deployment_id}"
1011+
_ = Server.broadcast(NervesHub.PubSub, topic, "device-updated", %{})
10211012

10221013
:ok
10231014
end
@@ -1159,12 +1150,8 @@ defmodule NervesHub.Devices do
11591150
{:ok, device} = result ->
11601151
_ =
11611152
if device.deployment_id do
1162-
Phoenix.Channel.Server.broadcast(
1163-
NervesHub.PubSub,
1164-
"orchestrator:deployment:#{device.deployment_id}",
1165-
"device-updated",
1166-
%{}
1167-
)
1153+
topic = "orchestrator:deployment:#{device.deployment_id}"
1154+
Server.broadcast(NervesHub.PubSub, topic, "device-updated", %{})
11681155
end
11691156

11701157
result
@@ -1640,13 +1627,8 @@ defmodule NervesHub.Devices do
16401627

16411628
payload = %{inflight_update: inflight_update, update_payload: update_payload}
16421629

1643-
_ =
1644-
Phoenix.Channel.Server.broadcast(
1645-
NervesHub.PubSub,
1646-
"device:#{device_id}",
1647-
"update-scheduled",
1648-
payload
1649-
)
1630+
topic = "device:#{device_id}"
1631+
_ = Server.broadcast(NervesHub.PubSub, topic, "update-scheduled", payload)
16501632

16511633
:ok
16521634
end

lib/nerves_hub/devices/connections.ex

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule NervesHub.Devices.Connections do
77
alias NervesHub.Devices.Device
88
alias NervesHub.Devices.DeviceConnection
99
alias NervesHub.Repo
10+
alias Phoenix.Channel.Server
1011

1112
@doc """
1213
Get all connections for a device.
@@ -90,20 +91,11 @@ defmodule NervesHub.Devices.Connections do
9091
{1, nil} =
9192
DeviceConnection
9293
|> where([dc], dc.id == ^id)
93-
|> Repo.update_all(
94-
set: [
95-
status: "connected",
96-
last_seen_at: DateTime.utc_now()
97-
]
98-
)
94+
|> Repo.update_all(set: [status: "connected", last_seen_at: DateTime.utc_now()])
9995

100-
Phoenix.Channel.Server.broadcast_from!(
101-
NervesHub.PubSub,
102-
self(),
103-
"device:#{device.identifier}:internal",
104-
"connection:heartbeat",
105-
%{}
106-
)
96+
event = "connection:heartbeat"
97+
topic = "device:#{device.identifier}:internal"
98+
Server.broadcast_from!(NervesHub.PubSub, self(), topic, event, %{})
10799
end
108100

109101
@doc """

lib/nerves_hub/devices/log_lines.ex

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ defmodule NervesHub.Devices.LogLines do
66
alias NervesHub.AnalyticsRepo
77
alias NervesHub.Devices.Device
88
alias NervesHub.Devices.LogLine
9+
alias Phoenix.Channel.Server
910

1011
import Ecto.Query
1112

@@ -53,13 +54,8 @@ defmodule NervesHub.Devices.LogLines do
5354
{:ok, log_line} ->
5455
_ = AnalyticsRepo.insert_all(LogLine, [changeset.changes], settings: [async_insert: 1])
5556

56-
_ =
57-
Phoenix.Channel.Server.broadcast(
58-
NervesHub.PubSub,
59-
"device:#{device.identifier}:internal",
60-
"logs:received",
61-
log_line
62-
)
57+
topic = "device:#{device.identifier}:internal"
58+
_ = Server.broadcast(NervesHub.PubSub, topic, "logs:received", log_line)
6359

6460
{:ok, log_line}
6561

0 commit comments

Comments
 (0)