Merge branch '1937-https-clone-url-username' into 'master'

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

Closes #1937

See merge request !9347
This commit is contained in:
Rémy Coutable 2017-02-27 12:21:04 +00:00
commit c425f366bf
8 changed files with 45 additions and 8 deletions

View file

@ -34,7 +34,7 @@ module ButtonHelper
content_tag (append_link ? :a : :span), protocol,
class: klass,
href: (project.http_url_to_repo if append_link),
href: (project.http_url_to_repo(current_user) if append_link),
data: {
html: true,
placement: placement,

View file

@ -241,7 +241,7 @@ module ProjectsHelper
when 'ssh'
project.ssh_url_to_repo
else
project.http_url_to_repo
project.http_url_to_repo(current_user)
end
end

View file

@ -869,8 +869,14 @@ class Project < ActiveRecord::Base
url_to_repo
end
def http_url_to_repo
"#{web_url}.git"
def http_url_to_repo(user = nil)
url = web_url
if user
url.sub!(%r{\Ahttps?://}) { |protocol| "#{protocol}#{user.username}@" }
end
"#{url}.git"
end
# Check if current branch name is marked as protected in the system

View file

@ -0,0 +1,4 @@
---
title: Add the Username to the HTTP(S) clone URL of a Repository
merge_request: 9347
author: Jan Christophersen

View file

@ -49,7 +49,7 @@ class Spinach::Features::ExploreProjects < Spinach::FeatureSteps
step 'I should see an http link to the repository' do
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
step 'I should see an ssh link to the repository' do

View file

@ -32,7 +32,7 @@ feature 'Admin disables Git access protocol', feature: true do
scenario 'shows only HTTP url' do
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')
end
end

View file

@ -56,8 +56,14 @@ feature 'Developer views empty project instructions', feature: true do
end
def expect_instructions_for(protocol)
msg = :"#{protocol.downcase}_url_to_repo"
url =
case protocol
when 'ssh'
project.ssh_url_to_repo
when 'http'
project.http_url_to_repo(developer)
end
expect(page).to have_content("git clone #{project.send(msg)}")
expect(page).to have_content("git clone #{url}")
end
end

View file

@ -1894,4 +1894,25 @@ describe Project, models: true do
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