2019-02-15 15:39:39 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2020-07-28 23:09:34 +05:30
|
|
|
RSpec.describe Suggestions::ApplyService do
|
2019-02-15 15:39:39 +05:30
|
|
|
include ProjectForksHelper
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def create_suggestion(args)
|
|
|
|
position_args = args.slice(:old_path, :new_path, :old_line, :new_line)
|
|
|
|
content_args = args.slice(:from_content, :to_content)
|
|
|
|
|
2021-02-22 17:27:13 +05:30
|
|
|
position = build_position(**position_args)
|
2020-06-23 00:09:42 +05:30
|
|
|
|
|
|
|
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
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
def apply(suggestions, custom_message = nil)
|
|
|
|
result = apply_service.new(user, *suggestions, message: custom_message).execute
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
suggestions.map { |suggestion| suggestion.reload }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates suggestion applied and commit_id columns' do
|
2020-06-23 00:09:42 +05:30
|
|
|
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-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'created commit has users email and name' do
|
2020-06-23 00:09:42 +05:30
|
|
|
apply(suggestions)
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
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-03-13 15:44:24 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'tracks apply suggestion event' do
|
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
|
|
|
|
.to receive(:track_apply_suggestion_action)
|
|
|
|
.with(user: user)
|
|
|
|
|
|
|
|
apply(suggestions)
|
|
|
|
end
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
context 'when a custom suggestion commit message' do
|
|
|
|
before do
|
|
|
|
project.update!(suggestion_commit_message: message)
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
apply(suggestions)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'is not specified' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:message) { '' }
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'is specified' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:message) do
|
|
|
|
'refactor: %{project_name} %{branch_name} %{username}'
|
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'generates a custom commit message' do
|
|
|
|
expect(project.repository.commit.message).to(
|
|
|
|
eq("refactor: Project_1 master test.user")
|
|
|
|
)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
context 'with a user suggested commit message' do
|
|
|
|
let(:message) { "i'm a custom commit message!" }
|
|
|
|
|
|
|
|
it "uses the user's commit message" do
|
|
|
|
apply(suggestions, message)
|
|
|
|
|
|
|
|
expect(project.repository.commit.message).to(eq(message))
|
|
|
|
end
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:position) { build_position }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
let(:diff_note) do
|
2020-06-23 00:09:42 +05:30
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request,
|
|
|
|
position: position, project: project)
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
let(:suggestion) do
|
2019-07-07 11:18:12 +05:30
|
|
|
create(:suggestion, :content_from_repo, note: diff_note,
|
2020-06-23 00:09:42 +05:30
|
|
|
to_content: " raise RuntimeError, 'Explosion'\n # explosion?\n")
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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] }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
context 'patch is appliable' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:popen_content) do
|
2019-02-15 15:39:39 +05:30
|
|
|
<<-CONTENT.strip_heredoc
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
return @cmd_output, @cmd_status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
CONTENT
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:regex_content) do
|
|
|
|
<<-CONTENT.strip_heredoc
|
|
|
|
module Gitlab
|
|
|
|
module Regex
|
2019-02-15 15:39:39 +05:30
|
|
|
extend self
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def username_regex
|
|
|
|
default_regex
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def project_name_regex
|
|
|
|
/\\A[a-zA-Z0-9][a-zA-Z0-9_\\-\\. ]*\\z/
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def name_regex
|
|
|
|
/\\A[a-zA-Z0-9_\\-\\. ]*\\z/
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def path_regex
|
|
|
|
default_regex
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def archive_formats_regex
|
|
|
|
*** ANOTHER SUGGESTION CHANGE ***
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
protected
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
def default_regex
|
|
|
|
/\\A[.?]?[a-zA-Z0-9][a-zA-Z0-9_\\-\\.]*(?<!\\.git)\\z/
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
CONTENT
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:expected_content_by_path) do
|
|
|
|
{
|
|
|
|
"files/ruby/popen.rb": popen_content,
|
|
|
|
"files/ruby/regex.rb": regex_content
|
|
|
|
}
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'non-fork project' do
|
2019-02-15 15:39:39 +05:30
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it_behaves_like 'successfully creates commit and updates suggestions'
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
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-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
# Reload the suggestion so it's memoized values get reset after the
|
|
|
|
# file was changed.
|
|
|
|
suggestion3.reload
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
Files::UpdateService.new(project, user, params).execute
|
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
it 'returns error message' do
|
|
|
|
result = apply_service.new(user, suggestion, suggestion3, suggestion2).execute
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(result).to eq(message: 'A file has been changed.', status: :error)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when HEAD from position is different from source branch HEAD on repo' do
|
|
|
|
it 'returns error message' do
|
|
|
|
allow(suggestion).to receive(:appliable?) { true }
|
|
|
|
allow(suggestion.position).to receive(:head_sha) { 'old-sha' }
|
|
|
|
allow(suggestion.noteable).to receive(:source_branch_sha) { 'new-sha' }
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
result = apply_service.new(user, suggestion).execute
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(result).to eq(message: 'A file has been changed.', status: :error)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'multiple suggestions applied sequentially' do
|
2019-02-15 15:39:39 +05:30
|
|
|
def apply_suggestion(suggestion)
|
2019-07-07 11:18:12 +05:30
|
|
|
suggestion.reload
|
2019-02-15 15:39:39 +05:30
|
|
|
merge_request.reload
|
|
|
|
merge_request.clear_memoized_shas
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
result = apply_service.new(user, suggestion).execute
|
|
|
|
suggestion.reload
|
2019-07-07 11:18:12 +05:30
|
|
|
expect(result[:status]).to eq(:success)
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
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-23 00:09:42 +05:30
|
|
|
project.reload.commit(suggestion.commit_id)
|
|
|
|
.diffs.diff_files.first.diff.diff
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
it 'applies multiple suggestions in subsequent versions correctly' do
|
2020-06-23 00:09:42 +05:30
|
|
|
suggestion1 = create_suggestion(
|
|
|
|
from_content: "\n",
|
|
|
|
to_content: "# v1 change\n",
|
|
|
|
old_line: nil,
|
|
|
|
new_line: 13)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
suggestion2 = create_suggestion(
|
|
|
|
from_content: " @cmd_output << stderr.read\n",
|
|
|
|
to_content: "# v2 change\n",
|
|
|
|
old_line: 24,
|
|
|
|
new_line: 31)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
apply_suggestion(suggestion1)
|
|
|
|
apply_suggestion(suggestion2)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
suggestion1_diff = fetch_raw_diff(suggestion1)
|
|
|
|
suggestion2_diff = fetch_raw_diff(suggestion2)
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
# rubocop: disable Layout/TrailingWhitespace
|
2020-06-23 00:09:42 +05:30
|
|
|
expected_suggestion1_diff = <<-CONTENT.strip_heredoc
|
2019-02-15 15:39:39 +05:30
|
|
|
@@ -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-23 00:09:42 +05:30
|
|
|
expected_suggestion2_diff = <<-CONTENT.strip_heredoc
|
2019-02-15 15:39:39 +05:30
|
|
|
@@ -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-23 00:09:42 +05:30
|
|
|
expect(suggestion1_diff.strip).to eq(expected_suggestion1_diff.strip)
|
|
|
|
expect(suggestion2_diff.strip).to eq(expected_suggestion2_diff.strip)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
|
|
|
context 'multi-line suggestion' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:popen_content) do
|
|
|
|
<<~CONTENT.strip_heredoc
|
2019-07-07 11:18:12 +05:30
|
|
|
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-23 00:09:42 +05:30
|
|
|
let(:expected_content_by_path) do
|
|
|
|
{
|
|
|
|
"files/ruby/popen.rb": popen_content
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
let(:suggestion) do
|
|
|
|
create(:suggestion, :content_from_repo, note: diff_note,
|
2020-06-23 00:09:42 +05:30
|
|
|
lines_above: 2,
|
|
|
|
lines_below: 3,
|
|
|
|
to_content: "# multi\n# line\n")
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:suggestions) { [suggestion] }
|
|
|
|
|
|
|
|
it_behaves_like 'successfully creates commit and updates suggestions'
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
2019-09-04 21:01:54 +05:30
|
|
|
|
|
|
|
context 'remove an empty line suggestion' do
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:popen_content) do
|
|
|
|
<<~CONTENT.strip_heredoc
|
2019-09-04 21:01:54 +05:30
|
|
|
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-23 00:09:42 +05:30
|
|
|
let(:expected_content_by_path) do
|
|
|
|
{
|
|
|
|
"files/ruby/popen.rb": popen_content
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
let(:suggestion) do
|
2020-06-23 00:09:42 +05:30
|
|
|
create_suggestion( to_content: "", new_line: 13)
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
let(:suggestions) { [suggestion] }
|
|
|
|
|
|
|
|
it_behaves_like 'successfully creates commit and updates suggestions'
|
2019-09-04 21:01:54 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'fork-project' do
|
|
|
|
let(:project) { create(:project, :public, :repository) }
|
|
|
|
|
|
|
|
let(:forked_project) do
|
2020-06-23 00:09:42 +05:30
|
|
|
fork_project_with_submodules(project,
|
|
|
|
user, repository: project.repository)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request,
|
2020-06-23 00:09:42 +05:30
|
|
|
source_branch: 'conflict-resolvable-fork',
|
|
|
|
source_project: forked_project,
|
|
|
|
target_branch: 'conflict-start',
|
|
|
|
target_project: project)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let!(:diff_note) do
|
2020-06-23 00:09:42 +05:30
|
|
|
create(:diff_note_on_merge_request,
|
|
|
|
noteable: merge_request,
|
|
|
|
position: position,
|
|
|
|
project: project)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'updates file in the source project' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(Files::MultiService).to receive(:new)
|
|
|
|
.with(merge_request.source_project,
|
|
|
|
user,
|
|
|
|
anything).and_call_original
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
apply_service.new(user, suggestion).execute
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no permission' do
|
|
|
|
let(:merge_request) do
|
|
|
|
create(:merge_request, source_project: project,
|
2020-06-23 00:09:42 +05:30
|
|
|
target_project: project)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:diff_note) do
|
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request,
|
2020-06-23 00:09:42 +05:30
|
|
|
position: position,
|
|
|
|
project: project)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
context 'user cannot write in project repo' do
|
|
|
|
before do
|
|
|
|
project.add_reporter(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error' do
|
2020-06-23 00:09:42 +05:30
|
|
|
result = apply_service.new(user, suggestion).execute
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
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-23 00:09:42 +05:30
|
|
|
target_project: project)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
let(:diff_note) do
|
|
|
|
create(:diff_note_on_merge_request, noteable: merge_request,
|
2020-06-23 00:09:42 +05:30
|
|
|
position: position,
|
|
|
|
project: project)
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_maintainer(user)
|
|
|
|
end
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
shared_examples_for 'service not tracking apply suggestion event' do
|
|
|
|
it 'does not track apply suggestion event' do
|
|
|
|
expect(Gitlab::UsageDataCounters::MergeRequestActivityUniqueCounter)
|
|
|
|
.not_to receive(:track_apply_suggestion_action)
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
context 'diff file was not found' do
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:result) { apply_service.new(user, suggestion).execute }
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
before do
|
|
|
|
expect(suggestion.note).to receive(:latest_diff_file) { nil }
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'returns error message' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(result).to eq(message: 'A file was not found.',
|
2019-07-07 11:18:12 +05:30
|
|
|
status: :error)
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it_behaves_like 'service not tracking apply suggestion event'
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2020-06-23 00:09:42 +05:30
|
|
|
context 'when not all suggestions belong to the same branch' do
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:merge_request2) do
|
|
|
|
create(
|
|
|
|
:merge_request,
|
|
|
|
:conflict,
|
|
|
|
source_project: project,
|
|
|
|
target_project: project
|
|
|
|
)
|
|
|
|
end
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:position2) do
|
|
|
|
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
|
|
|
|
)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:diff_note2) do
|
|
|
|
create(
|
|
|
|
:diff_note_on_merge_request,
|
|
|
|
noteable: merge_request2,
|
|
|
|
position: position2,
|
|
|
|
project: project
|
|
|
|
)
|
|
|
|
end
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:other_branch_suggestion) { create(:suggestion, note: diff_note2) }
|
|
|
|
let(:result) { apply_service.new(user, suggestion, other_branch_suggestion).execute }
|
2020-06-23 00:09:42 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'renders error message' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(result).to eq(message: 'Suggestions must all be on the same branch.',
|
2019-07-07 11:18:12 +05:30
|
|
|
status: :error)
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it_behaves_like 'service not tracking apply suggestion event'
|
2019-07-07 11:18:12 +05:30
|
|
|
end
|
|
|
|
|
2020-10-24 23:57:45 +05:30
|
|
|
context 'suggestion is not appliable' do
|
|
|
|
let(:inapplicable_reason) { "Can't apply this suggestion." }
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:result) { apply_service.new(user, suggestion).execute }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
before do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(suggestion).to receive(:appliable?).and_return(false)
|
|
|
|
expect(suggestion).to receive(:inapplicable_reason).and_return(inapplicable_reason)
|
2021-03-11 19:13:27 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'returns error message' do
|
2020-10-24 23:57:45 +05:30
|
|
|
expect(result).to eq(message: inapplicable_reason, status: :error)
|
2020-06-23 00:09:42 +05:30
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it_behaves_like 'service not tracking apply suggestion event'
|
2020-06-23 00:09:42 +05:30
|
|
|
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
|
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
let(:result) { apply_service.new(user, suggestion, overlapping_suggestion).execute }
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2021-03-11 19:13:27 +05:30
|
|
|
it 'returns error message' do
|
2020-06-23 00:09:42 +05:30
|
|
|
expect(result).to eq(message: 'Suggestions are not applicable as their lines cannot overlap.',
|
2019-02-15 15:39:39 +05:30
|
|
|
status: :error)
|
|
|
|
end
|
2021-03-11 19:13:27 +05:30
|
|
|
|
|
|
|
it_behaves_like 'service not tracking apply suggestion event'
|
2019-02-15 15:39:39 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|