summaryrefslogtreecommitdiff
path: root/spec/services
diff options
context:
space:
mode:
Diffstat (limited to 'spec/services')
-rw-r--r--spec/services/ci/enqueue_build_service_spec.rb16
-rw-r--r--spec/services/commits/tag_service_spec.rb102
-rw-r--r--spec/services/notes/quick_actions_service_spec.rb46
-rw-r--r--spec/services/preview_markdown_service_spec.rb25
-rw-r--r--spec/services/quick_actions/interpret_service_spec.rb107
-rw-r--r--spec/services/quick_actions/target_service_spec.rb83
-rw-r--r--spec/services/system_note_service_spec.rb19
7 files changed, 346 insertions, 52 deletions
diff --git a/spec/services/ci/enqueue_build_service_spec.rb b/spec/services/ci/enqueue_build_service_spec.rb
new file mode 100644
index 00000000000..e41b8e4800b
--- /dev/null
+++ b/spec/services/ci/enqueue_build_service_spec.rb
@@ -0,0 +1,16 @@
+# frozen_string_literal: true
+require 'spec_helper'
+
+describe Ci::EnqueueBuildService, '#execute' do
+ let(:user) { create(:user) }
+ let(:project) { create(:project) }
+ let(:ci_build) { create(:ci_build, :created) }
+
+ subject { described_class.new(project, user).execute(ci_build) }
+
+ it 'enqueues the build' do
+ subject
+
+ expect(ci_build.pending?).to be_truthy
+ end
+end
diff --git a/spec/services/commits/tag_service_spec.rb b/spec/services/commits/tag_service_spec.rb
new file mode 100644
index 00000000000..82377a8dace
--- /dev/null
+++ b/spec/services/commits/tag_service_spec.rb
@@ -0,0 +1,102 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe Commits::TagService do
+ let(:project) { create(:project, :repository) }
+ let(:user) { create(:user) }
+
+ let(:commit) { project.commit }
+
+ before do
+ project.add_maintainer(user)
+ end
+
+ describe '#execute' do
+ let(:service) { described_class.new(project, user, opts) }
+
+ shared_examples 'tag failure' do
+ it 'returns a hash with the :error status' do
+ result = service.execute(commit)
+
+ expect(result[:status]).to eq(:error)
+ expect(result[:message]).to eq(error_message)
+ end
+
+ it 'does not add a system note' do
+ service.execute(commit)
+
+ description_notes = find_notes('tag')
+ expect(description_notes).to be_empty
+ end
+ end
+
+ def find_notes(action)
+ commit
+ .notes
+ .joins(:system_note_metadata)
+ .where(system_note_metadata: { action: action })
+ end
+
+ context 'valid params' do
+ let(:opts) do
+ {
+ tag_name: 'v1.2.3',
+ tag_message: 'Release'
+ }
+ end
+
+ def find_notes(action)
+ commit
+ .notes
+ .joins(:system_note_metadata)
+ .where(system_note_metadata: { action: action })
+ end
+
+ context 'when tagging succeeds' do
+ it 'returns a hash with the :success status and created tag' do
+ result = service.execute(commit)
+
+ expect(result[:status]).to eq(:success)
+
+ tag = result[:tag]
+ expect(tag.name).to eq(opts[:tag_name])
+ expect(tag.message).to eq(opts[:tag_message])
+ end
+
+ it 'adds a system note' do
+ service.execute(commit)
+
+ description_notes = find_notes('tag')
+ expect(description_notes.length).to eq(1)
+ end
+ end
+
+ context 'when tagging fails' do
+ let(:tag_error) { 'GitLab: You are not allowed to push code to this project.' }
+
+ before do
+ tag_stub = instance_double(Tags::CreateService)
+ allow(Tags::CreateService).to receive(:new).and_return(tag_stub)
+ allow(tag_stub).to receive(:execute).and_return({
+ status: :error, message: tag_error
+ })
+ end
+
+ it_behaves_like 'tag failure' do
+ let(:error_message) { tag_error }
+ end
+ end
+ end
+
+ context 'invalid params' do
+ let(:opts) do
+ {}
+ end
+
+ it_behaves_like 'tag failure' do
+ let(:error_message) { 'Missing parameter tag_name' }
+ end
+ end
+ end
+end
diff --git a/spec/services/notes/quick_actions_service_spec.rb b/spec/services/notes/quick_actions_service_spec.rb
index 784dac55454..a8c994c101c 100644
--- a/spec/services/notes/quick_actions_service_spec.rb
+++ b/spec/services/notes/quick_actions_service_spec.rb
@@ -11,40 +11,6 @@ describe Notes::QuickActionsService do
end
end
- shared_examples 'note on noteable that does not support quick actions' do
- include_context 'note on noteable'
-
- before do
- note.note = note_text
- end
-
- describe 'note with only command' do
- describe '/close, /label, /assign & /milestone' do
- let(:note_text) { %(/close\n/assign @#{assignee.username}") }
-
- it 'saves the note and does not alter the note text' do
- content, command_params = service.extract_commands(note)
-
- expect(content).to eq note_text
- expect(command_params).to be_empty
- end
- end
- end
-
- describe 'note with command & text' do
- describe '/close, /label, /assign & /milestone' do
- let(:note_text) { %(HELLO\n/close\n/assign @#{assignee.username}\nWORLD) }
-
- it 'saves the note and does not alter the note text' do
- content, command_params = service.extract_commands(note)
-
- expect(content).to eq note_text
- expect(command_params).to be_empty
- end
- end
- end
- end
-
shared_examples 'note on noteable that supports quick actions' do
include_context 'note on noteable'
@@ -147,16 +113,16 @@ describe Notes::QuickActionsService do
expect(described_class.noteable_update_service(note)).to eq(Issues::UpdateService)
end
- it 'returns Issues::UpdateService for a note on a merge request' do
+ it 'returns MergeRequests::UpdateService for a note on a merge request' do
note = create(:note_on_merge_request, project: project)
expect(described_class.noteable_update_service(note)).to eq(MergeRequests::UpdateService)
end
- it 'returns nil for a note on a commit' do
+ it 'returns Commits::TagService for a note on a commit' do
note = create(:note_on_commit, project: project)
- expect(described_class.noteable_update_service(note)).to be_nil
+ expect(described_class.noteable_update_service(note)).to eq(Commits::TagService)
end
end
@@ -175,7 +141,7 @@ describe Notes::QuickActionsService do
let(:note) { create(:note_on_commit, project: project) }
it 'returns false' do
- expect(described_class.supported?(note)).to be_falsy
+ expect(described_class.supported?(note)).to be_truthy
end
end
end
@@ -203,10 +169,6 @@ describe Notes::QuickActionsService do
it_behaves_like 'note on noteable that supports quick actions' do
let(:note) { build(:note_on_merge_request, project: project) }
end
-
- it_behaves_like 'note on noteable that does not support quick actions' do
- let(:note) { build(:note_on_commit, project: project) }
- end
end
context 'CE restriction for issue assignees' do
diff --git a/spec/services/preview_markdown_service_spec.rb b/spec/services/preview_markdown_service_spec.rb
index 81dc7c57f4a..507909d9231 100644
--- a/spec/services/preview_markdown_service_spec.rb
+++ b/spec/services/preview_markdown_service_spec.rb
@@ -65,6 +65,31 @@ describe PreviewMarkdownService do
end
end
+ context 'commit description' do
+ let(:project) { create(:project, :repository) }
+ let(:commit) { project.commit }
+ let(:params) do
+ {
+ text: "My work\n/tag v1.2.3 Stable release",
+ quick_actions_target_type: 'Commit',
+ quick_actions_target_id: commit.id
+ }
+ end
+ let(:service) { described_class.new(project, user, params) }
+
+ it 'removes quick actions from text' do
+ result = service.execute
+
+ expect(result[:text]).to eq 'My work'
+ end
+
+ it 'explains quick actions effect' do
+ result = service.execute
+
+ expect(result[:commands]).to eq 'Tags this commit to v1.2.3 with "Stable release".'
+ end
+ end
+
it 'sets correct markdown engine' do
service = described_class.new(project, user, { markdown_version: CacheMarkdownField::CACHE_REDCARPET_VERSION })
result = service.execute
diff --git a/spec/services/quick_actions/interpret_service_spec.rb b/spec/services/quick_actions/interpret_service_spec.rb
index 743e281183e..bf1c157c4a2 100644
--- a/spec/services/quick_actions/interpret_service_spec.rb
+++ b/spec/services/quick_actions/interpret_service_spec.rb
@@ -6,6 +6,7 @@ describe QuickActions::InterpretService do
let(:developer2) { create(:user) }
let(:issue) { create(:issue, project: project) }
let(:milestone) { create(:milestone, project: project, title: '9.10') }
+ let(:commit) { create(:commit, project: project) }
let(:inprogress) { create(:label, project: project, title: 'In Progress') }
let(:bug) { create(:label, project: project, title: 'Bug') }
let(:note) { build(:note, commit_id: merge_request.diff_head_sha) }
@@ -347,6 +348,14 @@ describe QuickActions::InterpretService do
end
end
+ shared_examples 'tag command' do
+ it 'tags a commit' do
+ _, updates = service.execute(content, issuable)
+
+ expect(updates).to eq(tag_name: tag_name, tag_message: tag_message)
+ end
+ end
+
it_behaves_like 'reopen command' do
let(:content) { '/reopen' }
let(:issuable) { issue }
@@ -628,16 +637,6 @@ describe QuickActions::InterpretService do
let(:issuable) { merge_request }
end
- it_behaves_like 'todo command' do
- let(:content) { '/todo' }
- let(:issuable) { issue }
- end
-
- it_behaves_like 'todo command' do
- let(:content) { '/todo' }
- let(:issuable) { merge_request }
- end
-
it_behaves_like 'done command' do
let(:content) { '/done' }
let(:issuable) { issue }
@@ -787,6 +786,28 @@ describe QuickActions::InterpretService do
let(:issuable) { issue }
end
+ context '/todo' do
+ let(:content) { '/todo' }
+
+ context 'if issuable is an Issue' do
+ it_behaves_like 'todo command' do
+ let(:issuable) { issue }
+ end
+ end
+
+ context 'if issuable is a MergeRequest' do
+ it_behaves_like 'todo command' do
+ let(:issuable) { merge_request }
+ end
+ end
+
+ context 'if issuable is a Commit' do
+ it_behaves_like 'empty command' do
+ let(:issuable) { commit }
+ end
+ end
+ end
+
context '/copy_metadata command' do
let(:todo_label) { create(:label, project: project, title: 'To Do') }
let(:inreview_label) { create(:label, project: project, title: 'In Review') }
@@ -971,6 +992,12 @@ describe QuickActions::InterpretService do
let(:issuable) { issue }
end
end
+
+ context 'if issuable is a Commit' do
+ let(:content) { '/award :100:' }
+ let(:issuable) { commit }
+ it_behaves_like 'empty command'
+ end
end
context '/shrug command' do
@@ -1102,6 +1129,32 @@ describe QuickActions::InterpretService do
it_behaves_like 'empty command'
end
end
+
+ context '/tag command' do
+ let(:issuable) { commit }
+
+ context 'ignores command with no argument' do
+ it_behaves_like 'empty command' do
+ let(:content) { '/tag' }
+ end
+ end
+
+ context 'tags a commit with a tag name' do
+ it_behaves_like 'tag command' do
+ let(:tag_name) { 'v1.2.3' }
+ let(:tag_message) { nil }
+ let(:content) { "/tag #{tag_name}" }
+ end
+ end
+
+ context 'tags a commit with a tag name and message' do
+ it_behaves_like 'tag command' do
+ let(:tag_name) { 'v1.2.3' }
+ let(:tag_message) { 'Stable release' }
+ let(:content) { "/tag #{tag_name} #{tag_message}" }
+ end
+ end
+ end
end
describe '#explain' do
@@ -1319,5 +1372,39 @@ describe QuickActions::InterpretService do
expect(explanations).to eq(["Moves this issue to test/project."])
end
end
+
+ describe 'tag a commit' do
+ describe 'with a tag name' do
+ context 'without a message' do
+ let(:content) { '/tag v1.2.3' }
+
+ it 'includes the tag name only' do
+ _, explanations = service.explain(content, commit)
+
+ expect(explanations).to eq(["Tags this commit to v1.2.3."])
+ end
+ end
+
+ context 'with an empty message' do
+ let(:content) { '/tag v1.2.3 ' }
+
+ it 'includes the tag name only' do
+ _, explanations = service.explain(content, commit)
+
+ expect(explanations).to eq(["Tags this commit to v1.2.3."])
+ end
+ end
+ end
+
+ describe 'with a tag name and message' do
+ let(:content) { '/tag v1.2.3 Stable release' }
+
+ it 'includes the tag name and message' do
+ _, explanations = service.explain(content, commit)
+
+ expect(explanations).to eq(["Tags this commit to v1.2.3 with \"Stable release\"."])
+ end
+ end
+ end
end
end
diff --git a/spec/services/quick_actions/target_service_spec.rb b/spec/services/quick_actions/target_service_spec.rb
new file mode 100644
index 00000000000..0aeb29cbeec
--- /dev/null
+++ b/spec/services/quick_actions/target_service_spec.rb
@@ -0,0 +1,83 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+describe QuickActions::TargetService do
+ let(:project) { create(:project) }
+ let(:user) { create(:user) }
+ let(:service) { described_class.new(project, user) }
+
+ before do
+ project.add_maintainer(user)
+ end
+
+ describe '#execute' do
+ shared_examples 'no target' do |type_id:|
+ it 'returns nil' do
+ target = service.execute(type, type_id)
+
+ expect(target).to be_nil
+ end
+ end
+
+ shared_examples 'find target' do
+ it 'returns the target' do
+ found_target = service.execute(type, target_id)
+
+ expect(found_target).to eq(target)
+ end
+ end
+
+ shared_examples 'build target' do |type_id:|
+ it 'builds a new target' do
+ target = service.execute(type, type_id)
+
+ expect(target.project).to eq(project)
+ expect(target).to be_new_record
+ end
+ end
+
+ context 'for issue' do
+ let(:target) { create(:issue, project: project) }
+ let(:target_id) { target.iid }
+ let(:type) { 'Issue' }
+
+ it_behaves_like 'find target'
+ it_behaves_like 'build target', type_id: nil
+ it_behaves_like 'build target', type_id: -1
+ end
+
+ context 'for merge request' do
+ let(:target) { create(:merge_request, source_project: project) }
+ let(:target_id) { target.iid }
+ let(:type) { 'MergeRequest' }
+
+ it_behaves_like 'find target'
+ it_behaves_like 'build target', type_id: nil
+ it_behaves_like 'build target', type_id: -1
+ end
+
+ context 'for commit' do
+ let(:project) { create(:project, :repository) }
+ let(:target) { project.commit.parent }
+ let(:target_id) { target.sha }
+ let(:type) { 'Commit' }
+
+ it_behaves_like 'find target'
+ it_behaves_like 'no target', type_id: 'invalid_sha'
+
+ context 'with nil target_id' do
+ let(:target) { project.commit }
+ let(:target_id) { nil }
+
+ it_behaves_like 'find target'
+ end
+ end
+
+ context 'for unknown type' do
+ let(:type) { 'unknown' }
+
+ it_behaves_like 'no target', type_id: :unused
+ end
+ end
+end
diff --git a/spec/services/system_note_service_spec.rb b/spec/services/system_note_service_spec.rb
index 57d081cffb3..442de61f69b 100644
--- a/spec/services/system_note_service_spec.rb
+++ b/spec/services/system_note_service_spec.rb
@@ -108,6 +108,25 @@ describe SystemNoteService do
end
end
+ describe '.tag_commit' do
+ let(:noteable) do
+ project.commit
+ end
+ let(:tag_name) { 'v1.2.3' }
+
+ subject { described_class.tag_commit(noteable, project, author, tag_name) }
+
+ it_behaves_like 'a system note' do
+ let(:action) { 'tag' }
+ end
+
+ it 'sets the note text' do
+ link = "http://localhost/#{project.full_path}/tags/#{tag_name}"
+
+ expect(subject.note).to eq "tagged commit #{noteable.sha} to [`#{tag_name}`](#{link})"
+ end
+ end
+
describe '.change_assignee' do
subject { described_class.change_assignee(noteable, project, author, assignee) }