Skip to content
Snippets Groups Projects

WIP: Project option to require allow collaboration on merge requests

4 unresolved threads
Compare and Show latest version
12 files
+ 106
46
Compare changes
  • Side-by-side
  • Inline
Files
12
+ 15
13
@@ -167,6 +167,7 @@ def check_state?(merge_status)
@@ -167,6 +167,7 @@ def check_state?(merge_status)
validate :validate_branches, unless: [:allow_broken, :importing?, :closed_without_fork?]
validate :validate_branches, unless: [:allow_broken, :importing?, :closed_without_fork?]
validate :validate_fork, unless: :closed_without_fork?
validate :validate_fork, unless: :closed_without_fork?
validate :validate_target_project, on: :create
validate :validate_target_project, on: :create
 
validate :validate_collaboration
scope :by_source_or_target_branch, ->(branch_name) do
scope :by_source_or_target_branch, ->(branch_name) do
where("source_branch = :branch OR target_branch = :branch", branch: branch_name)
where("source_branch = :branch OR target_branch = :branch", branch: branch_name)
@@ -666,18 +667,15 @@ def validate_fork
@@ -666,18 +667,15 @@ def validate_fork
end
end
def validate_collaboration
def validate_collaboration
return true unless target_project.merge_requests_require_allow_collaboration?
return true unless target_project && source_project
 
return true unless must_allow_collaboration?
return true unless for_fork?
return true unless for_fork?
return true unless source_and_target_projects_not_private?
return true unless source_and_target_projects_not_private?
return true if can_allow_collaboration?(author)
return true if can_allow_collaboration?(author)
errors.add :base, 'Target project requires merge requests to allow commits from members who can merge to the target branch'
errors.add :base, 'Target project requires merge requests to allow commits from members who can merge to the target branch'
errors.add :base, 'Source branch is protected' if protected_source_branch?
if protected_source_branch?
errors.add :base, 'Author is not allowed to push code to the source project' unless can_push_to_source_project?(author)
errors.add :base, 'Source branch is protected'
elsif !can_push_to_source_branch?
errors.add :base, 'Author is not allowed to push code to the source project'
end
end
end
def merge_ongoing?
def merge_ongoing?
@@ -834,7 +832,7 @@ def can_cancel_auto_merge?(current_user)
@@ -834,7 +832,7 @@ def can_cancel_auto_merge?(current_user)
def can_remove_source_branch?(current_user)
def can_remove_source_branch?(current_user)
!protected_source_branch? &&
!protected_source_branch? &&
!source_project.root_ref?(source_branch) &&
!source_project.root_ref?(source_branch) &&
can_push_to_source_branch? &&
can_push_to_source_project?(current_user) &&
diff_head_sha == source_branch_head.try(:sha)
diff_head_sha == source_branch_head.try(:sha)
end
end
@@ -1430,15 +1428,19 @@ def source_and_target_projects_not_private?
@@ -1430,15 +1428,19 @@ def source_and_target_projects_not_private?
def can_allow_collaboration?(user)
def can_allow_collaboration?(user)
collaborative_push_possible? &&
collaborative_push_possible? &&
can_push_to_source_branch?
can_push_to_source_project?(user)
end
end
def must_allow_collaboration?
def must_allow_collaboration?
target_project.merge_requests_require_allow_collaboration?
target_project.merge_requests_require_allow_collaboration?
end
end
def can_toggle_collaboration?(user)
def can_toggle_collaboration?(current_user)
can_allow_collaboration?(user) && !must_allow_collaboration?
can_allow_collaboration?(current_user) && !must_allow_collaboration?
 
end
 
 
def should_allow_collaboration?(current_user)
 
can_allow_collaboration?(current_user) && must_allow_collaboration?
end
end
def squash_in_progress?
def squash_in_progress?
@@ -1476,7 +1478,7 @@ def protected_source_branch?
@@ -1476,7 +1478,7 @@ def protected_source_branch?
ProtectedBranch.protected?(source_project, source_branch)
ProtectedBranch.protected?(source_project, source_branch)
end
end
def can_push_to_source_branch?
def can_push_to_source_project?(current_user)
Ability.allowed?(author, :push_code, source_project)
Ability.allowed?(current_user, :push_code, source_project)
end
end
end
end
Loading