Skip to content
Snippets Groups Projects
Commit 0f8a3cbb authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

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
No related branches found
No related tags found
1 merge request!102Use full repository path instead of extracting name
Pipeline #
v4.0.0
- Use full repository path for API calls
v3.6.6
- Re-use the default logger when logging metrics data
 
Loading
Loading
Loading
Loading
@@ -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?
 
Loading
Loading
Loading
Loading
@@ -21,7 +21,7 @@ class GitlabNet
params = {
action: cmd,
changes: changes,
project: project_name(repo),
project: sanitize_path(repo),
protocol: protocol
}
 
Loading
Loading
@@ -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-', '')
}
 
Loading
Loading
@@ -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
 
Loading
Loading
@@ -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
Loading
Loading
Loading
Loading
@@ -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
Loading
Loading
@@ -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
Loading
Loading
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
Loading
Loading
Loading
Loading
@@ -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' }
Loading
Loading
Loading
Loading
@@ -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
 
Loading
Loading
@@ -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",
Loading
Loading
@@ -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",
Loading
Loading
@@ -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",
Loading
Loading
Loading
Loading
@@ -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' }
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment