From ce05e679d2784c675614e8657803dd082c40c313 Mon Sep 17 00:00:00 2001 From: Tomas Pinkas Date: Wed, 20 May 2026 20:32:54 +0200 Subject: [PATCH] fix: group profile contributions by repository --- lib/algora_web/live/user/profile_live.ex | 26 +++++------ .../live/user_profile_live_test.exs | 44 +++++++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) create mode 100644 test/algora_web/live/user_profile_live_test.exs diff --git a/lib/algora_web/live/user/profile_live.ex b/lib/algora_web/live/user/profile_live.ex index 67ebc4707..910de5564 100644 --- a/lib/algora_web/live/user/profile_live.ex +++ b/lib/algora_web/live/user/profile_live.ex @@ -234,28 +234,22 @@ defmodule AlgoraWeb.User.ProfileLive do <% end %> <% else %>

Top Contributions

- <%= for {owner, contributions} <- aggregate_contributions(@contributions) do %> + <%= for {repo, contributions} <- aggregate_contributions(@contributions) do %> <.link - href={"https://github.com/#{owner.provider_login}/#{List.first(contributions).repository.name}/pulls?q=author%3A#{@user.provider_login}+is%3Amerged+"} + href={"https://github.com/#{repo.user.provider_login}/#{repo.name}/pulls?q=author%3A#{@user.provider_login}+is%3Amerged+"} target="_blank" rel="noopener" class="flex items-center gap-3 rounded-xl pr-2 bg-card/50 border border-border/50 hover:border-border transition-all group" > {owner.name}
- - {if owner.type == :organization do - owner.name - else - List.first(contributions).repository.name - end} - - <%= if tech = List.first(List.first(contributions).repository.tech_stack) do %> + {repo.name} + <%= if tech = List.first(repo.tech_stack) do %> <.tech_badge variant="ghost" class="saturate-0 text-[11px] group-hover:saturate-100 transition-all" @@ -267,7 +261,7 @@ defmodule AlgoraWeb.User.ProfileLive do <.icon name="tabler-star-filled" class="h-4 w-4 mr-1" /> {Algora.Util.format_number_compact( - max(owner.stargazers_count, total_stars(contributions)) + max(repo.user.stargazers_count, total_stars(contributions)) )} @@ -327,11 +321,11 @@ defmodule AlgoraWeb.User.ProfileLive do end defp aggregate_contributions(contributions) do - groups = Enum.group_by(contributions, fn c -> c.repository.user end) + groups = Enum.group_by(contributions, fn c -> c.repository.id end) contributions - |> Enum.map(fn c -> {c.repository.user, groups[c.repository.user]} end) - |> Enum.uniq_by(fn {owner, _} -> owner.id end) + |> Enum.map(fn c -> {c.repository, groups[c.repository.id]} end) + |> Enum.uniq_by(fn {repo, _} -> repo.id end) end defp total_stars(contributions) do diff --git a/test/algora_web/live/user_profile_live_test.exs b/test/algora_web/live/user_profile_live_test.exs new file mode 100644 index 000000000..474e1441b --- /dev/null +++ b/test/algora_web/live/user_profile_live_test.exs @@ -0,0 +1,44 @@ +defmodule AlgoraWeb.User.ProfileLiveTest do + use AlgoraWeb.ConnCase, async: true + + import Algora.Factory + import Phoenix.LiveViewTest + + alias Algora.Repo + alias Algora.Workspace.UserContribution + + test "lists top contributions per repository instead of collapsing them by owner", %{conn: conn} do + user = insert!(:user, handle: "dev-user", provider_login: "dev-user") + org = insert!(:organization, provider_login: "acme-org", stargazers_count: 50) + + repo_one = insert!(:repository, user: org, name: "repo-one", tech_stack: ["Elixir"], stargazers_count: 10) + repo_two = insert!(:repository, user: org, name: "repo-two", tech_stack: ["Rust"], stargazers_count: 20) + + Repo.insert!( + UserContribution.changeset(%UserContribution{}, %{ + user_id: user.id, + repository_id: repo_one.id, + contribution_count: 3, + last_fetched_at: DateTime.utc_now(), + status: :initial + }) + ) + + Repo.insert!( + UserContribution.changeset(%UserContribution{}, %{ + user_id: user.id, + repository_id: repo_two.id, + contribution_count: 5, + last_fetched_at: DateTime.utc_now(), + status: :initial + }) + ) + + {:ok, _view, html} = live(conn, "/#{user.handle}") + + assert html =~ "repo-one" + assert html =~ "repo-two" + assert html =~ "https://github.com/acme-org/repo-one/pulls?q=author%3Adev-user+is%3Amerged+" + assert html =~ "https://github.com/acme-org/repo-two/pulls?q=author%3Adev-user+is%3Amerged+" + end +end