@@ -131,21 +131,13 @@ defmodule Algora.Workspace do
131131 preload: [ user: u ]
132132 )
133133
134- res =
135- case Repo . one ( repository_query ) do
136- % Repository { } = repository -> { :ok , repository }
137- nil -> create_repository_from_github ( token , owner , repo )
138- end
139-
140- case res do
141- { :ok , repository } -> maybe_schedule_og_image_update ( repository )
142- error -> error
134+ case Repo . one ( repository_query ) do
135+ % Repository { } = repository -> { :ok , repository }
136+ nil -> create_repository_from_github ( token , owner , repo )
143137 end
144-
145- res
146138 end
147139
148- defp maybe_schedule_og_image_update ( % Repository { } = repository ) do
140+ def maybe_schedule_og_image_update ( % Repository { } = repository ) do
149141 one_day_ago = DateTime . add ( DateTime . utc_now ( ) , - 1 , :day )
150142
151143 needs_update? =
@@ -646,19 +638,84 @@ defmodule Algora.Workspace do
646638 Repo . all ( query )
647639 end
648640
649- @ spec upsert_user_contribution ( User . t ( ) , Repository . t ( ) , integer ( ) ) ::
641+ @ spec upsert_user_contribution ( String . t ( ) , String . t ( ) , integer ( ) ) ::
650642 { :ok , UserContribution . t ( ) } | { :error , Ecto.Changeset . t ( ) }
651- def upsert_user_contribution ( % User { } = user , % Repository { } = repository , contribution_count ) do
643+ def upsert_user_contribution ( user_id , repository_id , contribution_count ) do
652644 attrs = % {
653- user_id: user . id ,
654- repository_id: repository . id ,
645+ user_id: user_id ,
646+ repository_id: repository_id ,
655647 contribution_count: contribution_count ,
656648 last_fetched_at: DateTime . utc_now ( )
657649 }
658650
659- case Repo . get_by ( UserContribution , user_id: user . id , repository_id: repository . id ) do
651+ case Repo . get_by ( UserContribution , user_id: user_id , repository_id: repository_id ) do
660652 nil -> % UserContribution { } |> UserContribution . changeset ( attrs ) |> Repo . insert ( )
661653 contribution -> contribution |> UserContribution . changeset ( attrs ) |> Repo . update ( )
662654 end
663655 end
656+
657+ def fetch_top_contributions ( provider_login ) do
658+ token = Algora.Admin . token ( )
659+
660+ with { :ok , contributions } <- Algora.Cloud . top_contributions ( provider_login ) ,
661+ { :ok , user } <- ensure_user ( token , provider_login ) ,
662+ :ok <- add_contributions ( user . id , contributions ) do
663+ { :ok , contributions }
664+ else
665+ { :error , reason } ->
666+ Logger . error ( "Failed to fetch contributions for #{ provider_login } : #{ inspect ( reason ) } " )
667+ { :error , reason }
668+ end
669+ end
670+
671+ def fetch_top_contributions_async ( provider_login ) do
672+ token = Algora.Admin . token ( )
673+
674+ with { :ok , contributions } <- Algora.Cloud . top_contributions ( provider_login ) ,
675+ { :ok , user } <- ensure_user ( token , provider_login ) ,
676+ { :ok , _ } <- add_contributions_async ( user . id , contributions ) do
677+ { :ok , nil }
678+ else
679+ { :error , reason } ->
680+ Logger . error ( "Failed to fetch contributions for #{ provider_login } : #{ inspect ( reason ) } " )
681+ { :error , reason }
682+ end
683+ end
684+
685+ def add_contributions_async ( user_id , opts ) do
686+ Repo . transact ( fn ->
687+ Enum . reduce_while ( opts , :ok , fn contribution , _ ->
688+ case % {
689+ "user_id" => user_id ,
690+ "repo_full_name" => contribution . repo_name ,
691+ "contribution_count" => contribution . contribution_count
692+ }
693+ |> Jobs.SyncContribution . new ( )
694+ |> Oban . insert ( ) do
695+ { :ok , _job } -> { :cont , :ok }
696+ error -> { :halt , error }
697+ end
698+ end )
699+ end )
700+ end
701+
702+ def add_contributions ( user_id , opts ) do
703+ results =
704+ Enum . map ( opts , fn % { repo_name: repo_name , contribution_count: contribution_count } ->
705+ add_contribution ( % { user_id: user_id , repo_full_name: repo_name , contribution_count: contribution_count } )
706+ end )
707+
708+ if Enum . any? ( results , fn result -> result == :ok end ) , do: :ok , else: { :error , :failed }
709+ end
710+
711+ def add_contribution ( % { user_id: user_id , repo_full_name: repo_full_name , contribution_count: contribution_count } ) do
712+ token = Algora.Admin . token ( )
713+
714+ with [ repo_owner , repo_name ] <- String . split ( repo_full_name , "/" ) ,
715+ { :ok , repo } <- ensure_repository ( token , repo_owner , repo_name ) ,
716+ { :ok , _tech_stack } <- ensure_repo_tech_stack ( token , repo ) ,
717+ { :ok , _contribution } <- upsert_user_contribution ( user_id , repo . id , contribution_count ) do
718+ :ok
719+ end
720+ end
664721end
0 commit comments