From 29c50c53159333bdd124d4d3584ae826f49c28ad Mon Sep 17 00:00:00 2001
From: Patricio Cano <suprnova32@gmail.com>
Date: Wed, 29 Jun 2016 15:25:04 -0500
Subject: [PATCH] Default Git access protocol to `web`

---
 app/helpers/application_settings_helper.rb | 10 +++-------
 app/helpers/branches_helper.rb             |  2 +-
 app/helpers/button_helper.rb               |  8 ++++----
 app/models/merge_request.rb                |  2 +-
 app/services/commits/change_service.rb     |  2 +-
 app/services/files/base_service.rb         |  2 +-
 lib/gitlab/git_access.rb                   |  4 +---
 lib/gitlab/protocol_access.rb              |  4 ++--
 spec/lib/gitlab/git_access_spec.rb         |  2 +-
 9 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/app/helpers/application_settings_helper.rb b/app/helpers/application_settings_helper.rb
index 6b0dde5dfe6..92166461462 100644
--- a/app/helpers/application_settings_helper.rb
+++ b/app/helpers/application_settings_helper.rb
@@ -44,19 +44,15 @@ module ApplicationSettingsHelper
     end
   end
 
-  def enabled_project_tooltip(project, protocol)
+  def enabled_project_button(project, protocol)
     case protocol
     when 'ssh'
-      sanitize_clone_button(ssh_clone_button(project, 'bottom'))
+      ssh_clone_button(project, 'bottom', false)
     else
-      sanitize_clone_button(http_clone_button(project, 'bottom'))
+      http_clone_button(project, 'bottom', false)
     end
   end
 
-  def sanitize_clone_button(input)
-    sanitize(input, tags: %w(a), attributes: %w(id class title data-html data-container data-placement data-title data-original-title aria-describedby))
-  end
-
   # Return a group of checkboxes that use Bootstrap's button plugin for a
   # toggle button effect.
   def restricted_level_checkboxes(help_block_id)
diff --git a/app/helpers/branches_helper.rb b/app/helpers/branches_helper.rb
index 601df5c18df..c533659b600 100644
--- a/app/helpers/branches_helper.rb
+++ b/app/helpers/branches_helper.rb
@@ -12,7 +12,7 @@ module BranchesHelper
   def can_push_branch?(project, branch_name)
     return false unless project.repository.branch_exists?(branch_name)
 
-    ::Gitlab::GitAccess.new(current_user, project, 'web').can_push_to_branch?(branch_name)
+    ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(branch_name)
   end
 
   def project_branches
diff --git a/app/helpers/button_helper.rb b/app/helpers/button_helper.rb
index a64e96eaec9..7fd20d13010 100644
--- a/app/helpers/button_helper.rb
+++ b/app/helpers/button_helper.rb
@@ -40,7 +40,7 @@ module ButtonHelper
       type: :button
   end
 
-  def http_clone_button(project, placement = 'right')
+  def http_clone_button(project, placement = 'right', append_link = true)
     klass = 'http-selector'
     klass << ' has-tooltip' if current_user.try(:require_password?)
 
@@ -48,7 +48,7 @@ module ButtonHelper
 
     content_tag :a, protocol,
       class: klass,
-      href: project.http_url_to_repo,
+      href: (project.http_url_to_repo if append_link),
       data: {
         html: true,
         placement: placement,
@@ -57,13 +57,13 @@ module ButtonHelper
       }
   end
 
-  def ssh_clone_button(project, placement = 'right')
+  def ssh_clone_button(project, placement = 'right', append_link = true)
     klass = 'ssh-selector'
     klass << ' has-tooltip' if current_user.try(:require_ssh_key?)
 
     content_tag :a, 'SSH',
       class: klass,
