Commit 1ff40a79 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'ci_fork' into 'master'

Project fork on CI side

https://dev.gitlab.org/gitlab/gitlab-ci/issues/187

!!! GitLab CI side MR - https://gitlab.com/gitlab-org/gitlab-ci/merge_requests/56

See merge request !499
parents f3f85602 e28bc41f
......@@ -64,6 +64,7 @@ v 7.10.0 (unreleased)
- Fixed link paths for HTTP and SSH on the admin project view (Jeremy Maziarz)
- Fix and improve help rendering (Sullivan Sénéchal)
- Fix final line in EmailsOnPush email diff being rendered as error.
- Authometic setup GitLab CI project for forks if origin project has GitLab CI enabled
v 7.9.3
- Contains no changes
......
......@@ -18,6 +18,8 @@
#
class GitlabCiService < CiService
API_PREFIX = "api/v1"
prop_accessor :project_url, :token
validates :project_url, presence: true, if: :activated?
validates :token, presence: true, if: :activated?
......@@ -59,6 +61,26 @@ class GitlabCiService < CiService
end
end
def fork_registration(new_project, private_token)
params = {
id: new_project.id,
name_with_namespace: new_project.name_with_namespace,
web_url: new_project.web_url,
default_branch: new_project.default_branch,
ssh_url_to_repo: new_project.ssh_url_to_repo
}
HTTParty.post(
fork_registration_path,
body: {
project_id: project.id,
project_token: token,
private_token: private_token,
data: params },
verify: false
)
end
def commit_coverage(sha, ref)
response = get_ci_build(sha, ref)
......@@ -97,4 +119,10 @@ class GitlabCiService < CiService
{ type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3' }
]
end
private
def fork_registration_path
project_url.sub(/projects\/\d*/, "#{API_PREFIX}/forks")
end
end
......@@ -40,12 +40,18 @@ module Projects
if project.save
project.team << [@current_user, :master]
end
#Now fork the repo
unless gitlab_shell.fork_repository(@from_project.path_with_namespace, project.namespace.path)
raise 'forking failed in gitlab-shell'
end
project.ensure_satellite_exists
end
if @from_project.gitlab_ci?
ForkRegistrationWorker.perform_async(@from_project.id, project.id, @current_user.private_token)
end
rescue => ex
project.errors.add(:base, 'Fork transaction failed.')
project.destroy
......
class ForkRegistrationWorker
include Sidekiq::Worker
sidekiq_options queue: :default
def perform(from_project_id, to_project_id, private_token)
from_project = Project.find(from_project_id)
to_project = Project.find(to_project_id)
from_project.gitlab_ci_service.fork_registration(to_project, private_token)
end
end
......@@ -46,4 +46,25 @@ describe GitlabCiService do
it { expect(@service.build_page("2ab7834c", 'master')).to eq("http://ci.gitlab.org/projects/2/refs/master/commits/2ab7834c")}
end
end
describe "Fork registration" do
before do
@old_project = create(:empty_project)
@project = create(:empty_project)
@user = create(:user)
@service = GitlabCiService.new
@service.stub(
service_hook: true,
project_url: 'http://ci.gitlab.org/projects/2',
token: 'verySecret',
project: @old_project
)
end
it "performs http reuquest to ci" do
stub_request(:post, "http://ci.gitlab.org/api/v1/forks")
@service.fork_registration(@project, @user.private_token)
end
end
end
......@@ -40,6 +40,17 @@ describe Projects::ForkService do
expect(@to_project.errors[:base]).not_to include("Fork transaction failed.")
end
end
context 'GitLab CI is enabled' do
it "calls fork registrator for CI" do
@from_project.build_missing_services
@from_project.gitlab_ci_service.update_attributes(active: true)
expect(ForkRegistrationWorker).to receive(:perform_async)
fork_project(@from_project, @to_user)
end
end
end
describe :fork_to_namespace do
......@@ -89,7 +100,8 @@ describe Projects::ForkService do
def fork_project(from_project, user, fork_success = true, params = {})
context = Projects::ForkService.new(from_project, user, params)
shell = double('gitlab_shell').stub(fork_repository: fork_success)
shell = double('gitlab_shell')
shell.stub(fork_repository: fork_success)
context.stub(gitlab_shell: shell)
context.execute
end
......
require 'spec_helper'
describe ForkRegistrationWorker do
context "as a resque worker" do
it "reponds to #perform" do
expect(ForkRegistrationWorker.new).to respond_to(:perform)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment