Skip to content

Commit cf09ac3

Browse files
authored
Début de sortie des lectures d'env à compile time (credo check + 1er refactoring) (#5521)
1 parent 6a5f6d0 commit cf09ac3

8 files changed

Lines changed: 81 additions & 37 deletions

File tree

.credo.exs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
2121
# You can give explicit globs or simply directories.
2222
# In the latter case `**/*.{ex,exs}` will be used.
2323
#
24-
included: ["lib/", "src/", "web/", "apps/"],
25-
excluded: [~r"/_build/", ~r"/deps/", ~r"/apps/transport/client/", "apps/transport/lib/transport/protobuf/gtfs-realtime.pb.ex"]
24+
included: ["lib/", "src/", "web/", "apps/", "config/"],
25+
excluded: [
26+
~r"/_build/",
27+
~r"/deps/",
28+
~r"/apps/transport/client/",
29+
"apps/transport/lib/transport/protobuf/gtfs-realtime.pb.ex",
30+
# Template only — copied by devs to dev.secret.exs (gitignored), never loaded at runtime.
31+
"config/dev.secret.template.exs"
32+
]
2633
},
2734
#
2835
# If you create your own checks, you must specify the source files for
2936
# them here, so they can be loaded by Credo before running the analysis.
3037
#
31-
requires: [],
38+
requires: ["credo_checks/no_system_env_at_compile_time.ex"],
3239
#
3340
# If you want to enforce a style guide and need a more traditional linting
3441
# experience, you can change `strict` to `true` below:
@@ -117,6 +124,9 @@
117124
{Credo.Check.Warning.UnusedTupleOperation},
118125
{Credo.Check.Warning.RaiseInsideRescue},
119126
{Credo.Check.Warning.MixEnv},
127+
{Credo.Check.Warning.ApplicationConfigInModuleAttribute},
128+
# During migration, do not block the CI.
129+
{Credo.Check.Custom.NoSystemEnvAtCompileTime, exit_status: 0},
120130

121131
# Controversial and experimental checks (opt-in, just remove `, false`)
122132
#

config/config.exs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ config :phoenix, :template_engines,
7676

7777
config :phoenix_markdown, :server_tags, :all
7878

79+
config :transport, gbfs_validator_impl: Shared.Validation.GBFSValidator.HTTPValidatorClient
80+
7981
config :transport,
8082
cache_impl: Transport.Cache.Cachex,
8183
ex_aws_impl: ExAws,
@@ -231,12 +233,12 @@ config :appsignal, :config,
231233
"Unlock.Controller#fetch"
232234
]
233235

236+
config :transport, DB.Repo, types: DB.PostgrexTypes
237+
config :transport, ecto_repos: [DB.Repo]
238+
234239
# Import environment specific config. This must remain at the bottom
235240
# of this file so it overrides the configuration defined above.
236241
import_config "datagouvfr.exs"
237-
import_config "database.exs"
238-
import_config "gtfs_validator.exs"
239-
import_config "gbfs_validator.exs"
240242
import_config "mail.exs"
241243
import_config "data_sharing_pilot.exs"
242244
import_config "#{config_env()}.exs"

config/database.exs

Lines changed: 0 additions & 10 deletions
This file was deleted.

config/gbfs_validator.exs

Lines changed: 0 additions & 10 deletions
This file was deleted.

config/gtfs_validator.exs

Lines changed: 0 additions & 6 deletions
This file was deleted.

config/runtime.exs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,19 @@ config :transport,
4646
dynamic_irve_tick_interval: :timer.seconds(if(config_env() == :dev, do: 10, else: 30)),
4747
dynamic_irve_initial_sync: config_env() != :test
4848

