summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-02-08 19:47:11 +0000
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2014-02-08 19:47:11 +0000
commit54b892153440935a08e00c63db3ec8a607ae3565 (patch)
tree1ce9f2decee052cf17e8d3df36fff4f0bf0dd413
parent803ba84c6b5d601f867ee69b9627252cda82adbf (diff)
parentfcc906e6aa1749046b691bbd60b2020fc970bdfb (diff)
downloadgitlab-ce-54b892153440935a08e00c63db3ec8a607ae3565.tar.gz
Merge branch 'feature/submodule-links' into 'master'
Better submodule links Detect if submodule is hosted on this GitLab server, gitlab.com or github.com. Hash links directly to commit in repo.
-rw-r--r--app/helpers/submodule_helper.rb42
-rw-r--r--app/models/gollum_wiki.rb5
-rw-r--r--app/views/projects/tree/_submodule_item.html.haml9
-rw-r--r--spec/helpers/submodule_helper_spec.rb109
4 files changed, 159 insertions, 6 deletions
diff --git a/app/helpers/submodule_helper.rb b/app/helpers/submodule_helper.rb
new file mode 100644
index 00000000000..285f4081bf9
--- /dev/null
+++ b/app/helpers/submodule_helper.rb
@@ -0,0 +1,42 @@
+module SubmoduleHelper
+ include Gitlab::ShellAdapter
+
+ # links to files listing for submodule if submodule is a project on this server
+ def submodule_links(submodule_item)
+ url = submodule_item.submodule_url
+ return url, nil unless url =~ /([^\/:]+\/[^\/]+\.git)\Z/
+
+ project = $1
+ project.chomp!('.git')
+
+ if self_url?(url, project)
+ return project_path(project), project_tree_path(project, submodule_item.id)
+ elsif github_dot_com_url?(url)
+ standard_links('github.com', project, submodule_item.id)
+ elsif gitlab_dot_com_url?(url)
+ standard_links('gitlab.com', project, submodule_item.id)
+ else
+ return url, nil
+ end
+ end
+
+ protected
+
+ def github_dot_com_url?(url)
+ url =~ /github\.com[\/:][^\/]+\/[^\/]+\Z/
+ end
+
+ def gitlab_dot_com_url?(url)
+ url =~ /gitlab\.com[\/:][^\/]+\/[^\/]+\Z/
+ end
+
+ def self_url?(url, project)
+ return true if url == [ Gitlab.config.gitlab.url, '/', project, '.git' ].join('')
+ url == gitlab_shell.url_to_repo(project)
+ end
+
+ def standard_links(host, project, commit)
+ base = [ 'https://', host, '/', project ].join('')
+ return base, [ base, '/tree/', commit ].join('')
+ end
+end \ No newline at end of file
diff --git a/app/models/gollum_wiki.rb b/app/models/gollum_wiki.rb
index 7ebaaff61cb..613a7110d12 100644
--- a/app/models/gollum_wiki.rb
+++ b/app/models/gollum_wiki.rb
@@ -1,4 +1,5 @@
class GollumWiki
+ include Gitlab::ShellAdapter
MARKUPS = {
"Markdown" => :markdown,
@@ -113,10 +114,6 @@ class GollumWiki
"#{@user.username} #{action} page: #{title}"
end
- def gitlab_shell
- @gitlab_shell ||= Gitlab::Shell.new
- end
-
def path_to_repo
@path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git")
end
diff --git a/app/views/projects/tree/_submodule_item.html.haml b/app/views/projects/tree/_submodule_item.html.haml
index badc7d992bd..ae87dbde67a 100644
--- a/app/views/projects/tree/_submodule_item.html.haml
+++ b/app/views/projects/tree/_submodule_item.html.haml
@@ -1,10 +1,15 @@
+- tree, commit = submodule_links(submodule_item)
%tr{ class: "tree-item" }
%td.tree-item-file-name
= image_tag "submodule.png"
%span
- = link_to truncate(submodule_item.name, length: 40), submodule_item.submodule_url
+ = link_to truncate(submodule_item.name, length: 40), tree
@
- %span.monospace #{submodule_item.id[0..10]}
+ %span.monospace
+ - if commit.nil?
+ #{submodule_item.id[0..10]}
+ - else
+ = link_to "#{submodule_item.id[0..10]}", commit
%td
%td
%td
diff --git a/spec/helpers/submodule_helper_spec.rb b/spec/helpers/submodule_helper_spec.rb
new file mode 100644
index 00000000000..507812e5223
--- /dev/null
+++ b/spec/helpers/submodule_helper_spec.rb
@@ -0,0 +1,109 @@
+require 'spec_helper'
+
+describe SubmoduleHelper do
+ describe 'submodule links' do
+ let(:submodule_item) { double(submodule_url: '', id: 'hash') }
+ let(:config) { Gitlab.config.gitlab }
+
+ context 'submodule on self' do
+ before do
+ Gitlab.config.gitlab.stub(protocol: 'http') # set this just to be sure
+ end
+
+ it 'should detect ssh on standard port' do
+ Gitlab.config.gitlab.stub(ssh_port: 22) # set this just to be sure
+ submodule_item.stub(submodule_url: [ config.user, '@', config.host, ':gitlab-org/gitlab-ce.git' ].join(''))
+ submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
+ end
+
+ it 'should detect ssh on non-standard port' do
+ Gitlab.config.gitlab_shell.stub(ssh_port: 2222)
+ Gitlab.config.gitlab_shell.stub(ssh_path_prefix: Settings.send(:build_gitlab_shell_ssh_path_prefix))
+ submodule_item.stub(submodule_url: [ 'ssh://', config.user, '@', config.host, ':2222/gitlab-org/gitlab-ce.git' ].join(''))
+ submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
+ end
+
+ it 'should detect http on standard port' do
+ Gitlab.config.gitlab.stub(port: 80)
+ Gitlab.config.gitlab.stub(url: Settings.send(:build_gitlab_url))
+ submodule_item.stub(submodule_url: [ 'http://', config.host, '/gitlab-org/gitlab-ce.git' ].join(''))
+ submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
+ end
+
+ it 'should detect http on non-standard port' do
+ Gitlab.config.gitlab.stub(port: 3000)
+ Gitlab.config.gitlab.stub(url: Settings.send(:build_gitlab_url))
+ submodule_item.stub(submodule_url: [ 'http://', config.host, ':3000/gitlab-org/gitlab-ce.git' ].join(''))
+ submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
+ end
+
+ it 'should work with relative_url_root' do
+ Gitlab.config.gitlab.stub(port: 80) # set this just to be sure
+ Gitlab.config.gitlab.stub(relative_url_root: '/gitlab/root')
+ Gitlab.config.gitlab.stub(url: Settings.send(:build_gitlab_url))
+ submodule_item.stub(submodule_url: [ 'http://', config.host, '/gitlab/root/gitlab-org/gitlab-ce.git' ].join(''))
+ submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
+ end
+ end
+
+ context 'submodule on github.com' do
+ it 'should detect ssh' do
+ submodule_item.stub(submodule_url: 'git@github.com:gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ]
+ end
+
+ it 'should detect http' do
+ submodule_item.stub(submodule_url: 'http://github.com/gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ]
+ end
+
+ it 'should detect https' do
+ submodule_item.stub(submodule_url: 'https://github.com/gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ]
+ end
+
+ it 'should return original with non-standard url' do
+ submodule_item.stub(submodule_url: 'http://github.com/gitlab-org/gitlab-ce')
+ submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
+
+ submodule_item.stub(submodule_url: 'http://github.com/another/gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
+ end
+ end
+
+ context 'submodule on gitlab.com' do
+ it 'should detect ssh' do
+ submodule_item.stub(submodule_url: 'git@gitlab.com:gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ]
+ end
+
+ it 'should detect http' do
+ submodule_item.stub(submodule_url: 'http://gitlab.com/gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ]
+ end
+
+ it 'should detect https' do
+ submodule_item.stub(submodule_url: 'https://gitlab.com/gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ]
+ end
+
+ it 'should return original with non-standard url' do
+ submodule_item.stub(submodule_url: 'http://gitlab.com/gitlab-org/gitlab-ce')
+ submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
+
+ submodule_item.stub(submodule_url: 'http://gitlab.com/another/gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
+ end
+ end
+
+ context 'submodule on unsupported' do
+ it 'should return original' do
+ submodule_item.stub(submodule_url: 'http://mygitserver.com/gitlab-org/gitlab-ce')
+ submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
+
+ submodule_item.stub(submodule_url: 'http://mygitserver.com/gitlab-org/gitlab-ce.git')
+ submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
+ end
+ end
+ end
+end