Commit 0f8a3cbb authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'dz-support-nested-namespaces' into 'master'

Use full repository path instead of extracting name

In order to implement nested groups https://gitlab.com/gitlab-org/gitlab-ce/issues/2772 we can not rely on old path with one slash that split namespace and project name like `namespace/project.git`. Now it can be both `namespace/project.git` and `namespace/namespace/namespace/project.git`. Because of that it makes no sense to extract part of full path. Instead we just pass path to API without change and let rails application do parsing/extraction. 
Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>

See merge request !102
parents eae98b67 00bf30ba
v4.0.0
- Use full repository path for API calls
v3.6.6
- Re-use the default logger when logging metrics data
......
......@@ -9,19 +9,18 @@ class GitlabAccess
include NamesHelper
attr_reader :config, :repo_path, :repo_name, :changes, :protocol
attr_reader :config, :repo_path, :changes, :protocol
def initialize(repo_path, actor, changes, protocol)
@config = GitlabConfig.new
@repo_path = repo_path.strip
@actor = actor
@repo_name = extract_repo_name(@repo_path.dup)
@changes = changes.lines
@protocol = protocol
end
def exec
status = api.check_access('git-receive-pack', @repo_name, @actor, @changes, @protocol)
status = api.check_access('git-receive-pack', @repo_path, @actor, @changes, @protocol)
raise AccessDeniedError, status.message unless status.allowed?
......
......@@ -21,7 +21,7 @@ class GitlabNet
params = {
action: cmd,
changes: changes,
project: project_name(repo),
project: sanitize_path(repo),
protocol: protocol
}
......@@ -49,7 +49,7 @@ class GitlabNet
def lfs_authenticate(key, repo)
params = {
project: project_name(repo),
project: sanitize_path(repo),
key_id: key.gsub('key-', '')
}
......@@ -65,10 +65,10 @@ class GitlabNet
JSON.parse(resp.body) rescue {}
end
def merge_request_urls(repo_name, changes)
def merge_request_urls(repo_path, changes)
changes = changes.join("\n") unless changes.kind_of?(String)
changes = changes.encode('UTF-8', 'ASCII', invalid: :replace, replace: '')
resp = get("#{host}/merge_request_urls?project=#{URI.escape(repo_name)}&changes=#{URI.escape(changes)}")
resp = get("#{host}/merge_request_urls?project=#{URI.escape(repo_path)}&changes=#{URI.escape(changes)}")
JSON.parse(resp.body) rescue []
end
......@@ -118,10 +118,8 @@ class GitlabNet
protected
def project_name(repo)
project_name = repo.gsub("'", "")
project_name = project_name.gsub(/\.git\Z/, "")
project_name.gsub(/\A\//, "")
def sanitize_path(repo)
repo.gsub("'", "")
end
def config
......
......@@ -13,7 +13,6 @@ class GitlabPostReceive
def initialize(repo_path, actor, changes)
@config = GitlabConfig.new
@repo_path, @actor = repo_path.strip, actor
@repo_name = extract_repo_name(@repo_path.dup)
@changes = changes
@jid = SecureRandom.hex(12)
end
......@@ -29,7 +28,7 @@ class GitlabPostReceive
print_broadcast_message(broadcast_message["message"])
end
merge_request_urls = api.merge_request_urls(@repo_name, @changes)
merge_request_urls = api.merge_request_urls(@repo_path, @changes)
print_merge_request_links(merge_request_urls)
rescue GitlabNet::ApiUnreachableError
nil
......
module NamesHelper
def extract_repo_name(path)
repo_name = path.strip
repo_name.gsub!(/\.git$/, "")
repo_name.gsub!(/^\//, "")
repo_name.split(File::SEPARATOR).last(2).join(File::SEPARATOR)
end
def extract_ref_name(ref)
ref.gsub(/\Arefs\/(tags|heads)\//, '')
end
......
......@@ -22,7 +22,6 @@ describe GitlabAccess do
end
describe :initialize do
it { subject.repo_name.should == repo_name }
it { subject.repo_path.should == repo_path }
it { subject.changes.should == ['wow'] }
it { subject.protocol.should == 'ssh' }
......
......@@ -18,7 +18,7 @@ describe GitlabPostReceive do
before do
GitlabConfig.any_instance.stub(repos_path: repository_path)
GitlabNet.any_instance.stub(broadcast_message: { })
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) { [] }
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_path, wrongly_encoded_changes) { [] }
expect(Time).to receive(:now).and_return(enqueued_at)
end
......@@ -35,7 +35,7 @@ describe GitlabPostReceive do
context 'Without broad cast message' do
context 'pushing new branch' do
before do
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) do
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_path, wrongly_encoded_changes) do
[{
"branch_name" => "new_branch",
"url" => "http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch",
......@@ -62,7 +62,7 @@ describe GitlabPostReceive do
context 'pushing existing branch with merge request created' do
before do
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) do
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_path, wrongly_encoded_changes) do
[{
"branch_name" => "feature_branch",
"url" => "http://localhost/dzaporozhets/gitlab-ci/merge_requests/1",
......@@ -90,7 +90,7 @@ describe GitlabPostReceive do
context 'show broadcast message and merge request link' do
before do
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_name, wrongly_encoded_changes) do
GitlabNet.any_instance.stub(:merge_request_urls).with(repo_path, wrongly_encoded_changes) do
[{
"branch_name" => "new_branch",
"url" => "http://localhost/dzaporozhets/gitlab-ci/merge_requests/new?merge_request%5Bsource_branch%5D=new_branch",
......
......@@ -4,11 +4,6 @@ require 'names_helper'
describe NamesHelper do
include NamesHelper
describe :extract_repo_name do
it { extract_repo_name(' /opt/repos/randx/gitlab.git').should == 'randx/gitlab' }
it { extract_repo_name("/opt/repos/randx/gitlab.git\r\n").should == 'randx/gitlab' }
end
describe :extract_ref_name do
it { extract_ref_name('refs/heads/awesome-feature').should == 'awesome-feature' }
it { extract_ref_name('refs/tags/v2.2.1').should == 'v2.2.1' }
......
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