2018-12-13 14:17:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-06-24 11:08:50 -04:00
|
|
|
RSpec.describe Suggestions::ApplyService do
|
2018-12-13 14:17:19 -05:00
|
|
|
include ProjectForksHelper
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def build_position(**optional_args)
|
|
|
|
args = { old_path: "files/ruby/popen.rb",
|
|
|
|
new_path: "files/ruby/popen.rb",
|
|
|
|
old_line: nil,
|
|
|
|
new_line: 9,
|
|
|
|
diff_refs: merge_request.diff_refs,
|
|
|
|
**optional_args }
|
|
|
|
|
|
|
|
Gitlab::Diff::Position.new(args)
|
2019-05-28 03:12:29 -04:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def create_suggestion(args)
|
|
|
|
position_args = args.slice(:old_path, :new_path, :old_line, :new_line)
|
|
|
|
content_args = args.slice(:from_content, :to_content)
|
|
|
|
|
|
|
|
position = build_position(position_args)
|
|
|
|
|
|
|
|
diff_note = create(:diff_note_on_merge_request,
|
|
|
|
noteable: merge_request,
|
|
|
|
position: position,
|
|
|
|
project: project)
|
|
|
|
|
|
|
|
suggestion_args = { note: diff_note }.merge(content_args)
|
|
|
|
|
|
|
|
create(:suggestion, :content_from_repo, suggestion_args)
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply(suggestions)
|
|
|
|
result = apply_service.new(user, *suggestions).execute
|
2019-03-15 13:35:34 -04:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
suggestions.map { |suggestion| suggestion.reload }
|
2019-03-15 13:35:34 -04:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'successfully creates commit and updates suggestions' do
|
|
|
|
it 'updates the files with the new content' do
|
|
|
|
apply(suggestions)
|
2019-03-15 13:35:34 -04:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
suggestions.each do |suggestion|
|
|
|
|
path = suggestion.diff_file.file_path
|
|
|
|
blob = project.repository.blob_at_branch(merge_request.source_branch,
|
|
|
|
path)
|
|
|
|
|
|
|
|
expect(blob.data).to eq(expected_content_by_path[path.to_sym])
|
|
|
|
end
|
2019-03-15 13:35:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates suggestion applied and commit_id columns' do
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(suggestions.map(&:applied)).to all(be false)
|
|
|
|
expect(suggestions.map(&:commit_id)).to all(be nil)
|
|
|
|
|
|
|
|
apply(suggestions)
|
|
|
|
|
|
|
|
expect(suggestions.map(&:applied)).to all(be true)
|
|
|
|
expect(suggestions.map(&:commit_id)).to all(be_present)
|
2019-03-15 13:35:34 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'created commit has users email and name' do
|
2020-06-08 20:08:47 -04:00
|
|
|
apply(suggestions)
|
2019-03-15 13:35:34 -04:00
|
|
|
|
|
|
|
commit = project.repository.commit
|
|
|
|
|
|
|
|
expect(user.commit_email).not_to eq(user.email)
|
|
|
|
expect(commit.author_email).to eq(user.commit_email)
|
|
|
|
expect(commit.committer_email).to eq(user.commit_email)
|
|
|
|
expect(commit.author_name).to eq(user.name)
|
|
|
|
end
|
2020-01-13 07:08:04 -05:00
|
|
|
|
|
|
|
context 'when a custom suggestion commit message' do
|
|
|
|
before do
|
|
|
|
project.update!(suggestion_commit_message: message)
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
apply(suggestions)
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'is not specified' do
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:message) { '' }
|
2020-01-31 07:08:33 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
it 'uses the default commit message' do
|
|
|
|
expect(project.repository.commit.message).to(
|
|
|
|
match(/\AApply #{suggestions.size} suggestion\(s\) to \d+ file\(s\)\z/)
|
|
|
|
)
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'is specified' do
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:message) do
|
|
|
|
'refactor: %{project_name} %{branch_name} %{username}'
|
|
|
|
end
|
2020-01-13 07:08:04 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
it 'generates a custom commit message' do
|
|
|
|
expect(project.repository.commit.message).to(
|
|
|
|
eq("refactor: Project_1 master test.user")
|
|
|
|
)
|
2020-01-13 07:08:04 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-03-15 13:35:34 -04:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
subject(:apply_service) { described_class }
|
|
|
|
|
|
|
|
let_it_be(:user) do
|
|
|
|
create(:user, :commit_email, name: 'Test User', username: 'test.user')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:project) do
|
|
|
|
create(:project, :repository, path: 'project-1', name: 'Project_1')
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, source_project: project,
|
|
|
|
target_project: project,
|
|
|
|
source_branch: 'master')
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2019-05-28 03:12:29 -04:00
|
|
|
let(:position) { build_position }
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
let(:diff_note) do
|
2020-06-08 20:08:47 -04:00
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request,
|
|
|
|
position: position, project: project)
|
2019-04-04 09:08:34 -04:00
|
|
|
end
|
|
|
|
|
2018-12-13 14:17:19 -05:00
|
|
|
let(:suggestion) do
|
2019-03-15 13:35:34 -04:00
|
|
|
create(:suggestion, :content_from_repo, note: diff_note,
|
2020-06-08 20:08:47 -04:00
|
|
|
to_content: " raise RuntimeError, 'Explosion'\n # explosion?\n")
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:suggestion2) do
|
|
|
|
create_suggestion(
|
|
|
|
to_content: " *** SUGGESTION CHANGE ***\n",
|
|
|
|
new_line: 15)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:suggestion3) do
|
|
|
|
create_suggestion(
|
|
|
|
to_content: " *** ANOTHER SUGGESTION CHANGE ***\n",
|
|
|
|
old_path: "files/ruby/regex.rb",
|
|
|
|
new_path: "files/ruby/regex.rb",
|
|
|
|
new_line: 22)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:suggestions) { [suggestion, suggestion2, suggestion3] }
|
2018-12-13 14:17:19 -05:00
|
|
|
|
|
|
|
context 'patch is appliable' do
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:popen_content) do
|
2018-12-13 14:17:19 -05:00
|
|
|
<<-CONTENT.strip_heredoc
|
2020-06-08 20:08:47 -04:00
|
|
|
require 'fileutils'
|
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
module Popen
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def popen(cmd, path=nil)
|
|
|
|
unless cmd.is_a?(Array)
|
|
|
|
raise RuntimeError, 'Explosion'
|
|
|
|
# explosion?
|
|
|
|
end
|
|
|
|
|
|
|
|
path ||= Dir.pwd
|
|
|
|
|
|
|
|
vars = {
|
|
|
|
*** SUGGESTION CHANGE ***
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
chdir: path
|
|
|
|
}
|
|
|
|
|
|
|
|
unless File.directory?(path)
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
@cmd_output = ""
|
|
|
|
@cmd_status = 0
|
|
|
|
|
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
|
|
|
@cmd_output << stdout.read
|
|
|
|
@cmd_output << stderr.read
|
|
|
|
@cmd_status = wait_thr.value.exitstatus
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
return @cmd_output, @cmd_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CONTENT
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:regex_content) do
|
|
|
|
<<-CONTENT.strip_heredoc
|
|
|
|
module Gitlab
|
|
|
|
module Regex
|
2018-12-13 14:17:19 -05:00
|
|
|
extend self
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def username_regex
|
|
|
|
default_regex
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def project_name_regex
|
|
|
|
/\\A[a-zA-Z0-9][a-zA-Z0-9_\\-\\. ]*\\z/
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def name_regex
|
|
|
|
/\\A[a-zA-Z0-9_\\-\\. ]*\\z/
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def path_regex
|
|
|
|
default_regex
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def archive_formats_regex
|
|
|
|
*** ANOTHER SUGGESTION CHANGE ***
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def git_reference_regex
|
|
|
|
# Valid git ref regex, see:
|
|
|
|
# https://www.kernel.org/pub/software/scm/git/docs/git-check-ref-format.html
|
|
|
|
%r{
|
|
|
|
(?!
|
|
|
|
(?# doesn't begins with)
|
|
|
|
\\/| (?# rule #6)
|
|
|
|
(?# doesn't contain)
|
|
|
|
.*(?:
|
|
|
|
[\\\/.]\\\.| (?# rule #1,3)
|
|
|
|
\\/\\/| (?# rule #6)
|
|
|
|
@\\{| (?# rule #8)
|
|
|
|
\\\\ (?# rule #9)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
[^\\000-\\040\\177~^:?*\\[]+ (?# rule #4-5)
|
|
|
|
(?# doesn't end with)
|
|
|
|
(?<!\\.lock) (?# rule #1)
|
|
|
|
(?<![\\/.]) (?# rule #6-7)
|
|
|
|
}x
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
protected
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
def default_regex
|
|
|
|
/\\A[.?]?[a-zA-Z0-9][a-zA-Z0-9_\\-\\.]*(?<!\\.git)\\z/
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
CONTENT
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:expected_content_by_path) do
|
|
|
|
{
|
|
|
|
"files/ruby/popen.rb": popen_content,
|
|
|
|
"files/ruby/regex.rb": regex_content
|
|
|
|
}
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
context 'non-fork project' do
|
2018-12-13 14:17:19 -05:00
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
it_behaves_like 'successfully creates commit and updates suggestions'
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
context 'when it fails to apply because a file was changed' do
|
|
|
|
before do
|
|
|
|
params = {
|
|
|
|
file_path: suggestion3.diff_file.file_path,
|
|
|
|
start_branch: suggestion3.branch,
|
|
|
|
branch_name: suggestion3.branch,
|
|
|
|
commit_message: 'Update file',
|
|
|
|
file_content: 'New content'
|
|
|
|
}
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
# Reload the suggestion so it's memoized values get reset after the
|
|
|
|
# file was changed.
|
|
|
|
suggestion3.reload
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
Files::UpdateService.new(project, user, params).execute
|
|
|
|
end
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
it 'returns error message' do
|
|
|
|
result = apply_service.new(user, suggestion, suggestion3, suggestion2).execute
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(result).to eq(message: 'A file has been changed.', status: :error)
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-23 09:42:26 -05:00
|
|
|
context 'when HEAD from position is different from source branch HEAD on repo' do
|
2019-01-10 07:38:15 -05:00
|
|
|
it 'returns error message' do
|
|
|
|
allow(suggestion).to receive(:appliable?) { true }
|
2019-01-23 09:42:26 -05:00
|
|
|
allow(suggestion.position).to receive(:head_sha) { 'old-sha' }
|
|
|
|
allow(suggestion.noteable).to receive(:source_branch_sha) { 'new-sha' }
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
result = apply_service.new(user, suggestion).execute
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(result).to eq(message: 'A file has been changed.', status: :error)
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
context 'multiple suggestions applied sequentially' do
|
2019-01-10 07:38:15 -05:00
|
|
|
def apply_suggestion(suggestion)
|
2019-03-15 13:35:34 -04:00
|
|
|
suggestion.reload
|
2019-01-10 07:38:15 -05:00
|
|
|
merge_request.reload
|
|
|
|
merge_request.clear_memoized_shas
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
result = apply_service.new(user, suggestion).execute
|
|
|
|
suggestion.reload
|
2019-03-15 13:35:34 -04:00
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
|
2019-01-10 07:38:15 -05:00
|
|
|
refresh = MergeRequests::RefreshService.new(project, user)
|
|
|
|
refresh.execute(merge_request.diff_head_sha,
|
|
|
|
suggestion.commit_id,
|
|
|
|
merge_request.source_branch_ref)
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_raw_diff(suggestion)
|
2020-06-08 20:08:47 -04:00
|
|
|
project.reload.commit(suggestion.commit_id)
|
|
|
|
.diffs.diff_files.first.diff.diff
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'applies multiple suggestions in subsequent versions correctly' do
|
2020-06-08 20:08:47 -04:00
|
|
|
suggestion1 = create_suggestion(
|
|
|
|
from_content: "\n",
|
|
|
|
to_content: "# v1 change\n",
|
|
|
|
old_line: nil,
|
|
|
|
new_line: 13)
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
suggestion2 = create_suggestion(
|
|
|
|
from_content: " @cmd_output << stderr.read\n",
|
|
|
|
to_content: "# v2 change\n",
|
|
|
|
old_line: 24,
|
|
|
|
new_line: 31)
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
apply_suggestion(suggestion1)
|
|
|
|
apply_suggestion(suggestion2)
|
2019-01-10 07:38:15 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
suggestion1_diff = fetch_raw_diff(suggestion1)
|
|
|
|
suggestion2_diff = fetch_raw_diff(suggestion2)
|
2019-01-10 07:38:15 -05:00
|
|
|
|
|
|
|
# rubocop: disable Layout/TrailingWhitespace
|
2020-06-08 20:08:47 -04:00
|
|
|
expected_suggestion1_diff = <<-CONTENT.strip_heredoc
|
2019-01-10 07:38:15 -05:00
|
|
|
@@ -10,7 +10,7 @@ module Popen
|
|
|
|
end
|
|
|
|
|
|
|
|
path ||= Dir.pwd
|
|
|
|
-
|
|
|
|
+# v1 change
|
|
|
|
vars = {
|
|
|
|
"PWD" => path
|
|
|
|
}
|
|
|
|
CONTENT
|
|
|
|
# rubocop: enable Layout/TrailingWhitespace
|
|
|
|
|
|
|
|
# rubocop: disable Layout/TrailingWhitespace
|
2020-06-08 20:08:47 -04:00
|
|
|
expected_suggestion2_diff = <<-CONTENT.strip_heredoc
|
2019-01-10 07:38:15 -05:00
|
|
|
@@ -28,7 +28,7 @@ module Popen
|
|
|
|
|
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
|
|
|
@cmd_output << stdout.read
|
|
|
|
- @cmd_output << stderr.read
|
|
|
|
+# v2 change
|
|
|
|
@cmd_status = wait_thr.value.exitstatus
|
|
|
|
end
|
|
|
|
CONTENT
|
|
|
|
# rubocop: enable Layout/TrailingWhitespace
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(suggestion1_diff.strip).to eq(expected_suggestion1_diff.strip)
|
|
|
|
expect(suggestion2_diff.strip).to eq(expected_suggestion2_diff.strip)
|
2019-01-10 07:38:15 -05:00
|
|
|
end
|
|
|
|
end
|
2019-04-04 09:08:34 -04:00
|
|
|
|
|
|
|
context 'multi-line suggestion' do
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:popen_content) do
|
|
|
|
<<~CONTENT.strip_heredoc
|
2019-04-04 09:08:34 -04:00
|
|
|
require 'fileutils'
|
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
module Popen
|
|
|
|
extend self
|
|
|
|
|
|
|
|
# multi
|
|
|
|
# line
|
|
|
|
|
|
|
|
vars = {
|
|
|
|
"PWD" => path
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
chdir: path
|
|
|
|
}
|
|
|
|
|
|
|
|
unless File.directory?(path)
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
@cmd_output = ""
|
|
|
|
@cmd_status = 0
|
|
|
|
|
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
|
|
|
@cmd_output << stdout.read
|
|
|
|
@cmd_output << stderr.read
|
|
|
|
@cmd_status = wait_thr.value.exitstatus
|
|
|
|
end
|
|
|
|
|
|
|
|
return @cmd_output, @cmd_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CONTENT
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:expected_content_by_path) do
|
|
|
|
{
|
|
|
|
"files/ruby/popen.rb": popen_content
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
let(:suggestion) do
|
|
|
|
create(:suggestion, :content_from_repo, note: diff_note,
|
2020-06-08 20:08:47 -04:00
|
|
|
lines_above: 2,
|
|
|
|
lines_below: 3,
|
|
|
|
to_content: "# multi\n# line\n")
|
2019-04-04 09:08:34 -04:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:suggestions) { [suggestion] }
|
|
|
|
|
|
|
|
it_behaves_like 'successfully creates commit and updates suggestions'
|
2019-04-04 09:08:34 -04:00
|
|
|
end
|
2019-05-28 03:12:29 -04:00
|
|
|
|
|
|
|
context 'remove an empty line suggestion' do
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:popen_content) do
|
|
|
|
<<~CONTENT.strip_heredoc
|
2019-05-28 03:12:29 -04:00
|
|
|
require 'fileutils'
|
|
|
|
require 'open3'
|
|
|
|
|
|
|
|
module Popen
|
|
|
|
extend self
|
|
|
|
|
|
|
|
def popen(cmd, path=nil)
|
|
|
|
unless cmd.is_a?(Array)
|
|
|
|
raise RuntimeError, "System commands must be given as an array of strings"
|
|
|
|
end
|
|
|
|
|
|
|
|
path ||= Dir.pwd
|
|
|
|
vars = {
|
|
|
|
"PWD" => path
|
|
|
|
}
|
|
|
|
|
|
|
|
options = {
|
|
|
|
chdir: path
|
|
|
|
}
|
|
|
|
|
|
|
|
unless File.directory?(path)
|
|
|
|
FileUtils.mkdir_p(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
@cmd_output = ""
|
|
|
|
@cmd_status = 0
|
|
|
|
|
|
|
|
Open3.popen3(vars, *cmd, options) do |stdin, stdout, stderr, wait_thr|
|
|
|
|
@cmd_output << stdout.read
|
|
|
|
@cmd_output << stderr.read
|
|
|
|
@cmd_status = wait_thr.value.exitstatus
|
|
|
|
end
|
|
|
|
|
|
|
|
return @cmd_output, @cmd_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CONTENT
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:expected_content_by_path) do
|
|
|
|
{
|
|
|
|
"files/ruby/popen.rb": popen_content
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-05-28 03:12:29 -04:00
|
|
|
let(:suggestion) do
|
2020-06-08 20:08:47 -04:00
|
|
|
create_suggestion( to_content: "", new_line: 13)
|
2019-05-28 03:12:29 -04:00
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
let(:suggestions) { [suggestion] }
|
|
|
|
|
|
|
|
it_behaves_like 'successfully creates commit and updates suggestions'
|
2019-05-28 03:12:29 -04:00
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork-project' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
|
|
|
|
let(:forked_project) do
|
2020-06-08 20:08:47 -04:00
|
|
|
fork_project_with_submodules(project,
|
|
|
|
user, repository: project.repository)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request,
|
2020-06-08 20:08:47 -04:00
|
|
|
source_branch: 'conflict-resolvable-fork',
|
|
|
|
source_project: forked_project,
|
|
|
|
target_branch: 'conflict-start',
|
|
|
|
target_project: project)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let!(:diff_note) do
|
2020-06-08 20:08:47 -04:00
|
|
|
create(:diff_note_on_merge_request,
|
|
|
|
noteable: merge_request,
|
|
|
|
position: position,
|
|
|
|
project: project)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates file in the source project' do
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(Files::MultiService).to receive(:new)
|
|
|
|
.with(merge_request.source_project,
|
|
|
|
user,
|
|
|
|
anything).and_call_original
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
apply_service.new(user, suggestion).execute
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no permission' do
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, source_project: project,
|
2020-06-08 20:08:47 -04:00
|
|
|
target_project: project)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:diff_note) do
|
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request,
|
2020-06-08 20:08:47 -04:00
|
|
|
position: position,
|
|
|
|
project: project)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'user cannot write in project repo' do
|
|
|
|
before do
|
|
|
|
project.add_reporter(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error' do
|
2020-06-08 20:08:47 -04:00
|
|
|
result = apply_service.new(user, suggestion).execute
|
2018-12-13 14:17:19 -05:00
|
|
|
|
|
|
|
expect(result).to eq(message: "You are not allowed to push into this branch",
|
|
|
|
status: :error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'patch is not appliable' do
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, source_project: project,
|
2020-06-08 20:08:47 -04:00
|
|
|
target_project: project)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:diff_note) do
|
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request,
|
2020-06-08 20:08:47 -04:00
|
|
|
position: position,
|
|
|
|
project: project)
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2019-02-20 16:20:08 -05:00
|
|
|
context 'diff file was not found' do
|
|
|
|
it 'returns error message' do
|
|
|
|
expect(suggestion.note).to receive(:latest_diff_file) { nil }
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
result = apply_service.new(user, suggestion).execute
|
2019-02-20 16:20:08 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(result).to eq(message: 'A file was not found.',
|
2019-03-15 13:35:34 -04:00
|
|
|
status: :error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
context 'when not all suggestions belong to the same branch' do
|
|
|
|
it 'renders error message' do
|
|
|
|
merge_request2 = create(:merge_request,
|
|
|
|
:conflict,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project)
|
2019-03-15 13:35:34 -04:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
position2 = Gitlab::Diff::Position.new(old_path: "files/ruby/popen.rb",
|
|
|
|
new_path: "files/ruby/popen.rb",
|
|
|
|
old_line: nil,
|
|
|
|
new_line: 15,
|
|
|
|
diff_refs: merge_request2
|
|
|
|
.diff_refs)
|
2019-03-15 13:35:34 -04:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
diff_note2 = create(:diff_note_on_merge_request,
|
|
|
|
noteable: merge_request2,
|
|
|
|
position: position2,
|
|
|
|
project: project)
|
|
|
|
|
|
|
|
other_branch_suggestion = create(:suggestion, note: diff_note2)
|
|
|
|
|
|
|
|
result = apply_service.new(user, suggestion, other_branch_suggestion).execute
|
|
|
|
|
|
|
|
expect(result).to eq(message: 'Suggestions must all be on the same branch.',
|
2019-02-20 16:20:08 -05:00
|
|
|
status: :error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-21 05:09:34 -04:00
|
|
|
context 'suggestion is not appliable' do
|
|
|
|
let(:inapplicable_reason) { "Can't apply this suggestion." }
|
2018-12-13 14:17:19 -05:00
|
|
|
|
|
|
|
it 'returns error message' do
|
2020-07-21 05:09:34 -04:00
|
|
|
expect(suggestion).to receive(:appliable?).and_return(false)
|
|
|
|
expect(suggestion).to receive(:inapplicable_reason).and_return(inapplicable_reason)
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
result = apply_service.new(user, suggestion).execute
|
|
|
|
|
2020-07-21 05:09:34 -04:00
|
|
|
expect(result).to eq(message: inapplicable_reason, status: :error)
|
2020-06-08 20:08:47 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'lines of suggestions overlap' do
|
|
|
|
let(:suggestion) do
|
|
|
|
create_suggestion(
|
|
|
|
to_content: " raise RuntimeError, 'Explosion'\n # explosion?\n")
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:overlapping_suggestion) do
|
|
|
|
create_suggestion(to_content: "I Overlap!")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error message' do
|
|
|
|
result = apply_service.new(user, suggestion, overlapping_suggestion).execute
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2020-06-08 20:08:47 -04:00
|
|
|
expect(result).to eq(message: 'Suggestions are not applicable as their lines cannot overlap.',
|
2018-12-13 14:17:19 -05:00
|
|
|
status: :error)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|