CI forking: tests
This commit is contained in:
parent
9c2301808b
commit
bafec400f1
|
@ -18,6 +18,8 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
class GitlabCiService < CiService
|
class GitlabCiService < CiService
|
||||||
|
API_PREFIX = "api/v1"
|
||||||
|
|
||||||
prop_accessor :project_url, :token
|
prop_accessor :project_url, :token
|
||||||
validates :project_url, presence: true, if: :activated?
|
validates :project_url, presence: true, if: :activated?
|
||||||
validates :token, presence: true, if: :activated?
|
validates :token, presence: true, if: :activated?
|
||||||
|
@ -59,7 +61,7 @@ class GitlabCiService < CiService
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_fork(new_project, user_token)
|
def fork_registration(new_project, private_token)
|
||||||
params = {
|
params = {
|
||||||
id: new_project.id,
|
id: new_project.id,
|
||||||
name_with_namespace: new_project.name_with_namespace,
|
name_with_namespace: new_project.name_with_namespace,
|
||||||
|
@ -69,12 +71,12 @@ class GitlabCiService < CiService
|
||||||
}
|
}
|
||||||
|
|
||||||
HTTParty.post(
|
HTTParty.post(
|
||||||
register_fork_path,
|
fork_registration_path,
|
||||||
body: {
|
body: {
|
||||||
project_id: project.id,
|
project_id: project.id,
|
||||||
project_token: token,
|
project_token: token,
|
||||||
user_token: user_token,
|
private_token: private_token,
|
||||||
data: params.to_yaml},
|
data: params },
|
||||||
verify: false
|
verify: false
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
@ -95,10 +97,6 @@ class GitlabCiService < CiService
|
||||||
project_url + "?ref=" + project.default_branch
|
project_url + "?ref=" + project.default_branch
|
||||||
end
|
end
|
||||||
|
|
||||||
def register_fork_path
|
|
||||||
project_url.sub(/projects\/\d*/, 'api/v1/forks')
|
|
||||||
end
|
|
||||||
|
|
||||||
def status_img_path
|
def status_img_path
|
||||||
project_url + "/status.png?ref=" + project.default_branch
|
project_url + "/status.png?ref=" + project.default_branch
|
||||||
end
|
end
|
||||||
|
@ -121,4 +119,10 @@ class GitlabCiService < CiService
|
||||||
{ type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3' }
|
{ type: 'text', name: 'project_url', placeholder: 'http://ci.gitlabhq.com/projects/3' }
|
||||||
]
|
]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def fork_registration_path
|
||||||
|
project_url.sub(/projects\/\d*/, "#{API_PREFIX}/forks")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -50,7 +50,7 @@ module Projects
|
||||||
end
|
end
|
||||||
|
|
||||||
if @from_project.gitlab_ci?
|
if @from_project.gitlab_ci?
|
||||||
ForkRegistratorWorker.perform_async(@from_project.id, project.id, @current_user.private_token)
|
ForkRegistrationWorker.perform_async(@from_project.id, project.id, @current_user.private_token)
|
||||||
end
|
end
|
||||||
rescue => ex
|
rescue => ex
|
||||||
project.errors.add(:base, 'Fork transaction failed.')
|
project.errors.add(:base, 'Fork transaction failed.')
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
class ForkRegistratorWorker
|
class ForkRegistrationWorker
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
|
|
||||||
sidekiq_options queue: :default
|
sidekiq_options queue: :default
|
||||||
|
@ -7,6 +7,6 @@ class ForkRegistratorWorker
|
||||||
from_project = Project.find(from_project_id)
|
from_project = Project.find(from_project_id)
|
||||||
to_project = Project.find(to_project_id)
|
to_project = Project.find(to_project_id)
|
||||||
|
|
||||||
from_project.gitlab_ci_service.register_fork(to_project, private_token)
|
from_project.gitlab_ci_service.fork_registration(to_project, private_token)
|
||||||
end
|
end
|
||||||
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")}
|
it { expect(@service.build_page("2ab7834c", 'master')).to eq("http://ci.gitlab.org/projects/2/refs/master/commits/2ab7834c")}
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
|
@ -40,6 +40,17 @@ describe Projects::ForkService do
|
||||||
expect(@to_project.errors[:base]).not_to include("Fork transaction failed.")
|
expect(@to_project.errors[:base]).not_to include("Fork transaction failed.")
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
describe :fork_to_namespace do
|
describe :fork_to_namespace do
|
||||||
|
@ -89,7 +100,8 @@ describe Projects::ForkService do
|
||||||
|
|
||||||
def fork_project(from_project, user, fork_success = true, params = {})
|
def fork_project(from_project, user, fork_success = true, params = {})
|
||||||
context = Projects::ForkService.new(from_project, user, 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.stub(gitlab_shell: shell)
|
||||||
context.execute
|
context.execute
|
||||||
end
|
end
|
||||||
|
|
|
@ -0,0 +1,10 @@
|
||||||
|
|
||||||
|
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
|
Loading…
Reference in New Issue