-      href: project.ssh_url_to_repo,
+      href: (project.ssh_url_to_repo if append_link),
       data: {
         html: true,
         placement: placement,
diff --git a/app/models/merge_request.rb b/app/models/merge_request.rb
index 4f7e1d2f302..cb0f871897a 100644
--- a/app/models/merge_request.rb
+++ b/app/models/merge_request.rb
@@ -481,7 +481,7 @@ class MergeRequest < ActiveRecord::Base
   end
 
   def can_be_merged_by?(user)
-    ::Gitlab::GitAccess.new(user, project, 'web').can_push_to_branch?(target_branch)
+    ::Gitlab::GitAccess.new(user, project).can_push_to_branch?(target_branch)
   end
 
   def mergeable_ci_state?
diff --git a/app/services/commits/change_service.rb b/app/services/commits/change_service.rb
index c578097376a..6b69cb53b2c 100644
--- a/app/services/commits/change_service.rb
+++ b/app/services/commits/change_service.rb
@@ -23,7 +23,7 @@ module Commits
     private
 
     def check_push_permissions
-      allowed = ::Gitlab::GitAccess.new(current_user, project, 'web').can_push_to_branch?(@target_branch)
+      allowed = ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(@target_branch)
 
       unless allowed
         raise ValidationError.new('You are not allowed to push into this branch')
diff --git a/app/services/files/base_service.rb b/app/services/files/base_service.rb
index 4bdb68a3698..0326a8823e9 100644
--- a/app/services/files/base_service.rb
+++ b/app/services/files/base_service.rb
@@ -43,7 +43,7 @@ module Files
     end
 
     def validate
-      allowed = ::Gitlab::GitAccess.new(current_user, project, 'web').can_push_to_branch?(@target_branch)
+      allowed = ::Gitlab::GitAccess.new(current_user, project).can_push_to_branch?(@target_branch)
 
       unless allowed
         raise_error("You are not allowed to push into this branch")
diff --git a/lib/gitlab/git_access.rb b/lib/gitlab/git_access.rb
index ae609021eb6..93b75a7bb05 100644
--- a/lib/gitlab/git_access.rb
+++ b/lib/gitlab/git_access.rb
@@ -5,7 +5,7 @@ module Gitlab
 
     attr_reader :actor, :project, :protocol
 
-    def initialize(actor, project, protocol)
+    def initialize(actor, project, protocol = 'web')
       @actor    = actor
       @project  = project
       @protocol = protocol
@@ -50,8 +50,6 @@ module Gitlab
     end
 
     def check(cmd, changes = nil)
-      raise 'Access denied due to unspecified Git access protocol' unless protocol.present?
-
       return build_status_object(false, "Git access over #{protocol.upcase} is not allowed") unless protocol_allowed?
 
       unless actor
diff --git a/lib/gitlab/protocol_access.rb b/lib/gitlab/protocol_access.rb
index 4c90654c59c..21aefc884be 100644
--- a/lib/gitlab/protocol_access.rb
+++ b/lib/gitlab/protocol_access.rb
@@ -1,12 +1,12 @@
 module Gitlab
   module ProtocolAccess
     def self.allowed?(protocol)
-      if protocol.to_s == 'web'
+      if protocol == 'web'
         true
       elsif current_application_settings.enabled_git_access_protocol.blank?
         true
       else
-        protocol.to_s == current_application_settings.enabled_git_access_protocol
+        protocol == current_application_settings.enabled_git_access_protocol
       end
     end
   end
diff --git a/spec/lib/gitlab/git_access_spec.rb b/spec/lib/gitlab/git_access_spec.rb
index c79ba11f782..81530bb2db7 100644
--- a/spec/lib/gitlab/git_access_spec.rb
+++ b/spec/lib/gitlab/git_access_spec.rb
@@ -1,7 +1,7 @@
 require 'spec_helper'
 
 describe Gitlab::GitAccess, lib: true do
-  let(:access) { Gitlab::GitAccess.new(actor, project, 'web') }
+  let(:access) { Gitlab::GitAccess.new(actor, project) }
   let(:project) { create(:project) }
   let(:user) { create(:user) }
   let(:actor) { user }
-- 
GitLab