diff options
-rw-r--r-- | app/models/project.rb | 8 | ||||
-rw-r--r-- | app/services/create_project_service.rb | 21 | ||||
-rw-r--r-- | lib/api/api.rb | 1 | ||||
-rw-r--r-- | lib/api/forks.rb | 38 |
4 files changed, 62 insertions, 6 deletions
diff --git a/app/models/project.rb b/app/models/project.rb index c931c50..e534312 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -74,8 +74,12 @@ ls -la eos end - def parse(project_yaml) - project = YAML.load(project_yaml) + def parse(project_params) + project = YAML.load(project_params) + + unless project.is_a?(OpenStruct) + project = OpenStruct.new(project) + end params = { name: project.name_with_namespace, diff --git a/app/services/create_project_service.rb b/app/services/create_project_service.rb index 1d99330..f532fff 100644 --- a/app/services/create_project_service.rb +++ b/app/services/create_project_service.rb @@ -1,7 +1,7 @@ class CreateProjectService include Rails.application.routes.url_helpers - def execute(current_user, params, project_route) + def execute(current_user, params, project_route, forked_project = nil) @project = Project.parse(params) Project.transaction do @@ -13,13 +13,26 @@ class CreateProjectService project_url: project_route.gsub(":project_id", @project.id.to_s), } - if Network.new.enable_ci(current_user.url, @project.gitlab_id, opts, current_user.private_token) - true - else + unless Network.new.enable_ci(current_user.url, @project.gitlab_id, opts, current_user.private_token) raise ActiveRecord::Rollback end end + if forked_project + # Copy jobs + @project.jobs = forked_project.jobs.map do |job| + Job.new(job.attributes) + end + + # Copy settings + settings = forked_project.attributes.select do |attr_name, value| + ["public", "shared_runners_enabled", "allow_git_fetch"].include? attr_name + end + + @project.update(settings) + + end + EventService.new.create_project(current_user, @project) @project diff --git a/lib/api/api.rb b/lib/api/api.rb index a969e4b..d1127ed 100644 --- a/lib/api/api.rb +++ b/lib/api/api.rb @@ -29,5 +29,6 @@ module API mount Commits mount Runners mount Projects + mount Forks end end diff --git a/lib/api/forks.rb b/lib/api/forks.rb new file mode 100644 index 0000000..248eb3b --- /dev/null +++ b/lib/api/forks.rb @@ -0,0 +1,38 @@ +module API + class Forks < Grape::API + resource :forks do + # Create a fork + # + # Parameters: + # project_id (required) - The ID of a project + # project_token (requires) - Project token + # user_token(required) - User private token + # data (required) - GitLab push data + # + # + # Example Request: + # POST /forks + post do + required_attributes! [:project_id, :data, :project_token, :user_token] + project = Project.find_by!(gitlab_id: params[:project_id]) + authenticate_project_token!(project) + + user_session = UserSession.new + user = user_session.authenticate_by_token(private_token: params[:user_token], url: GitlabCi.config.gitlab_server.url) + + fork = CreateProjectService.new.execute( + user, + params[:data], + RoutesHelper.project_url(":project_id"), + project + ) + + if fork + present fork, with: Entities::Project + else + not_found! + end + end + end + end +end |