Read about our upcoming Code of Conduct on this issue

Commit 150c3637 authored by Jan Christophersen's avatar Jan Christophersen
Browse files

Add the username of the current user to the HTTP(S) clone URL

parent 0da3be492f6a
...@@ -34,7 +34,7 @@ def http_clone_button(project, placement = 'right', append_link: true) ...@@ -34,7 +34,7 @@ def http_clone_button(project, placement = 'right', append_link: true)
content_tag (append_link ? :a : :span), protocol, content_tag (append_link ? :a : :span), protocol,
class: klass, class: klass,
href: (project.http_url_to_repo if append_link), href: (project.http_url_to_repo(current_user) if append_link),
data: { data: {
html: true, html: true,
placement: placement, placement: placement,
......
...@@ -241,7 +241,7 @@ def default_url_to_repo(project = @project) ...@@ -241,7 +241,7 @@ def default_url_to_repo(project = @project)
when 'ssh' when 'ssh'
project.ssh_url_to_repo project.ssh_url_to_repo
else else
project.http_url_to_repo project.http_url_to_repo(current_user)
end end
end end
......
...@@ -873,8 +873,14 @@ def ssh_url_to_repo ...@@ -873,8 +873,14 @@ def ssh_url_to_repo
url_to_repo url_to_repo
end end
def http_url_to_repo def http_url_to_repo(user = nil)
"#{web_url}.git" url = web_url
if user
url.sub!(%r{\Ahttps?://}) { |protocol| "#{protocol}#{user.username}@" }
end
"#{url}.git"
end end
# Check if current branch name is marked as protected in the system # Check if current branch name is marked as protected in the system
......
---
title: Add the Username to the HTTP(S) clone URL of a Repository
merge_request: 9347
author: Jan Christophersen
...@@ -49,7 +49,7 @@ class Spinach::Features::ExploreProjects < Spinach::FeatureSteps ...@@ -49,7 +49,7 @@ class Spinach::Features::ExploreProjects < Spinach::FeatureSteps
step 'I should see an http link to the repository' do step 'I should see an http link to the repository' do
project = Project.find_by(name: 'Community') project = Project.find_by(name: 'Community')
expect(page).to have_field('project_clone', with: project.http_url_to_repo) expect(page).to have_field('project_clone', with: project.http_url_to_repo(@user))
end end
step 'I should see an ssh link to the repository' do step 'I should see an ssh link to the repository' do
......
...@@ -32,7 +32,7 @@ ...@@ -32,7 +32,7 @@
scenario 'shows only HTTP url' do scenario 'shows only HTTP url' do
visit_project visit_project
expect(page).to have_content("git clone #{project.http_url_to_repo}") expect(page).to have_content("git clone #{project.http_url_to_repo(admin)}")
expect(page).not_to have_selector('#clone-dropdown') expect(page).not_to have_selector('#clone-dropdown')
end end
end end
......
...@@ -56,8 +56,14 @@ def select_protocol(protocol) ...@@ -56,8 +56,14 @@ def select_protocol(protocol)
end end
def expect_instructions_for(protocol) def expect_instructions_for(protocol)
msg = :"#{protocol.downcase}_url_to_repo" url =
case protocol
expect(page).to have_content("git clone #{project.send(msg)}") when 'ssh'
project.ssh_url_to_repo
when 'http'
project.http_url_to_repo(developer)
end
expect(page).to have_content("git clone #{url}")
end end
end end
...@@ -1896,4 +1896,25 @@ def enable_lfs ...@@ -1896,4 +1896,25 @@ def enable_lfs
end end
end end
end end
describe '#http_url_to_repo' do
let(:project) { create :empty_project }
context 'when no user is given' do
it 'returns the url to the repo without a username' do
url = project.http_url_to_repo
expect(url).to eq(project.http_url_to_repo)
expect(url).not_to include('@')
end
end
context 'when user is given' do
it 'returns the url to the repo with the username' do
user = build_stubbed(:user)
expect(project.http_url_to_repo(user)).to match(%r{https?:\/\/#{user.username}@})
end
end
end
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