Commit 96080fce authored by Rémy Coutable's avatar Rémy Coutable

Merge branch '58583-confidential-mr-branch-backend' into 'master'

Support creating an MR/branch on a fork from an issue

See merge request gitlab-org/gitlab-ce!29831
parents 0e8b76e1 6b68acbf
...@@ -68,8 +68,9 @@ class Projects::BranchesController < Projects::ApplicationController ...@@ -68,8 +68,9 @@ class Projects::BranchesController < Projects::ApplicationController
success = (result[:status] == :success) success = (result[:status] == :success)
if params[:issue_iid] && success if params[:issue_iid] && success
issue = IssuesFinder.new(current_user, project_id: @project.id).find_by(iid: params[:issue_iid]) target_project = confidential_issue_project || @project
SystemNoteService.new_issue_branch(issue, @project, current_user, branch_name) if issue issue = IssuesFinder.new(current_user, project_id: target_project.id).find_by(iid: params[:issue_iid])
SystemNoteService.new_issue_branch(issue, target_project, current_user, branch_name, branch_project: @project) if issue
end end
respond_to do |format| respond_to do |format|
...@@ -166,4 +167,15 @@ class Projects::BranchesController < Projects::ApplicationController ...@@ -166,4 +167,15 @@ class Projects::BranchesController < Projects::ApplicationController
@branches = Kaminari.paginate_array(@branches).page(params[:page]) @branches = Kaminari.paginate_array(@branches).page(params[:page])
end end
end end
def confidential_issue_project
return unless Feature.enabled?(:create_confidential_merge_request, @project)
return if params[:confidential_issue_project_id].blank?
confidential_issue_project = Project.find(params[:confidential_issue_project_id])
return unless can?(current_user, :update_issue, confidential_issue_project)
confidential_issue_project
end
end end
...@@ -172,6 +172,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -172,6 +172,7 @@ class Projects::IssuesController < Projects::ApplicationController
def create_merge_request def create_merge_request
create_params = params.slice(:branch_name, :ref).merge(issue_iid: issue.iid) create_params = params.slice(:branch_name, :ref).merge(issue_iid: issue.iid)
create_params[:target_project_id] = params[:target_project_id] if Feature.enabled?(:create_confidential_merge_request, @project)
result = ::MergeRequests::CreateFromIssueService.new(project, current_user, create_params).execute result = ::MergeRequests::CreateFromIssueService.new(project, current_user, create_params).execute
if result[:status] == :success if result[:status] == :success
......
...@@ -6,17 +6,20 @@ module MergeRequests ...@@ -6,17 +6,20 @@ module MergeRequests
# branch - the name of new branch # branch - the name of new branch
# ref - the source of new branch. # ref - the source of new branch.
@branch_name = params[:branch_name] @branch_name = params[:branch_name]
@issue_iid = params[:issue_iid] @issue_iid = params[:issue_iid]
@ref = params[:ref] @ref = params[:ref]
@target_project_id = params[:target_project_id]
super(project, user) super(project, user)
end end
def execute def execute
return error('Project not found') if target_project.blank?
return error('Not allowed to create merge request') unless can_create_merge_request?
return error('Invalid issue iid') unless @issue_iid.present? && issue.present? return error('Invalid issue iid') unless @issue_iid.present? && issue.present?
result = CreateBranchService.new(project, current_user).execute(branch_name, ref) result = CreateBranchService.new(target_project, current_user).execute(branch_name, ref)
return result if result[:status] == :error return result if result[:status] == :error
new_merge_request = create(merge_request) new_merge_request = create(merge_request)
...@@ -26,7 +29,7 @@ module MergeRequests ...@@ -26,7 +29,7 @@ module MergeRequests
success(new_merge_request) success(new_merge_request)
else else
SystemNoteService.new_issue_branch(issue, project, current_user, branch_name) SystemNoteService.new_issue_branch(issue, project, current_user, branch_name, branch_project: target_project)
error(new_merge_request.errors) error(new_merge_request.errors)
end end
...@@ -34,6 +37,10 @@ module MergeRequests ...@@ -34,6 +37,10 @@ module MergeRequests
private private
def can_create_merge_request?
can?(current_user, :create_merge_request_from, target_project)
end
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def issue def issue
@issue ||= IssuesFinder.new(current_user, project_id: project.id).find_by(iid: @issue_iid) @issue ||= IssuesFinder.new(current_user, project_id: project.id).find_by(iid: @issue_iid)
...@@ -45,21 +52,21 @@ module MergeRequests ...@@ -45,21 +52,21 @@ module MergeRequests
end end
def ref def ref
return @ref if project.repository.branch_exists?(@ref) return @ref if target_project.repository.branch_exists?(@ref)
project.default_branch || 'master' target_project.default_branch || 'master'
end end
def merge_request def merge_request
MergeRequests::BuildService.new(project, current_user, merge_request_params).execute MergeRequests::BuildService.new(target_project, current_user, merge_request_params).execute
end end
def merge_request_params def merge_request_params
{ {
issue_iid: @issue_iid, issue_iid: @issue_iid,
source_project_id: project.id, source_project_id: target_project.id,
source_branch: branch_name, source_branch: branch_name,
target_project_id: project.id, target_project_id: target_project.id,
target_branch: ref target_branch: ref
} }
end end
...@@ -67,5 +74,14 @@ module MergeRequests ...@@ -67,5 +74,14 @@ module MergeRequests
def success(merge_request) def success(merge_request)
super().merge(merge_request: merge_request) super().merge(merge_request: merge_request)
end end
def target_project
@target_project ||=
if @target_project_id.present?
project.forks.find_by_id(@target_project_id)
else
project
end
end
end end
end end
...@@ -404,8 +404,9 @@ module SystemNoteService ...@@ -404,8 +404,9 @@ module SystemNoteService
# Example note text: # Example note text:
# #
# "created branch `201-issue-branch-button`" # "created branch `201-issue-branch-button`"
def new_issue_branch(issue, project, author, branch) def new_issue_branch(issue, project, author, branch, branch_project: nil)
link = url_helpers.project_compare_path(project, from: project.default_branch, to: branch) branch_project ||= project
link = url_helpers.project_compare_path(branch_project, from: branch_project.default_branch, to: branch)
body = "created branch [`#{branch}`](#{link}) to address this issue" body = "created branch [`#{branch}`](#{link}) to address this issue"
...@@ -413,7 +414,7 @@ module SystemNoteService ...@@ -413,7 +414,7 @@ module SystemNoteService
end end
def new_merge_request(issue, project, author, merge_request) def new_merge_request(issue, project, author, merge_request)
body = "created merge request #{merge_request.to_reference} to address this issue" body = "created merge request #{merge_request.to_reference(project)} to address this issue"
create_note(NoteSummary.new(issue, project, author, body, action: 'merge')) create_note(NoteSummary.new(issue, project, author, body, action: 'merge'))
end end
......
...@@ -92,7 +92,7 @@ describe Projects::BranchesController do ...@@ -92,7 +92,7 @@ describe Projects::BranchesController do
end end
it 'posts a system note' do it 'posts a system note' do
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, "1-feature-branch") expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, "1-feature-branch", branch_project: project)
post :create, post :create,
params: { params: {
...@@ -103,6 +103,75 @@ describe Projects::BranchesController do ...@@ -103,6 +103,75 @@ describe Projects::BranchesController do
} }
end end
context 'confidential_issue_project_id is present' do
let(:confidential_issue_project) { create(:project) }
def create_branch_with_confidential_issue_project
post(
:create,
params: {
namespace_id: project.namespace,
project_id: project,
branch_name: branch,
confidential_issue_project_id: confidential_issue_project.id,
issue_iid: issue.iid
}
)
end
context 'create_confidential_merge_request feature is enabled' do
before do
stub_feature_flags(create_confidential_merge_request: true)
end
context 'user cannot update issue' do
let(:issue) { create(:issue, project: confidential_issue_project) }
it 'does not post a system note' do
expect(SystemNoteService).not_to receive(:new_issue_branch)
create_branch_with_confidential_issue_project
end
end
context 'user can update issue' do
before do
confidential_issue_project.add_reporter(user)
end
context 'issue is under the specified project' do
let(:issue) { create(:issue, project: confidential_issue_project) }
it 'posts a system note' do
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, confidential_issue_project, user, "1-feature-branch", branch_project: project)
create_branch_with_confidential_issue_project
end
end
context 'issue is not under the specified project' do
it 'does not post a system note' do
expect(SystemNoteService).not_to receive(:new_issue_branch)
create_branch_with_confidential_issue_project
end
end
end
end
context 'create_confidential_merge_request feature is disabled' do
before do
stub_feature_flags(create_confidential_merge_request: false)
end
it 'posts a system note on project' do
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, "1-feature-branch", branch_project: project)
create_branch_with_confidential_issue_project
end
end
end
context 'repository-less project' do context 'repository-less project' do
let(:project) { create :project } let(:project) { create :project }
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe Projects::IssuesController do describe Projects::IssuesController do
include ProjectForksHelper
let(:project) { create(:project) } let(:project) { create(:project) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:issue) { create(:issue, project: project) } let(:issue) { create(:issue, project: project) }
...@@ -1130,6 +1132,7 @@ describe Projects::IssuesController do ...@@ -1130,6 +1132,7 @@ describe Projects::IssuesController do
end end
describe 'POST create_merge_request' do describe 'POST create_merge_request' do
let(:target_project_id) { nil }
let(:project) { create(:project, :repository, :public) } let(:project) { create(:project, :repository, :public) }
before do before do
...@@ -1163,13 +1166,42 @@ describe Projects::IssuesController do ...@@ -1163,13 +1166,42 @@ describe Projects::IssuesController do
expect(response).to have_gitlab_http_status(404) expect(response).to have_gitlab_http_status(404)
end end
context 'target_project_id is set' do
let(:target_project) { fork_project(project, user, repository: true) }
let(:target_project_id) { target_project.id }
context 'create_confidential_merge_request feature is enabled' do
before do
stub_feature_flags(create_confidential_merge_request: true)
end
it 'creates a new merge request' do
expect { create_merge_request }.to change(target_project.merge_requests, :count).by(1)
end
end
context 'create_confidential_merge_request feature is disabled' do
before do
stub_feature_flags(create_confidential_merge_request: false)
end
it 'creates a new merge request' do
expect { create_merge_request }.to change(project.merge_requests, :count).by(1)
end
end
end
def create_merge_request def create_merge_request
post :create_merge_request, params: { post(
namespace_id: project.namespace.to_param, :create_merge_request,
project_id: project.to_param, params: {
id: issue.to_param namespace_id: project.namespace.to_param,
}, project_id: project.to_param,
format: :json id: issue.to_param,
target_project_id: target_project_id
},
format: :json
)
end end
end end
......
...@@ -454,16 +454,32 @@ describe SystemNoteService do ...@@ -454,16 +454,32 @@ describe SystemNoteService do
end end
describe '.new_issue_branch' do describe '.new_issue_branch' do
subject { described_class.new_issue_branch(noteable, project, author, "1-mepmep") } let(:branch) { '1-mepmep' }
it_behaves_like 'a system note' do subject { described_class.new_issue_branch(noteable, project, author, branch, branch_project: branch_project) }
let(:action) { 'branch' }
end
context 'when a branch is created from the new branch button' do shared_examples_for 'a system note for new issue branch' do
it 'sets the note text' do it_behaves_like 'a system note' do
expect(subject.note).to start_with("created branch [`1-mepmep`]") let(:action) { 'branch' }
end end
context 'when a branch is created from the new branch button' do
it 'sets the note text' do
expect(subject.note).to start_with("created branch [`#{branch}`]")
end
end
end
context 'branch_project is set' do
let(:branch_project) { create(:project, :repository) }
it_behaves_like 'a system note for new issue branch'
end
context 'branch_project is not set' do
let(:branch_project) { nil }
it_behaves_like 'a system note for new issue branch'
end end
end end
...@@ -477,7 +493,7 @@ describe SystemNoteService do ...@@ -477,7 +493,7 @@ describe SystemNoteService do
end end
it 'sets the new merge request note text' do it 'sets the new merge request note text' do
expect(subject.note).to eq("created merge request #{merge_request.to_reference} to address this issue") expect(subject.note).to eq("created merge request #{merge_request.to_reference(project)} to address this issue")
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