49+
config :transport,
50+
# This endpoint is not really public but we can use it for now
51+
# See https://github.com/MobilityData/gbfs-validator/issues/53#issuecomment-957917240
52+
gbfs_validator_url:
53+
System.get_env("GBFS_VALIDATOR_URL", "https://gbfs-validator.netlify.app/.netlify/functions/validator"),
54+
gbfs_validator_website: System.get_env("GBFS_VALIDATOR_WEBSITE", "https://gbfs-validator.netlify.app")
55+
56+
# In :test, gtfs_validator_url is set in test.exs.
57+
if config_env() != :test do
58+
config :transport,
59+
gtfs_validator_url: System.get_env("GTFS_VALIDATOR_URL", "https://validation.transport.data.gouv.fr")
60+
end
61+
4962
# Inside IEx, we do not want jobs to start processing, nor plugins working.
5063
# The jobs can be heavy and for instance in production, one person could
5164
# unknowningly create duplicate RAM heavy jobs. With this trick, we can still
@@ -236,6 +249,12 @@ if config_env() == :dev do
236249
watchers: if(webserver, do: [npm: ["run", "--prefix", "apps/transport/client", "watch"]], else: [])
237250
end
238251

252+
if config_env() == :dev do
253+
config :transport, DB.Repo,
254+
url: System.get_env("PG_URL") || "ecto://postgres:postgres@localhost/transport_repo",
255+
pool_size: (System.get_env("PG_POOL_SIZE") || "10") |> String.to_integer()
256+
end
257+
239258
if config_env() == :prod do
240259
pool_size =
241260
case app_env do
@@ -262,8 +281,10 @@ if config_env() == :prod do
262281
],
263282
socket_options: [keepalive: true],
264283
# See https://hexdocs.pm/db_connection/DBConnection.html#start_link/2-queue-config
265-
# [Ecto.Repo] :pool_timeout is no longer supported in favor of a new queue system described in DBConnection.start_link/2
266-
# under "Queue config". For most users, configuring :timeout is enough, as it now includes both queue and query time
284+
# [Ecto.Repo] :pool_timeout is no longer supported in favor of a new queue system
285+
# described in DBConnection.start_link/2
286+
# under "Queue config". For most users, configuring :timeout is enough,
287+
# as it now includes both queue and query time
267288
timeout: 15_000
268289

269290
config :transport, TransportWeb.Endpoint,

config/test.exs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ config :oauth2, Authentication,
9292
client_id: "my_client_id",
9393
client_secret: "my_client_secret"
9494

95-
# Validator configuration
96-
config :transport, gtfs_validator_url: System.get_env("GTFS_VALIDATOR_URL") || "http://127.0.0.1:7878"
97-
9895
config :exvcr,
9996
vcr_cassette_library_dir: "test/fixture/cassettes",
10097
filter_request_headers: ["authorization"]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule Credo.Check.Custom.NoSystemEnvAtCompileTime do
2+
@moduledoc "Make sure to keep an env-free compilation (for Elixir releases)."
3+
use Credo.Check, base_priority: :normal, category: :refactor
4+
5+
@forbidden ~w(get_env fetch_env fetch_env!)a
6+
7+
@impl true
8+
def run(%SourceFile{filename: filename} = source_file, params \\ []) do
9+
if compile_time_config?(filename) do
10+
issue_meta = IssueMeta.for(source_file, params)
11+
Credo.Code.prewalk(source_file, &traverse(&1, &2, issue_meta))
12+
else
13+
[]
14+
end
15+
end
16+
17+
defp compile_time_config?(filename) do
18+
rel = Path.relative_to_cwd(filename)
19+
String.starts_with?(rel, "config/") and rel != "config/runtime.exs"
20+
end
21+
22+
# AST shape of any dotted call `_.<fun>(...args)`:
23+
# {{:., _, [_module, fun]}, call_meta, args}
24+
defp traverse(
25+
{{:., _, [_module, fun]}, meta, _args} = ast,
26+
issues,
27+
issue_meta
28+
)
29+
when fun in @forbidden do
30+
issue =
31+
format_issue(issue_meta,
32+
message: "#{fun} forbidden at compile time; move to config/runtime.exs",
33+
line_no: meta[:line]
34+
)
35+
36+
{ast, [issue | issues]}
37+
end
38+
39+
defp traverse(ast, issues, _issue_meta), do: {ast, issues}
40+
end

0 commit comments

Comments
 (0)