Commit 00bf30ba authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Use full repository path for API calls instead of extracting name

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