summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-08-13 11:33:14 +0300
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-08-13 11:33:14 +0300
commitb6ec27d941ab7d2674d5479f196b9b647ff246dc (patch)
tree64b75e3653ce26e1fa82990761d7fa3cca23d1e1
parent59bffe6b1ed6ed425723cf6d98259bdee9f75f0a (diff)
downloadgitlab-shell-b6ec27d941ab7d2674d5479f196b9b647ff246dc.tar.gz
Fix invalid repo_name/repo_path parsing by GitlabUpdate
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
-rw-r--r--.gitignore1
-rw-r--r--lib/gitlab_update.rb20
-rw-r--r--lib/names_helper.rb13
-rw-r--r--spec/gitlab_update_spec.rb25
-rw-r--r--spec/names_helper_spec.rb18
5 files changed, 65 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore
index 5eb0e6e..ad77bf5 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@ tmp/*
*.log
/*.log.*
authorized_keys.lock
+coverage/
diff --git a/lib/gitlab_update.rb b/lib/gitlab_update.rb
index 3857346..cd7a1e5 100644
--- a/lib/gitlab_update.rb
+++ b/lib/gitlab_update.rb
@@ -1,23 +1,19 @@
require_relative 'gitlab_init'
require_relative 'gitlab_net'
+require_relative 'names_helper'
require 'json'
class GitlabUpdate
- attr_reader :config
+ include NamesHelper
+
+ attr_reader :config, :repo_path, :repo_name,
+ :ref, :ref_name, :oldrev, :newrev
def initialize(repo_path, actor, ref)
@config = GitlabConfig.new
-
- @repo_path = repo_path.strip
- @repo_name = @repo_path
- @repo_name.gsub!(config.repos_path.to_s, "")
- @repo_name.gsub!(/\.git$/, "")
- @repo_name.gsub!(/^\//, "")
-
- @actor = actor
- @ref = ref
- @ref_name = ref.gsub(/\Arefs\/(tags|heads)\//, '')
-
+ @repo_path, @actor, @ref = repo_path.strip, actor, ref
+ @repo_name = extract_repo_name(@repo_path.dup, config.repos_path.to_s)
+ @ref_name = extract_ref_name(ref)
@oldrev = ARGV[1]
@newrev = ARGV[2]
end
diff --git a/lib/names_helper.rb b/lib/names_helper.rb
new file mode 100644
index 0000000..efad56f
--- /dev/null
+++ b/lib/names_helper.rb
@@ -0,0 +1,13 @@
+module NamesHelper
+ def extract_repo_name(path, base)
+ repo_name = path.strip
+ repo_name.gsub!(base, "")
+ repo_name.gsub!(/\.git$/, "")
+ repo_name.gsub!(/^\//, "")
+ repo_name
+ end
+
+ def extract_ref_name(ref)
+ ref.gsub(/\Arefs\/(tags|heads)\//, '')
+ end
+end
diff --git a/spec/gitlab_update_spec.rb b/spec/gitlab_update_spec.rb
new file mode 100644
index 0000000..580d8c9
--- /dev/null
+++ b/spec/gitlab_update_spec.rb
@@ -0,0 +1,25 @@
+require 'spec_helper'
+require 'gitlab_update'
+
+describe GitlabUpdate do
+ let(:repository_path) { "/home/git/repositories" }
+ let(:repo_name) { 'dzaporozhets/gitlab-ci' }
+ let(:repo_path) { File.join(repository_path, repo_name) + ".git" }
+ let(:ref) { 'refs/heads/awesome-feature' }
+ let(:gitlab_update) { GitlabUpdate.new(repo_path, 'key-123', ref) }
+
+ before do
+ ARGV[1] = 'd1e3ca3b25'
+ ARGV[2] = 'c2b3653b25'
+ GitlabConfig.any_instance.stub(repos_path: repository_path)
+ end
+
+ describe :initialize do
+ it { gitlab_update.repo_name.should == repo_name }
+ it { gitlab_update.repo_path.should == repo_path }
+ it { gitlab_update.ref.should == ref }
+ it { gitlab_update.ref_name.should == 'awesome-feature' }
+ it { gitlab_update.oldrev.should == 'd1e3ca3b25' }
+ it { gitlab_update.newrev.should == 'c2b3653b25' }
+ end
+end
diff --git a/spec/names_helper_spec.rb b/spec/names_helper_spec.rb
new file mode 100644
index 0000000..db2a692
--- /dev/null
+++ b/spec/names_helper_spec.rb
@@ -0,0 +1,18 @@
+require 'spec_helper'
+require 'gitlab_update'
+
+describe NamesHelper do
+ include NamesHelper
+
+ describe :extract_repo_name do
+ it { extract_repo_name(' /opt/repos/randx/gitlab.git', '/opt/repos').should == 'randx/gitlab' }
+ it { extract_repo_name("/opt/repos/randx/gitlab.git\r\n", '/opt/repos/').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' }
+ it { extract_ref_name('refs/tags/releases/v2.2.1').should == 'releases/v2.2.1' }
+ end
+end
+