@@ -200,22 +200,45 @@ defmodule Codebattle.ExternalPlatform do
200200 def get_invite ( _ ) , do: { :error , :invalid_invite_id }
201201
202202 @ doc """
203- Forks a repository into the target organization.
204- POST /repos /{org_slug}/{repo_slug}/fork
203+ Creates a repository in the target organization from a template repository .
204+ POST /orgs /{org_slug}/repos
205205 """
206- @ spec fork_repo ( String . t ( ) , String . t ( ) , keyword ( ) ) :: { :ok , map ( ) } | { :error , term ( ) }
207- def fork_repo ( repo_slug , target_org_slug , opts \\ [ ] ) do
208- source_org_slug = Keyword . get ( opts , :source_org_slug , default_org_slug ( ) )
206+ @ spec create_repo_from_template ( String . t ( ) , keyword ( ) ) :: { :ok , map ( ) } | { :error , term ( ) }
207+ def create_repo_from_template ( target_org_slug , opts \\ [ ] ) do
208+ slug = opts [ :slug ]
209+ template_id = opts [ :template_id ]
209210
210- body = % { org_slug: target_org_slug }
211- body = if opts [ :slug ] , do: Map . put ( body , :slug , opts [ :slug ] ) , else: body
211+ cond do
212+ ! is_binary ( target_org_slug ) || String . trim ( target_org_slug ) == "" ->
213+ { :error , :invalid_org_slug }
212214
213- body =
214- if Keyword . has_key? ( opts , :default_branch_only ) ,
215- do: Map . put ( body , :default_branch_only , opts [ :default_branch_only ] ) ,
216- else: body
215+ ! is_binary ( slug ) || String . trim ( slug ) == "" ->
216+ { :error , :invalid_repo_slug }
217+
218+ ! is_binary ( template_id ) || String . trim ( template_id ) == "" ->
219+ { :error , :invalid_template_id }
220+
221+ true ->
222+ do_create_repo_from_template ( String . trim ( target_org_slug ) , opts )
223+ end
224+ end
217225
218- url = "#{ external_platform_service_url ( ) } /repos/#{ source_org_slug } /#{ repo_slug } /fork"
226+ defp do_create_repo_from_template ( target_org_slug , opts ) do
227+ slug = String . trim ( opts [ :slug ] )
228+ template_id = String . trim ( opts [ :template_id ] )
229+
230+ body =
231+ % {
232+ name: Keyword . get ( opts , :name , slug ) ,
233+ slug: slug ,
234+ description: opts [ :description ] ,
235+ visibility: Keyword . get ( opts , :visibility , "public" ) ,
236+ templating_options: % { template_id: template_id }
237+ }
238+ |> Enum . reject ( fn { _key , value } -> is_nil ( value ) end )
239+ |> Map . new ( )
240+
241+ url = "#{ external_platform_service_url ( ) } /orgs/#{ target_org_slug } /repos"
219242
220243 req_opts =
221244 Keyword . merge (
@@ -225,7 +248,7 @@ defmodule Codebattle.ExternalPlatform do
225248 receive_timeout: @ invite_timeout_ms
226249 )
227250
228- Logger . info ( "ExternalPlatform.fork_repo START method=POST url=#{ url } body=#{ inspect ( body ) } " )
251+ Logger . info ( "ExternalPlatform.create_repo_from_template START method=POST url=#{ url } body=#{ inspect ( body ) } " )
229252
230253 started_at = System . monotonic_time ( :millisecond )
231254 result = safe_request ( :post , url , req_opts )
@@ -234,20 +257,22 @@ defmodule Codebattle.ExternalPlatform do
234257 case result do
235258 { :ok , % { status: status , body: resp_body } } when status in [ 200 , 201 ] ->
236259 Logger . info (
237- "ExternalPlatform.fork_repo OK url=#{ url } status=#{ status } duration_ms=#{ duration_ms } body=#{ inspect ( resp_body ) } "
260+ "ExternalPlatform.create_repo_from_template OK url=#{ url } status=#{ status } duration_ms=#{ duration_ms } body=#{ inspect ( resp_body ) } "
238261 )
239262
240263 { :ok , resp_body }
241264
242265 { :ok , % { status: status , body: resp_body } } ->
243266 Logger . warning (
244- "ExternalPlatform.fork_repo FAIL url=#{ url } status=#{ status } duration_ms=#{ duration_ms } body=#{ inspect ( resp_body ) } "
267+ "ExternalPlatform.create_repo_from_template FAIL url=#{ url } status=#{ status } duration_ms=#{ duration_ms } body=#{ inspect ( resp_body ) } "
245268 )
246269
247270 { :error , resp_body }
248271
249272 { :error , reason } ->
250- Logger . warning ( "ExternalPlatform.fork_repo ERROR url=#{ url } duration_ms=#{ duration_ms } reason=#{ inspect ( reason ) } " )
273+ Logger . warning (
274+ "ExternalPlatform.create_repo_from_template ERROR url=#{ url } duration_ms=#{ duration_ms } reason=#{ inspect ( reason ) } "
275+ )
251276
252277 { :error , reason }
253278 end
0 commit comments