Skip to content

Commit a5ddfca

Browse files
committed
style: fix credo issues in prs diff helpers
Extracted do_render_file_diff/6, emit_diff_or_json/5, and collect_diffs/5 to reduce nesting depth and cyclomatic complexity in the new unified diff code.
1 parent 9797a4b commit a5ddfca

1 file changed

Lines changed: 54 additions & 40 deletions

File tree

lib/ado_cli/cli/pull_requests.ex

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -762,36 +762,50 @@ defmodule AdoCli.CLI.PullRequests do
762762
"No change matches --file '#{file}'. Use 'ado prs diff' (no flags) to list files."}
763763

764764
change ->
765-
case fetch_iteration_data(parsed, iteration_id) do
766-
{:ok, iteration} ->
767-
case fetch_file_diff(parsed, iteration, file) do
768-
{:ok, content} ->
769-
if json? do
770-
IO.puts(
771-
JSON.encode!(%{
772-
ok: true,
773-
iteration: iteration_id,
774-
path: change_path(change),
775-
change_type: change_type(change),
776-
diff: content
777-
})
778-
)
779-
else
780-
IO.puts(content)
781-
end
782-
783-
:ok
784-
785-
{:error, reason} ->
786-
bail(reason, parsed)
787-
end
765+
do_render_file_diff(parsed, iteration_id, change, file, json?)
766+
end
767+
end
768+
769+
defp do_render_file_diff(parsed, iteration_id, change, file, json?) do
770+
case fetch_iteration_data(parsed, iteration_id) do
771+
{:ok, iteration} ->
772+
case fetch_file_diff(parsed, iteration, file) do
773+
{:ok, content} ->
774+
emit_diff_or_json(
775+
json?,
776+
iteration_id,
777+
change_path(change),
778+
change_type(change),
779+
content
780+
)
781+
782+
:ok
788783

789784
{:error, reason} ->
790785
bail(reason, parsed)
791786
end
787+
788+
{:error, reason} ->
789+
bail(reason, parsed)
792790
end
793791
end
794792

793+
defp emit_diff_or_json(true, iteration_id, path, type, content) do
794+
IO.puts(
795+
JSON.encode!(%{
796+
ok: true,
797+
iteration: iteration_id,
798+
path: path,
799+
change_type: type,
800+
diff: content
801+
})
802+
)
803+
end
804+
805+
defp emit_diff_or_json(false, _iteration_id, _path, _type, content) do
806+
IO.puts(content)
807+
end
808+
795809
# --unified: emit the full diff between source and target commits.
796810
defp render_unified(parsed, iteration_id, changes, json?) do
797811
case fetch_iteration_data(parsed, iteration_id) do
@@ -863,7 +877,6 @@ defmodule AdoCli.CLI.PullRequests do
863877
if !base || !target do
864878
{:error, "Iteration is missing sourceRefCommit or targetRefCommit"}
865879
else
866-
# Get the list of changed files, then fetch each one's diff
867880
changes_path = "/#{project}/_apis/git/repositories/#{repo_id}/diffs/commits"
868881

869882
params = %{
@@ -875,22 +888,7 @@ defmodule AdoCli.CLI.PullRequests do
875888

876889
case Client.get(changes_path, params) do
877890
{:ok, %{"changes" => changes}} when is_list(changes) ->
878-
diffs =
879-
Enum.map(changes, fn ch ->
880-
path = get_in(ch, ["item", "path"])
881-
_old_id = get_in(ch, ["item", "originalObjectId"])
882-
_new_id = get_in(ch, ["item", "objectId"])
883-
884-
with {:ok, old_content} <- fetch_file_content(project, repo_id, path, base),
885-
{:ok, new_content} <- fetch_file_content(project, repo_id, path, target) do
886-
format_unified_diff(path, old_content, new_content, base, target)
887-
else
888-
_ -> nil
889-
end
890-
end)
891-
|> Enum.reject(&is_nil/1)
892-
|> Enum.join("\n")
893-
891+
diffs = collect_diffs(changes, project, repo_id, base, target)
894892
{:ok, diffs}
895893

896894
_ ->
@@ -899,6 +897,22 @@ defmodule AdoCli.CLI.PullRequests do
899897
end
900898
end
901899

900+
defp collect_diffs(changes, project, repo_id, base, target) do
901+
changes
902+
|> Enum.map(fn ch ->
903+
path = get_in(ch, ["item", "path"])
904+
905+
with {:ok, old_content} <- fetch_file_content(project, repo_id, path, base),
906+
{:ok, new_content} <- fetch_file_content(project, repo_id, path, target) do
907+
format_unified_diff(path, old_content, new_content, base, target)
908+
else
909+
_ -> nil
910+
end
911+
end)
912+
|> Enum.reject(&is_nil/1)
913+
|> Enum.join("\n")
914+
end
915+
902916
defp fetch_file_content(project, repo_id, path, commit_id) do
903917
content_path = "/#{project}/_apis/git/repositories/#{repo_id}/items"
904918

0 commit comments

Comments
 (0)