@@ -2,7 +2,6 @@ defmodule CodeCorps.GitHub.Sync do
2
2
3
3
alias CodeCorps . {
4
4
Comment ,
5
- GithubIssue ,
6
5
GitHub ,
7
6
GitHub.Utils.ResultAggregator ,
8
7
GithubPullRequest ,
@@ -108,44 +107,62 @@ defmodule CodeCorps.GitHub.Sync do
108
107
defp sync_step ( { :ok , _ } = result , _step ) , do: result
109
108
defp sync_step ( { :error , _ = error } , _step ) , do: { :error , error }
110
109
111
- @ spec mark ( GithubRepo . t , String . t ) :: { :ok , GithubRepo . t } | { :error , Changeset . t }
112
- defp mark ( % GithubRepo { } = repo , sync_state ) when is_binary ( sync_state ) do
113
- IO . puts ( "------------ #{ sync_state } -----------------" )
110
+ @ spec mark_repo ( GithubRepo . t , String . t , Keyword . t ) :: { :ok , GithubRepo . t } | { :error , Changeset . t }
111
+ defp mark_repo ( % GithubRepo { } = repo , sync_state , opts \\ [ ] ) do
112
+ params = build_sync_params ( sync_state , opts )
114
113
repo
115
- |> GithubRepo . update_sync_changeset ( % { sync_state: sync_state } )
114
+ |> GithubRepo . update_sync_changeset ( params )
116
115
|> Repo . update
117
116
end
118
- @ spec mark ( ProjectGithubRepo . t , String . t ) :: { :ok , GithubRepo . t } | { :error , Changeset . t }
119
- defp mark ( % ProjectGithubRepo { } = project_github_repo , sync_state ) when is_binary ( sync_state ) do
120
- IO . puts ( "------------#{ sync_state } -----------------" )
117
+
118
+ @ spec mark_project_repo ( ProjectGithubRepo . t , String . t , Keyword . t ) :: { :ok , GithubRepo . t } | { :error , Changeset . t }
119
+ defp mark_project_repo ( % ProjectGithubRepo { } = project_github_repo , sync_state , opts \\ [ ] ) do
120
+ params = build_sync_params ( sync_state , opts )
121
121
project_github_repo
122
- |> ProjectGithubRepo . update_sync_changeset ( % { sync_state: sync_state } )
122
+ |> ProjectGithubRepo . update_sync_changeset ( params )
123
123
|> Repo . update
124
124
end
125
125
126
+
127
+ @ count_fields [ :syncing_comments_count , :syncing_issues_count , :syncing_pull_requests_count ]
128
+
129
+ defp build_sync_params ( sync_state , opts ) do
130
+ Enum . reduce @ count_fields , % { sync_state: sync_state } , fn field , acc ->
131
+ put_sync_opt ( opts , field , acc )
132
+ end
133
+ end
134
+
135
+ defp put_sync_opt ( opts , key , map ) do
136
+ case Keyword . get ( opts , key ) do
137
+ nil -> map
138
+ count -> map |> Map . put ( key , count )
139
+ end
140
+ end
141
+
126
142
@ spec sync_repo ( GithubRepo . t ) :: { :ok , GithubRepo . t }
127
143
def sync_repo ( % GithubRepo { } = repo ) do
128
- with { :ok , pr_payloads } <- repo |> GitHub.API.Repository . pulls |> sync_step ( :fetch_pull_requests ) ,
129
- { :ok , repo } <- repo |> mark ( "syncing_pull_requests" ) ,
144
+ with { :ok , repo } <- repo |> mark_repo ( "fetching_pull_requests" ) ,
145
+ { :ok , pr_payloads } <- repo |> GitHub.API.Repository . pulls |> sync_step ( :fetch_pull_requests ) ,
146
+ { :ok , repo } <- repo |> mark_repo ( "syncing_github_pull_requests" , [ syncing_pull_requests_count: pr_payloads |> Enum . count ] ) ,
130
147
{ :ok , _pull_requests } <- pr_payloads |> Enum . map ( & Sync.PullRequest.GithubPullRequest . create_or_update_pull_request ( repo , & 1 ) ) |> ResultAggregator . aggregate |> sync_step ( :sync_pull_requests ) ,
131
- { :ok , repo } <- repo |> mark ( "fetching_issues" ) ,
148
+ { :ok , repo } <- repo |> mark_repo ( "fetching_issues" ) ,
132
149
{ :ok , issue_payloads } <- repo |> GitHub.API.Repository . issues |> sync_step ( :fetch_issues ) ,
133
- { :ok , repo } <- repo |> mark ( "syncing_issues" ) ,
150
+ { :ok , repo } <- repo |> mark_repo ( "syncing_github_issues" , [ syncing_issues_count: issue_payloads |> Enum . count ] ) ,
134
151
{ :ok , _issues } <- issue_payloads |> Enum . map ( & Sync.Issue.GithubIssue . create_or_update_issue ( repo , & 1 ) ) |> ResultAggregator . aggregate |> sync_step ( :sync_issues ) ,
135
- { :ok , repo } <- repo |> mark ( "fetching_comments" ) ,
152
+ { :ok , repo } <- repo |> mark_repo ( "fetching_comments" ) ,
136
153
{ :ok , comment_payloads } <- repo |> GitHub.API.Repository . issue_comments |> sync_step ( :fetch_comments ) ,
137
- { :ok , repo } <- repo |> mark ( "syncing_comments" ) ,
154
+ { :ok , repo } <- repo |> mark_repo ( "syncing_github_comments" , [ syncing_comments_count: comment_payloads |> Enum . count ] ) ,
138
155
{ :ok , _comments } <- comment_payloads |> Enum . map ( & Sync.Comment.GithubComment . create_or_update_comment ( repo , & 1 ) ) |> ResultAggregator . aggregate |> sync_step ( :sync_comments ) ,
139
- { :ok , repo } <- repo |> mark ( "receiving_webhooks" )
156
+ { :ok , repo } <- repo |> mark_repo ( "receiving_webhooks" )
140
157
do
141
158
{ :ok , repo }
142
159
else
143
- { :error , :fetch_pull_requests } -> repo |> mark ( "errored_fetching_pull_requests" )
144
- { :error , :sync_pull_requests } -> repo |> mark ( "errored_syncing_pull_requests" )
145
- { :error , :fetch_issues } -> repo |> mark ( "errored_fetching_issues" )
146
- { :error , :sync_issues } -> repo |> mark ( "errored_syncing_issues" )
147
- { :error , :fetch_comments } -> repo |> mark ( "errored_fetching_comments" )
148
- { :error , :sync_comments } -> repo |> mark ( "errored_syncing_comments" )
160
+ { :error , :fetch_pull_requests } -> repo |> mark_repo ( "errored_fetching_pull_requests" )
161
+ { :error , :sync_pull_requests } -> repo |> mark_repo ( "errored_syncing_pull_requests" )
162
+ { :error , :fetch_issues } -> repo |> mark_repo ( "errored_fetching_issues" )
163
+ { :error , :sync_issues } -> repo |> mark_repo ( "errored_syncing_issues" )
164
+ { :error , :fetch_comments } -> repo |> mark_repo ( "errored_fetching_comments" )
165
+ { :error , :sync_comments } -> repo |> mark_repo ( "errored_syncing_comments" )
149
166
end
150
167
end
151
168
@@ -154,20 +171,20 @@ defmodule CodeCorps.GitHub.Sync do
154
171
project_github_repo
155
172
|> preload_project_github_repo
156
173
157
- with { :ok , project_github_repo } <- project_github_repo |> mark ( "syncing_repo ") ,
174
+ with { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "syncing_github_repo ") ,
158
175
{ :ok , % GithubRepo { sync_state: "receiving_webhooks" } } <- repo |> sync_repo ( ) ,
159
176
project_github_repo <- Repo . get ( ProjectGithubRepo , project_github_repo . id ) |> preload_project_github_repo ( ) ,
160
- { :ok , project_github_repo } <- project_github_repo |> mark ( "syncing_tasks" ) ,
177
+ { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "syncing_tasks" ) ,
161
178
{ :ok , _tasks } <- project_github_repo |> Sync.Issue.Task . sync_project_github_repo ( ) |> sync_step ( :sync_tasks ) ,
162
- { :ok , project_github_repo } <- project_github_repo |> mark ( "syncing_comments" ) ,
179
+ { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "syncing_comments" ) ,
163
180
{ :ok , _comments } <- project_github_repo |> Sync.Comment.Comment . sync_project_github_repo ( ) |> sync_step ( :sync_comments ) ,
164
- { :ok , project_github_repo } <- project_github_repo |> mark ( "synced" )
181
+ { :ok , project_github_repo } <- project_github_repo |> mark_project_repo ( "synced" )
165
182
do
166
183
{ :ok , project_github_repo }
167
184
else
168
- { :ok , % GithubRepo { } } -> project_github_repo |> mark ( "errored_syncing_repo ")
169
- { :error , :sync_tasks } -> repo |> mark ( "errored_syncing_tasks" )
170
- { :error , :sync_comments } -> repo |> mark ( "errored_syncing_comments" )
185
+ { :ok , % GithubRepo { } } -> project_github_repo |> mark_project_repo ( "errored_syncing_github_repo ")
186
+ { :error , :sync_tasks } -> repo |> mark_project_repo ( "errored_syncing_tasks" )
187
+ { :error , :sync_comments } -> repo |> mark_project_repo ( "errored_syncing_comments" )
171
188
end
172
189
end
173
190
0 commit comments