summaryrefslogtreecommitdiff
path: root/app/services
diff options
context:
space:
mode:
authorDouwe Maan <douwe@gitlab.com>2015-12-24 21:19:17 +0100
committerDouwe Maan <douwe@gitlab.com>2015-12-24 21:19:17 +0100
commitef8b1dbf21a90f719c2e8b8c052e16f6107193c6 (patch)
tree6138768916e7b65a7955e8e8c4ed1eb1541e8ced /app/services
parent32543f3bd94b1b66dd949b0fb1f57bff3732eb45 (diff)
parente3befaed82f9aa52c79a1d4c437fe4fc63f8d07a (diff)
downloadgitlab-ce-ef8b1dbf21a90f719c2e8b8c052e16f6107193c6.tar.gz
Merge branch 'master' into milestone-ref
Diffstat (limited to 'app/services')
-rw-r--r--app/services/base_service.rb5
-rw-r--r--app/services/create_branch_service.rb19
-rw-r--r--app/services/files/base_service.rb26
-rw-r--r--app/services/files/create_service.rb2
-rw-r--r--app/services/projects/update_service.rb28
5 files changed, 53 insertions, 27 deletions
diff --git a/app/services/base_service.rb b/app/services/base_service.rb
index f00ec7408b6..b48ca67d4d2 100644
--- a/app/services/base_service.rb
+++ b/app/services/base_service.rb
@@ -39,10 +39,7 @@ class BaseService
def deny_visibility_level(model, denied_visibility_level = nil)
denied_visibility_level ||= model.visibility_level
- level_name = 'Unknown'
- Gitlab::VisibilityLevel.options.each do |name, level|
- level_name = name if level == denied_visibility_level
- end
+ level_name = Gitlab::VisibilityLevel.level_name(denied_visibility_level)
model.errors.add(
:visibility_level,
diff --git a/app/services/create_branch_service.rb b/app/services/create_branch_service.rb
index de18f3bc556..f139872c728 100644
--- a/app/services/create_branch_service.rb
+++ b/app/services/create_branch_service.rb
@@ -1,10 +1,10 @@
require_relative 'base_service'
class CreateBranchService < BaseService
- def execute(branch_name, ref)
+ def execute(branch_name, ref, source_project: @project)
valid_branch = Gitlab::GitRefValidator.validate(branch_name)
if valid_branch == false
- return error('Branch name invalid')
+ return error('Branch name is invalid')
end
repository = project.repository
@@ -13,7 +13,20 @@ class CreateBranchService < BaseService
return error('Branch already exists')
end
- new_branch = repository.add_branch(current_user, branch_name, ref)
+ new_branch = nil
+ if source_project != @project
+ repository.with_tmp_ref do |tmp_ref|
+ repository.fetch_ref(
+ source_project.repository.path_to_repo,
+ "refs/heads/#{ref}",
+ tmp_ref
+ )
+
+ new_branch = repository.add_branch(current_user, branch_name, tmp_ref)
+ end
+ else
+ new_branch = repository.add_branch(current_user, branch_name, ref)
+ end
if new_branch
push_data = build_push_data(project, current_user, new_branch)
diff --git a/app/services/files/base_service.rb b/app/services/files/base_service.rb
index 9a67b160940..0326a8823e9 100644
--- a/app/services/files/base_service.rb
+++ b/app/services/files/base_service.rb
@@ -3,8 +3,10 @@ module Files
class ValidationError < StandardError; end
def execute
- @current_branch = params[:current_branch]
+ @source_project = params[:source_project] || @project
+ @source_branch = params[:source_branch]
@target_branch = params[:target_branch]
+
@commit_message = params[:commit_message]
@file_path = params[:file_path]
@file_content = if params[:file_content_encoding] == 'base64'
@@ -16,8 +18,8 @@ module Files
# Validate parameters
validate
- # Create new branch if it different from current_branch
- if @target_branch != @current_branch
+ # Create new branch if it different from source_branch
+ if different_branch?
create_target_branch
end
@@ -26,18 +28,14 @@ module Files
else
error("Something went wrong. Your changes were not committed")
end
- rescue Repository::CommitError, GitHooksService::PreReceiveError, ValidationError => ex
+ rescue Repository::CommitError, Gitlab::Git::Repository::InvalidBlobName, GitHooksService::PreReceiveError, ValidationError => ex
error(ex.message)
end
private
- def current_branch
- @current_branch ||= params[:current_branch]
- end
-
- def target_branch
- @target_branch ||= params[:target_branch]
+ def different_branch?
+ @source_branch != @target_branch || @source_project != @project
end
def raise_error(message)
@@ -52,11 +50,11 @@ module Files
end
unless project.empty_repo?
- unless repository.branch_names.include?(@current_branch)
+ unless @source_project.repository.branch_names.include?(@source_branch)
raise_error("You can only create or edit files when you are on a branch")
end
- if @current_branch != @target_branch
+ if different_branch?
if repository.branch_names.include?(@target_branch)
raise_error("Branch with such name already exists. You need to switch to this branch in order to make changes")
end
@@ -65,10 +63,10 @@ module Files
end
def create_target_branch
- result = CreateBranchService.new(project, current_user).execute(@target_branch, @current_branch)
+ result = CreateBranchService.new(project, current_user).execute(@target_branch, @source_branch, source_project: @source_project)
unless result[:status] == :success
- raise_error("Something went wrong when we tried to create #{@target_branch} for you")
+ raise_error("Something went wrong when we tried to create #{@target_branch} for you: #{result[:message]}")
end
end
end
diff --git a/app/services/files/create_service.rb b/app/services/files/create_service.rb
index 2348920cc58..e4cde4a2fd8 100644
--- a/app/services/files/create_service.rb
+++ b/app/services/files/create_service.rb
@@ -26,7 +26,7 @@ module Files
unless project.empty_repo?
@file_path.slice!(0) if @file_path.start_with?('/')
- blob = repository.blob_at_branch(@current_branch, @file_path)
+ blob = repository.blob_at_branch(@source_branch, @file_path)
if blob
raise_error("Your changes could not be committed because a file with the same name already exists")
diff --git a/app/services/projects/update_service.rb b/app/services/projects/update_service.rb
index 69bdd045ddf..895e089bea3 100644
--- a/app/services/projects/update_service.rb
+++ b/app/services/projects/update_service.rb
@@ -3,12 +3,16 @@ module Projects
def execute
# check that user is allowed to set specified visibility_level
new_visibility = params[:visibility_level]
- if new_visibility && new_visibility.to_i != project.visibility_level
- unless can?(current_user, :change_visibility_level, project) &&
- Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
- deny_visibility_level(project, new_visibility)
- return project
+ if new_visibility
+ if new_visibility.to_i != project.visibility_level
+ unless can?(current_user, :change_visibility_level, project) &&
+ Gitlab::VisibilityLevel.allowed_for?(current_user, new_visibility)
+ deny_visibility_level(project, new_visibility)
+ return project
+ end
end
+
+ return false unless visibility_level_allowed?(new_visibility)
end
new_branch = params[:default_branch]
@@ -23,5 +27,19 @@ module Projects
end
end
end
+
+ private
+
+ def visibility_level_allowed?(level)
+ return true if project.visibility_level_allowed?(level)
+
+ level_name = Gitlab::VisibilityLevel.level_name(level)
+ project.errors.add(
+ :visibility_level,
+ "#{level_name} could not be set as visibility level of this project - parent project settings are more restrictive"
+ )
+
+ false
+ end
end
end