Commit 54b892153440935a08e00c63db3ec8a607ae3565

Authored by Dmitriy Zaporozhets
2 parents 803ba84c fcc906e6

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.
app/helpers/submodule_helper.rb 0 → 100644
... ... @@ -0,0 +1,42 @@
  1 +module SubmoduleHelper
  2 + include Gitlab::ShellAdapter
  3 +
  4 + # links to files listing for submodule if submodule is a project on this server
  5 + def submodule_links(submodule_item)
  6 + url = submodule_item.submodule_url
  7 + return url, nil unless url =~ /([^\/:]+\/[^\/]+\.git)\Z/
  8 +
  9 + project = $1
  10 + project.chomp!('.git')
  11 +
  12 + if self_url?(url, project)
  13 + return project_path(project), project_tree_path(project, submodule_item.id)
  14 + elsif github_dot_com_url?(url)
  15 + standard_links('github.com', project, submodule_item.id)
  16 + elsif gitlab_dot_com_url?(url)
  17 + standard_links('gitlab.com', project, submodule_item.id)
  18 + else
  19 + return url, nil
  20 + end
  21 + end
  22 +
  23 + protected
  24 +
  25 + def github_dot_com_url?(url)
  26 + url =~ /github\.com[\/:][^\/]+\/[^\/]+\Z/
  27 + end
  28 +
  29 + def gitlab_dot_com_url?(url)
  30 + url =~ /gitlab\.com[\/:][^\/]+\/[^\/]+\Z/
  31 + end
  32 +
  33 + def self_url?(url, project)
  34 + return true if url == [ Gitlab.config.gitlab.url, '/', project, '.git' ].join('')
  35 + url == gitlab_shell.url_to_repo(project)
  36 + end
  37 +
  38 + def standard_links(host, project, commit)
  39 + base = [ 'https://', host, '/', project ].join('')
  40 + return base, [ base, '/tree/', commit ].join('')
  41 + end
  42 +end
0 43 \ No newline at end of file
... ...
app/models/gollum_wiki.rb
1 1 class GollumWiki
  2 + include Gitlab::ShellAdapter
