@@ -12,7 +12,6 @@ defmodule CodeCorps.GitHub.Sync do
1212 GithubRepo ,
1313 GitHub.Sync ,
1414 GitHub.Sync.Utils.RepoFinder ,
15- ProjectGithubRepo ,
1615 Repo
1716 }
1817
@@ -134,14 +133,6 @@ defmodule CodeCorps.GitHub.Sync do
134133 |> Repo . update
135134 end
136135
137- @ spec mark_project_repo ( ProjectGithubRepo . t , String . t , Keyword . t ) :: { :ok , GithubRepo . t } | { :error , Changeset . t }
138- defp mark_project_repo ( % ProjectGithubRepo { } = project_github_repo , sync_state , opts \\ [ ] ) do
139- params = build_sync_params ( sync_state , opts )
140- project_github_repo
141- |> ProjectGithubRepo . update_sync_changeset ( params )
142- |> Repo . update
143- end
144-
145136 @ count_fields [
146137 :syncing_comments_count ,
147138 :syncing_issues_count ,
@@ -181,9 +172,15 @@ defmodule CodeCorps.GitHub.Sync do
181172 - Fetches the comments from the API
182173 - Creates or updates `GithubComment` records (and their related `GithubUser`
183174 records)
175+ - Creates or updates `User` records for the `GithubUser` records
176+ - Creates or updates `Task` records, and relates them to any related
177+ `GithubIssue` and `User` records created previously
178+ - Creates or updates `Comment` records, and relates them to any related
179+ `GithubComment` and `User` records created previously
184180 """
185181 @ spec sync_repo ( GithubRepo . t ) :: { :ok , GithubRepo . t }
186182 def sync_repo ( % GithubRepo { } = repo ) do
183+ repo = preload_github_repo ( repo )
187184 with { :ok , repo } <- repo |> mark_repo ( "fetching_pull_requests" ) ,
188185 { :ok , pr_payloads } <- repo |> GitHub.API.Repository . pulls |> sync_step ( :fetch_pull_requests ) ,
189186 { :ok , repo } <- repo |> mark_repo ( "syncing_github_pull_requests" , [ syncing_pull_requests_count: pr_payloads |> Enum . count ] ) ,
@@ -196,7 +193,14 @@ defmodule CodeCorps.GitHub.Sync do
196193 { :ok , comment_payloads } <- repo |> GitHub.API.Repository . issue_comments |> sync_step ( :fetch_comments ) ,
197194 { :ok , repo } <- repo |> mark_repo ( "syncing_github_comments" , [ syncing_comments_count: comment_payloads |> Enum . count ] ) ,
198195 { :ok , _comments } <- comment_payloads |> Enum . map ( & Sync.Comment.GithubComment . create_or_update_comment ( repo , & 1 ) ) |> ResultAggregator . aggregate |> sync_step ( :sync_comments ) ,
199- { :ok , repo } <- repo |> mark_repo ( "receiving_webhooks" )
196+ repo <- Repo . get ( GithubRepo , repo . id ) |> preload_github_repo ( ) ,
197+ { :ok , repo } <- repo |> mark_repo ( "syncing_users" ) ,
198+ { :ok , _users } <- repo |> Sync.User.User . sync_github_repo ( ) |> sync_step ( :sync_users ) ,
199+ { :ok , repo } <- repo |> mark_repo ( "syncing_tasks" ) ,
200+ { :ok , _tasks } <- repo |> Sync.Issue.Task . sync_github_repo ( ) |> sync_step ( :sync_tasks ) ,
201+ { :ok , repo } <- repo |> mark_repo ( "syncing_comments" ) ,
202+ { :ok , _comments } <- repo |> Sync.Comment.Comment . sync_github_repo ( ) |> sync_step ( :sync_comments ) ,
203+ { :ok , repo } <- repo |> mark_repo ( "synced" )
200204 do
201205 { :ok , repo }
202206 else
@@ -206,51 +210,20 @@ defmodule CodeCorps.GitHub.Sync do
206210 { :error , :sync_issues } -> repo |> mark_repo ( "errored_syncing_issues" )
207211 { :error , :fetch_comments } -> repo |> mark_repo ( "errored_fetching_comments" )
208212 { :error , :sync_comments } -> repo |> mark_repo ( "errored_syncing_comments" )
213+ { :error , :sync_users } -> repo |> mark_repo ( "errored_syncing_users" )
214+ { :error , :sync_tasks } -> repo |> mark_repo ( "errored_syncing_tasks" )
215+ { :error , :sync_comments } -> repo |> mark_repo ( "errored_syncing_comments" )
209216 end
210217 end
211218
212- @ doc ~S"""
213- Syncs a `ProjectGithubRepo` with Code Corps.
214-
215- Fetches and syncs records from the GitHub API for a given project's GitHub
216- repository, marking progress of the sync state along the way.
217-
218- - Finds the `GithubRepo` and syncs it with Github using `sync_repo/1`
219- - Creates or updates `User` records for the `GithubUser` records
220- - Creates or updates `Task` records, and relates them to any related
221- `GithubIssue` and `User` records created previously
222- - Creates or updates `Comment` records, and relates them to any related
223- `GithubComment` and `User` records created previously
224- """
225- @ spec sync_project_github_repo ( ProjectGithubRepo . t ) :: { :ok , ProjectGithubRepo . t }
226- def sync_project_github_repo ( % ProjectGithubRepo { } = project_github_repo ) do
227- % ProjectGithubRepo { github_repo: % GithubRepo { } = repo } = project_github_repo =
228- project_github_repo
229- |> preload_project_github_repo
230-
231- with { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "syncing_github_repo" ) ,
232- { :ok , % GithubRepo { sync_state: "receiving_webhooks" } } <- repo |> sync_repo ( ) ,
233- project_github_repo <- Repo . get ( ProjectGithubRepo , project_github_repo . id ) |> preload_project_github_repo ( ) ,
234- { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "syncing_users" ) ,
235- { :ok , _users } <- project_github_repo |> Sync.User.User . sync_project_github_repo ( ) |> sync_step ( :sync_users ) ,
236- { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "syncing_tasks" ) ,
237- { :ok , _tasks } <- project_github_repo |> Sync.Issue.Task . sync_project_github_repo ( ) |> sync_step ( :sync_tasks ) ,
238- { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "syncing_comments" ) ,
239- { :ok , _comments } <- project_github_repo |> Sync.Comment.Comment . sync_project_github_repo ( ) |> sync_step ( :sync_comments ) ,
240- { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "synced" )
241- do
242- { :ok , project_github_repo }
243- else
244- { :ok , % GithubRepo { } } -> project_github_repo |> mark_project_repo ( "errored_syncing_github_repo" )
245- { :error , :sync_users } -> repo |> mark_project_repo ( "errored_syncing_users" )
246- { :error , :sync_tasks } -> repo |> mark_project_repo ( "errored_syncing_tasks" )
247- { :error , :sync_comments } -> repo |> mark_project_repo ( "errored_syncing_comments" )
248- end
249- end
250-
251- defp preload_project_github_repo ( % ProjectGithubRepo { } = project_github_repo ) do
252- project_github_repo
253- |> Repo . preload ( [ :project , github_repo: [ :github_app_installation , [ github_comments: [ :github_issue , :github_user ] , github_issues: [ :github_comments , :github_user ] ] ] ] )
219+ defp preload_github_repo ( % GithubRepo { } = github_repo ) do
220+ github_repo
221+ |> Repo . preload ( [
222+ :github_app_installation ,
223+ :project ,
224+ github_comments: [ :github_issue , :github_user ] ,
225+ github_issues: [ :github_comments , :github_user ]
226+ ] )
254227 end
255228
256229 @ spec marshall_result ( tuple ) :: tuple
0 commit comments