Commit 91fe32d0 authored by Heinrich Lee Yu's avatar Heinrich Lee Yu

Merge branch '225617-refactor-inapplicable-reason' into 'master'

Make inapplicable_reason text to be reusable

See merge request gitlab-org/gitlab!37266
parents 582436b1 8e532be8
...@@ -43,12 +43,12 @@ class Suggestion < ApplicationRecord ...@@ -43,12 +43,12 @@ class Suggestion < ApplicationRecord
def inapplicable_reason(cached: true) def inapplicable_reason(cached: true)
strong_memoize("inapplicable_reason_#{cached}") do strong_memoize("inapplicable_reason_#{cached}") do
next :applied if applied? next _("Can't apply this suggestion.") if applied?
next :merge_request_merged if noteable.merged? next _("This merge request was merged. To apply this suggestion, edit this file directly.") if noteable.merged?
next :merge_request_closed if noteable.closed? next _("This merge request is closed. To apply this suggestion, edit this file directly.") if noteable.closed?
next :source_branch_deleted unless noteable.source_branch_exists? next _("Can't apply as the source branch was deleted.") unless noteable.source_branch_exists?
next :outdated if outdated?(cached: cached) || !note.active? next outdated_reason if outdated?(cached: cached) || !note.active?
next :same_content unless different_content? next _("This suggestion already matches its content.") unless different_content?
end end
end end
...@@ -73,4 +73,12 @@ class Suggestion < ApplicationRecord ...@@ -73,4 +73,12 @@ class Suggestion < ApplicationRecord
def different_content? def different_content?
from_content != to_content from_content != to_content
end end
def outdated_reason
if single_line?
_("Can't apply as this line was changed in a more recent version.")
else
_("Can't apply as these lines were changed in a more recent version.")
end
end
end end
...@@ -16,24 +16,8 @@ class SuggestionEntity < API::Entities::Suggestion ...@@ -16,24 +16,8 @@ class SuggestionEntity < API::Entities::Suggestion
expose :inapplicable_reason do |suggestion| expose :inapplicable_reason do |suggestion|
next _("You don't have write access to the source branch.") unless can_apply?(suggestion) next _("You don't have write access to the source branch.") unless can_apply?(suggestion)
next if suggestion.appliable?
case suggestion.inapplicable_reason suggestion.inapplicable_reason
when :merge_request_merged
_("This merge request was merged. To apply this suggestion, edit this file directly.")
when :merge_request_closed
_("This merge request is closed. To apply this suggestion, edit this file directly.")
when :source_branch_deleted
_("Can't apply as the source branch was deleted.")
when :outdated
phrase = suggestion.single_line? ? 'this line was' : 'these lines were'
_("Can't apply as %{phrase} changed in a more recent version.") % { phrase: phrase }
when :same_content
_("This suggestion already matches its content.")
else
_("Can't apply this suggestion.")
end
end end
private private
......
...@@ -4149,10 +4149,13 @@ msgstr "" ...@@ -4149,10 +4149,13 @@ msgstr ""
msgid "Can override approvers and approvals required per merge request" msgid "Can override approvers and approvals required per merge request"
msgstr "" msgstr ""
msgid "Can't apply as %{phrase} changed in a more recent version." msgid "Can't apply as the source branch was deleted."
msgstr "" msgstr ""
msgid "Can't apply as the source branch was deleted." msgid "Can't apply as these lines were changed in a more recent version."
msgstr ""
msgid "Can't apply as this line was changed in a more recent version."
msgstr "" msgstr ""
msgid "Can't apply this suggestion." msgid "Can't apply this suggestion."
......
...@@ -53,7 +53,7 @@ RSpec.describe Suggestion do ...@@ -53,7 +53,7 @@ RSpec.describe Suggestion do
end end
context 'when inapplicable_reason is not nil' do context 'when inapplicable_reason is not nil' do
let(:inapplicable_reason) { :applied } let(:inapplicable_reason) { "Can't apply this suggestion." }
it { is_expected.to be_falsey } it { is_expected.to be_falsey }
end end
...@@ -77,7 +77,7 @@ RSpec.describe Suggestion do ...@@ -77,7 +77,7 @@ RSpec.describe Suggestion do
context 'when suggestion is already applied' do context 'when suggestion is already applied' do
let(:suggestion) { build(:suggestion, :applied, note: note) } let(:suggestion) { build(:suggestion, :applied, note: note) }
it { is_expected.to eq(:applied) } it { is_expected.to eq("Can't apply this suggestion.") }
end end
context 'when merge request was merged' do context 'when merge request was merged' do
...@@ -85,7 +85,7 @@ RSpec.describe Suggestion do ...@@ -85,7 +85,7 @@ RSpec.describe Suggestion do
merge_request.mark_as_merged! merge_request.mark_as_merged!
end end
it { is_expected.to eq(:merge_request_merged) } it { is_expected.to eq("This merge request was merged. To apply this suggestion, edit this file directly.") }
end end
context 'when merge request is closed' do context 'when merge request is closed' do
...@@ -93,7 +93,7 @@ RSpec.describe Suggestion do ...@@ -93,7 +93,7 @@ RSpec.describe Suggestion do
merge_request.close! merge_request.close!
end end
it { is_expected.to eq(:merge_request_closed) } it { is_expected.to eq("This merge request is closed. To apply this suggestion, edit this file directly.") }
end end
context 'when source branch is deleted' do context 'when source branch is deleted' do
...@@ -101,23 +101,51 @@ RSpec.describe Suggestion do ...@@ -101,23 +101,51 @@ RSpec.describe Suggestion do
merge_request.project.repository.rm_branch(merge_request.author, merge_request.source_branch) merge_request.project.repository.rm_branch(merge_request.author, merge_request.source_branch)
end end
it { is_expected.to eq(:source_branch_deleted) } it { is_expected.to eq("Can't apply as the source branch was deleted.") }
end end
context 'when content is outdated' do context 'when outdated' do
before do shared_examples_for 'outdated suggestion' do
allow(suggestion).to receive(:outdated?).and_return(true) before do
allow(suggestion).to receive(:single_line?).and_return(single_line)
end
context 'and suggestion is for a single line' do
let(:single_line) { true }
it { is_expected.to eq("Can't apply as this line was changed in a more recent version.") }
end
context 'and suggestion is for multiple lines' do
let(:single_line) { false }
it { is_expected.to eq("Can't apply as these lines were changed in a more recent version.") }
end
end end
it { is_expected.to eq(:outdated) } context 'and content is outdated' do
before do
allow(suggestion).to receive(:outdated?).and_return(true)
end
it_behaves_like 'outdated suggestion'
end
context 'and note is outdated' do
before do
allow(note).to receive(:active?).and_return(false)
end
it_behaves_like 'outdated suggestion'
end
end end
context 'when note is outdated' do context 'when suggestion has the same content' do
before do before do
allow(note).to receive(:active?).and_return(false) allow(suggestion).to receive(:different_content?).and_return(false)
end end
it { is_expected.to eq(:outdated) } it { is_expected.to eq("This suggestion already matches its content.") }
end end
context 'when applicable' do context 'when applicable' do
......
...@@ -36,87 +36,11 @@ RSpec.describe SuggestionEntity do ...@@ -36,87 +36,11 @@ RSpec.describe SuggestionEntity do
let(:can_apply_suggestion) { true } let(:can_apply_suggestion) { true }
before do before do
allow(suggestion).to receive(:appliable?).and_return(appliable) allow(suggestion).to receive(:inapplicable_reason).and_return("Can't apply this suggestion.")
end end
context 'and suggestion is appliable' do it 'returns the inapplicable reason' do
let(:appliable) { true } expect(inapplicable_reason).to eq(suggestion.inapplicable_reason)
it 'returns nil' do
expect(inapplicable_reason).to be_nil
end
end
context 'but suggestion is not applicable' do
let(:appliable) { false }
before do
allow(suggestion).to receive(:inapplicable_reason).and_return(reason)
end
context 'and merge request was merged' do
let(:reason) { :merge_request_merged }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("This merge request was merged. To apply this suggestion, edit this file directly.")
end
end
context 'and source branch was deleted' do
let(:reason) { :source_branch_deleted }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("Can't apply as the source branch was deleted.")
end
end
context 'and merge request is closed' do
let(:reason) { :merge_request_closed }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("This merge request is closed. To apply this suggestion, edit this file directly.")
end
end
context 'and suggestion is outdated' do
let(:reason) { :outdated }
before do
allow(suggestion).to receive(:single_line?).and_return(single_line)
end
context 'and suggestion is for a single line' do
let(:single_line) { true }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("Can't apply as this line was changed in a more recent version.")
end
end
context 'and suggestion is for multiple lines' do
let(:single_line) { false }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("Can't apply as these lines were changed in a more recent version.")
end
end
end
context 'and suggestion has the same content' do
let(:reason) { :same_content }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("This suggestion already matches its content.")
end
end
context 'and suggestion is inapplicable for other reasons' do
let(:reason) { :some_other_reason }
it 'returns default message' do
expect(inapplicable_reason).to eq("Can't apply this suggestion.")
end
end
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