2 3  
3 4 MARKUPS = {
4 5 "Markdown" => :markdown,
... ... @@ -113,10 +114,6 @@ class GollumWiki
113 114 "#{@user.username} #{action} page: #{title}"
114 115 end
115 116  
116   - def gitlab_shell
117   - @gitlab_shell ||= Gitlab::Shell.new
118   - end
119   -
120 117 def path_to_repo
121 118 @path_to_repo ||= File.join(Gitlab.config.gitlab_shell.repos_path, "#{path_with_namespace}.git")
122 119 end
... ...
app/views/projects/tree/_submodule_item.html.haml
  1 +- tree, commit = submodule_links(submodule_item)
1 2 %tr{ class: "tree-item" }
2 3 %td.tree-item-file-name
3 4 = image_tag "submodule.png"
4 5 %span
5   - = link_to truncate(submodule_item.name, length: 40), submodule_item.submodule_url
  6 + = link_to truncate(submodule_item.name, length: 40), tree
6 7 @
7   - %span.monospace #{submodule_item.id[0..10]}
  8 + %span.monospace
  9 + - if commit.nil?
  10 + #{submodule_item.id[0..10]}
  11 + - else
  12 + = link_to "#{submodule_item.id[0..10]}", commit
8 13 %td
9 14 %td
10 15 %td
... ...
spec/helpers/submodule_helper_spec.rb 0 → 100644
... ... @@ -0,0 +1,109 @@
  1 +require 'spec_helper'
  2 +
  3 +describe SubmoduleHelper do
  4 + describe 'submodule links' do
  5 + let(:submodule_item) { double(submodule_url: '', id: 'hash') }
  6 + let(:config) { Gitlab.config.gitlab }
  7 +
  8 + context 'submodule on self' do
  9 + before do
  10 + Gitlab.config.gitlab.stub(protocol: 'http') # set this just to be sure
  11 + end
  12 +
  13 + it 'should detect ssh on standard port' do
  14 + Gitlab.config.gitlab.stub(ssh_port: 22) # set this just to be sure
  15 + submodule_item.stub(submodule_url: [ config.user, '@', config.host, ':gitlab-org/gitlab-ce.git' ].join(''))
  16 + submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
  17 + end
  18 +
  19 + it 'should detect ssh on non-standard port' do
  20 + Gitlab.config.gitlab_shell.stub(ssh_port: 2222)
  21 + Gitlab.config.gitlab_shell.stub(ssh_path_prefix: Settings.send(:build_gitlab_shell_ssh_path_prefix))
  22 + submodule_item.stub(submodule_url: [ 'ssh://', config.user, '@', config.host, ':2222/gitlab-org/gitlab-ce.git' ].join(''))
  23 + submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
  24 + end
  25 +
  26 + it 'should detect http on standard port' do
  27 + Gitlab.config.gitlab.stub(port: 80)
  28 + Gitlab.config.gitlab.stub(url: Settings.send(:build_gitlab_url))
  29 + submodule_item.stub(submodule_url: [ 'http://', config.host, '/gitlab-org/gitlab-ce.git' ].join(''))
  30 + submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
  31 + end
  32 +
  33 + it 'should detect http on non-standard port' do
  34 + Gitlab.config.gitlab.stub(port: 3000)
  35 + Gitlab.config.gitlab.stub(url: Settings.send(:build_gitlab_url))
  36 + submodule_item.stub(submodule_url: [ 'http://', config.host, ':3000/gitlab-org/gitlab-ce.git' ].join(''))
  37 + submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
  38 + end
  39 +
  40 + it 'should work with relative_url_root' do
  41 + Gitlab.config.gitlab.stub(port: 80) # set this just to be sure
  42 + Gitlab.config.gitlab.stub(relative_url_root: '/gitlab/root')
  43 + Gitlab.config.gitlab.stub(url: Settings.send(:build_gitlab_url))
  44 + submodule_item.stub(submodule_url: [ 'http://', config.host, '/gitlab/root/gitlab-org/gitlab-ce.git' ].join(''))
  45 + submodule_links(submodule_item).should == [ project_path('gitlab-org/gitlab-ce'), project_tree_path('gitlab-org/gitlab-ce', 'hash') ]
  46 + end
  47 + end
  48 +
  49 + context 'submodule on github.com' do
  50 + it 'should detect ssh' do
  51 + submodule_item.stub(submodule_url: 'git@github.com:gitlab-org/gitlab-ce.git')
  52 + submodule_links(submodule_item).should == [ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ]
  53 + end
  54 +
  55 + it 'should detect http' do
  56 + submodule_item.stub(submodule_url: 'http://github.com/gitlab-org/gitlab-ce.git')
  57 + submodule_links(submodule_item).should == [ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ]
  58 + end
  59 +
  60 + it 'should detect https' do
  61 + submodule_item.stub(submodule_url: 'https://github.com/gitlab-org/gitlab-ce.git')
  62 + submodule_links(submodule_item).should == [ 'https://github.com/gitlab-org/gitlab-ce', 'https://github.com/gitlab-org/gitlab-ce/tree/hash' ]
  63 + end
  64 +
  65 + it 'should return original with non-standard url' do
  66 + submodule_item.stub(submodule_url: 'http://github.com/gitlab-org/gitlab-ce')
  67 + submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
  68 +
  69 + submodule_item.stub(submodule_url: 'http://github.com/another/gitlab-org/gitlab-ce.git')
  70 + submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
  71 + end
  72 + end
  73 +
  74 + context 'submodule on gitlab.com' do
  75 + it 'should detect ssh' do
  76 + submodule_item.stub(submodule_url: 'git@gitlab.com:gitlab-org/gitlab-ce.git')
  77 + submodule_links(submodule_item).should == [ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ]
  78 + end
  79 +
  80 + it 'should detect http' do
  81 + submodule_item.stub(submodule_url: 'http://gitlab.com/gitlab-org/gitlab-ce.git')
  82 + submodule_links(submodule_item).should == [ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ]
  83 + end
  84 +
  85 + it 'should detect https' do
  86 + submodule_item.stub(submodule_url: 'https://gitlab.com/gitlab-org/gitlab-ce.git')
  87 + submodule_links(submodule_item).should == [ 'https://gitlab.com/gitlab-org/gitlab-ce', 'https://gitlab.com/gitlab-org/gitlab-ce/tree/hash' ]
  88 + end
  89 +
  90 + it 'should return original with non-standard url' do
  91 + submodule_item.stub(submodule_url: 'http://gitlab.com/gitlab-org/gitlab-ce')
  92 + submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
  93 +
  94 + submodule_item.stub(submodule_url: 'http://gitlab.com/another/gitlab-org/gitlab-ce.git')
  95 + submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
  96 + end
  97 + end
  98 +
  99 + context 'submodule on unsupported' do
  100 + it 'should return original' do
  101 + submodule_item.stub(submodule_url: 'http://mygitserver.com/gitlab-org/gitlab-ce')
  102 + submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
  103 +
  104 + submodule_item.stub(submodule_url: 'http://mygitserver.com/gitlab-org/gitlab-ce.git')
  105 + submodule_links(submodule_item).should == [ submodule_item.submodule_url, nil ]
  106 + end
  107 + end
  108 + end
  109 +end
... ...