Commit 1cc92dae authored by Guilherme Vieira's avatar Guilherme Vieira

Returns a ssh url for go-get=1

parent e1122c9f
---
title: Returns a ssh url for go-get=1
merge_request: 14990
author: gvieira37
type: fixed
......@@ -4,6 +4,7 @@ module Gitlab
module Middleware
class Go
include ActionView::Helpers::TagHelper
include Gitlab::CurrentSettings
PROJECT_PATH_REGEX = %r{\A(#{Gitlab::PathRegex.full_namespace_route_regex}/#{Gitlab::PathRegex.project_route_regex})/}.freeze
......@@ -37,10 +38,20 @@ module Gitlab
end
def go_body(path)
project_url = URI.join(Gitlab.config.gitlab.url, path)
config = Gitlab.config
project_url = URI.join(config.gitlab.url, path)
import_prefix = strip_url(project_url.to_s)
meta_tag = tag :meta, name: 'go-import', content: "#{import_prefix} git #{project_url}.git"
repository_url = case current_application_settings.enabled_git_access_protocol
when 'ssh'
shell = config.gitlab_shell
port = ":#{shell.ssh_port}" unless shell.ssh_port == 22
"ssh://#{shell.ssh_user}@#{shell.ssh_host}#{port}/#{path}.git"
when 'http', nil
"#{project_url}.git"
end
meta_tag = tag :meta, name: 'go-import', content: "#{import_prefix} git #{repository_url}"
head_tag = content_tag :head, meta_tag
content_tag :html, head_tag
end
......
......@@ -17,6 +17,7 @@ describe Gitlab::Middleware::Go do
describe 'when go-get=1' do
let(:current_user) { nil }
shared_examples 'go-get=1' do |enabled_protocol:|
context 'with simple 2-segment project path' do
let!(:project) { create(:project, :private) }
......@@ -24,7 +25,7 @@ describe Gitlab::Middleware::Go do
let(:path) { "#{project.full_path}/subpackage" }
it 'returns the full project path' do
expect_response_with_path(go, project.full_path)
expect_response_with_path(go, enabled_protocol, project.full_path)
end
end
......@@ -32,7 +33,7 @@ describe Gitlab::Middleware::Go do
let(:path) { project.full_path }
it 'returns the full project path' do
expect_response_with_path(go, project.full_path)
expect_response_with_path(go, enabled_protocol, project.full_path)
end
end
end
......@@ -44,7 +45,7 @@ describe Gitlab::Middleware::Go do
shared_examples 'a nested project' do
context 'when the project is public' do
it 'returns the full project path' do
expect_response_with_path(go, project.full_path)
expect_response_with_path(go, enabled_protocol, project.full_path)
end
end
......@@ -61,13 +62,13 @@ describe Gitlab::Middleware::Go do
end
it 'returns the full project path' do
expect_response_with_path(go, project.full_path)
expect_response_with_path(go, enabled_protocol, project.full_path)
end
end
context 'without access to the project' do
it 'returns the 2-segment group path' do
expect_response_with_path(go, group.full_path)
expect_response_with_path(go, enabled_protocol, group.full_path)
end
end
end
......@@ -103,6 +104,31 @@ describe Gitlab::Middleware::Go do
end
end
context 'with SSH disabled' do
before do
stub_application_setting(enabled_git_access_protocol: 'http')
end
include_examples 'go-get=1', enabled_protocol: :http
end
context 'with HTTP disabled' do
before do
stub_application_setting(enabled_git_access_protocol: 'ssh')
end
include_examples 'go-get=1', enabled_protocol: :ssh
end
context 'with nothing disabled' do
before do
stub_application_setting(enabled_git_access_protocol: nil)
end
include_examples 'go-get=1', enabled_protocol: nil
end
end
def go
env = {
'rack.input' => '',
......@@ -113,10 +139,16 @@ describe Gitlab::Middleware::Go do
middleware.call(env)
end
def expect_response_with_path(response, path)
def expect_response_with_path(response, protocol, path)
repository_url = case protocol
when :ssh
"ssh://git@#{Gitlab.config.gitlab.host}/#{path}.git"
when :http, nil
"http://#{Gitlab.config.gitlab.host}/#{path}.git"
end
expect(response[0]).to eq(200)
expect(response[1]['Content-Type']).to eq('text/html')
expected_body = %{<html><head><meta name="go-import" content="#{Gitlab.config.gitlab.host}/#{path} git http://#{Gitlab.config.gitlab.host}/#{path}.git" /></head></html>}
expected_body = %{<html><head><meta name="go-import" content="#{Gitlab.config.gitlab.host}/#{path} git #{repository_url}" /></head></html>}
expect(response[2].body).to eq([expected_body])
end
end
......
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