Credo: atom-or-string attrs read through one helper

4f53e42 · Gabe Morell · 2026-11-04 21:22

6 files +32 -20

Files changed

modified lib/git_gud/audit.ex
+4 −1
@@ -268,7 +268,7 @@ defmodule GitGud.Audit do
268 268 @spec repo_created(Repository.t(), map()) :: attrs()
269 269 def repo_created(%Repository{} = repo, attrs) do
270 270 host =
271 − case attrs["clone_addr"] || attrs[:clone_addr] do
271 + case attr(attrs, :clone_addr) do
272 272 url when is_binary(url) and url != "" -> URI.parse(url).host
273 273 _ -> nil
274 274 end
@@ -618,4 +618,7 @@ defmodule GitGud.Audit do
618 618
619 619 defp organization_attrs(%Organization{} = org),
620 620 do: %{organization_id: org.id, organization_label: "@#{org.handle}"}
621 +
622 + # Attrs arrive string-keyed from forms and atom-keyed from code.
623 + defp attr(attrs, key), do: attrs[Atom.to_string(key)] || attrs[key]
621 624 end
modified lib/git_gud/imports.ex
+8 −5
@@ -53,9 +53,9 @@ defmodule GitGud.Imports do
53 53 from the result before anything is created.
54 54 """
55 55 def discover(attrs, token \\ nil) do
56 − provider = attrs["provider"] || attrs[:provider]
57 − owner = trim(attrs["remote_owner"] || attrs[:remote_owner])
58 − base_url = trim(attrs["base_url"] || attrs[:base_url]) || Provider.default_base_url(provider)
56 + provider = attr(attrs, :provider)
57 + owner = trim(attr(attrs, :remote_owner))
58 + base_url = trim(attr(attrs, :base_url)) || Provider.default_base_url(provider)
59 59
60 60 with {:ok, adapter} <- Provider.adapter(provider),
61 61 :ok <- require_present(owner, :remote_owner),
@@ -81,8 +81,8 @@ defmodule GitGud.Imports do
81 81 Filter a discovered list by the batch's fork/archive preferences.
82 82 """
83 83 def filter_discovered(repos, opts) do
84 − include_forks = truthy(opts["include_forks"] || opts[:include_forks])
85 − include_archived = truthy(opts["include_archived"] || opts[:include_archived])
84 + include_forks = truthy(attr(opts, :include_forks))
85 + include_archived = truthy(attr(opts, :include_archived))
86 86
87 87 Enum.filter(repos, fn r ->
88 88 (include_forks or not r.fork) and (include_archived or not r.archived)
@@ -563,4 +563,7 @@ defmodule GitGud.Imports do
563 563 defp stringify(map) do
564 564 Map.new(map, fn {k, v} -> {to_string(k), v} end)
565 565 end
566 +
567 + # Attrs arrive string-keyed from forms and atom-keyed from code.
568 + defp attr(attrs, key), do: attrs[Atom.to_string(key)] || attrs[key]
566 569 end
modified lib/git_gud/pull_requests.ex
+11 −8
@@ -196,9 +196,9 @@ defmodule GitGud.PullRequests do
196 196 defp do_create_pull_request(%Repository{} = repo, %User{id: uid} = author, attrs, opts) do
197 197 run_id = opts[:workflow_run_id]
198 198
199 − source_ref = attrs["source_ref"] || attrs[:source_ref]
200 − target_ref = attrs["target_ref"] || attrs[:target_ref]
201 − source_repo_id = attrs["source_repository_id"] || attrs[:source_repository_id]
199 + source_ref = attr(attrs, :source_ref)
200 + target_ref = attr(attrs, :target_ref)
201 + source_repo_id = attr(attrs, :source_repository_id)
202 202
203 203 result =
204 204 with :ok <- authorize_read(author, repo, run_id),
@@ -474,8 +474,8 @@ defmodule GitGud.PullRequests do
474 474 source_actor_id: source_actor.id
475 475 }
476 476 |> PrComment.federated_changeset(%{
477 − body: Map.get(attrs, :body) || Map.get(attrs, "body"),
478 − activity_url: Map.get(attrs, :activity_url) || Map.get(attrs, "activity_url"),
477 + body: attr(attrs, :body),
478 + activity_url: attr(attrs, :activity_url),
479 479 source_actor_id: source_actor.id,
480 480 pull_request_id: pr.id
481 481 })
@@ -1472,9 +1472,9 @@ defmodule GitGud.PullRequests do
1472 1472 reviewer_id: nil
1473 1473 }
1474 1474 |> PrReview.federated_changeset(%{
1475 − state: Map.get(attrs, :state) || Map.get(attrs, "state"),
1476 − body: Map.get(attrs, :body) || Map.get(attrs, "body"),
1477 − activity_url: Map.get(attrs, :activity_url) || Map.get(attrs, "activity_url"),
1475 + state: attr(attrs, :state),
1476 + body: attr(attrs, :body),
1477 + activity_url: attr(attrs, :activity_url),
1478 1478 source_actor_id: source_actor.id,
1479 1479 pull_request_id: pr.id
1480 1480 })
@@ -1534,4 +1534,7 @@ defmodule GitGud.PullRequests do
1534 1534 end
1535 1535
1536 1536 def target_type, do: @target_type
1537 +
1538 + # Attrs arrive string-keyed from forms and atom-keyed from code.
1539 + defp attr(attrs, key), do: attrs[Atom.to_string(key)] || attrs[key]
1537 1540 end
modified lib/git_gud/repositories.ex
+9 −6
@@ -592,7 +592,7 @@ defmodule GitGud.Repositories do
592 592 # duration of the clone — they reach `git` through GIT_ASKPASS, never
593 593 # through the URL, so they are not written to the repo's git config.
594 594 defp clone_fk(fk, attrs) do
595 − case attrs["clone_addr"] || attrs[:clone_addr] do
595 + case attr(attrs, :clone_addr) do
596 596 url when is_binary(url) and url != "" ->
597 597 fk
598 598 |> Map.put(:clone_from_url, url)
@@ -604,9 +604,9 @@ defmodule GitGud.Repositories do
604 604 end
605 605
606 606 defp clone_credentials(attrs) do
607 − case attrs["clone_token"] || attrs[:clone_token] do
607 + case attr(attrs, :clone_token) do
608 608 token when is_binary(token) and token != "" ->
609 − username = attrs["clone_username"] || attrs[:clone_username] || "oauth2"
609 + username = attr(attrs, :clone_username) || "oauth2"
610 610 %{username: username, token: token}
611 611
612 612 _ ->
@@ -625,7 +625,7 @@ defmodule GitGud.Repositories do
625 625 not GitGud.Organizations.org_admin?(org, creator) ->
626 626 {:error, :forbidden}
627 627
628 − not allowed_under_org?(attrs["visibility"] || attrs[:visibility], org.visibility) ->
628 + not allowed_under_org?(attr(attrs, :visibility), org.visibility) ->
629 629 {:error, {:visibility_too_open, org.visibility}}
630 630
631 631 true ->
@@ -651,7 +651,7 @@ defmodule GitGud.Repositories do
651 651 end
652 652
653 653 defp do_create_repository(namespace, attrs, fk) do
654 − disk_path = Storage.repo_path(namespace, attrs["name"] || attrs[:name] || "")
654 + disk_path = Storage.repo_path(namespace, attr(attrs, :name) || "")
655 655
656 656 changeset =
657 657 %Repository{
@@ -1757,7 +1757,7 @@ defmodule GitGud.Repositories do
1757 1757 end
1758 1758
1759 1759 def update_repository(%Repository{} = repo, attrs) do
1760 − new_vis = attrs["visibility"] || attrs[:visibility]
1760 + new_vis = attr(attrs, :visibility)
1761 1761
1762 1762 case maybe_check_org_visibility(repo, new_vis) do
1763 1763 :ok ->
@@ -2512,4 +2512,7 @@ defmodule GitGud.Repositories do
2512 2512 paths: d.paths
2513 2513 }
2514 2514 end
2515 +
2516 + # Attrs arrive string-keyed from forms and atom-keyed from code.
2517 + defp attr(attrs, key), do: attrs[Atom.to_string(key)] || attrs[key]
2515 2518 end

Parents: 3e433f5