summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2018-09-03 09:46:21 +0000
committerNick Thomas <nick@gitlab.com>2018-09-03 09:46:21 +0000
commit85b8206c417387c3716ccdc9257bed948f335935 (patch)
tree33d2d1980fe6a4237db7a60967db4bf57e117c78
parent91ed0dbc8cd96b63e5fca32fc127df9262b2dcd2 (diff)
parent7a57e59e1af9c2e3439c3d079515ea9164d16451 (diff)
downloadgitlab-ce-85b8206c417387c3716ccdc9257bed948f335935.tar.gz
Merge branch '50564-chat-service-refactoring' into 'master'
Use sample data if repo has no commit in ChatService Closes #50564 See merge request gitlab-org/gitlab-ce!21440
-rw-r--r--changelogs/unreleased/50564-chat-service-refactoring.yml5
-rw-r--r--lib/gitlab/data_builder/push.rb8
-rw-r--r--spec/models/project_services/chat_notification_service_spec.rb50
3 files changed, 61 insertions, 2 deletions
diff --git a/changelogs/unreleased/50564-chat-service-refactoring.yml b/changelogs/unreleased/50564-chat-service-refactoring.yml
new file mode 100644
index 00000000000..aec5e8fab0a
--- /dev/null
+++ b/changelogs/unreleased/50564-chat-service-refactoring.yml
@@ -0,0 +1,5 @@
+---
+title: Use sample data for push event when no commits created
+merge_request: 21440
+author: Takuya Noguchi
+type: fixed
diff --git a/lib/gitlab/data_builder/push.rb b/lib/gitlab/data_builder/push.rb
index c169c8fe135..b498f113859 100644
--- a/lib/gitlab/data_builder/push.rb
+++ b/lib/gitlab/data_builder/push.rb
@@ -97,11 +97,15 @@ module Gitlab
}
end
- # This method provide a sample data generated with
+ # This method provides a sample data generated with
# existing project and commits to test webhooks
def build_sample(project, user)
+ # Use sample data if repo has no commit
+ # (expect the case of test service configuration settings)
+ return sample_data if project.empty_repo?
+
ref = "#{Gitlab::Git::BRANCH_REF_PREFIX}#{project.default_branch}"
- commits = project.repository.commits(project.default_branch.to_s, limit: 3) rescue []
+ commits = project.repository.commits(project.default_branch.to_s, limit: 3)
build(project, user, commits.last&.id, commits.first&.id, ref, commits)
end
diff --git a/spec/models/project_services/chat_notification_service_spec.rb b/spec/models/project_services/chat_notification_service_spec.rb
index 3aa1039d8bf..46713df77da 100644
--- a/spec/models/project_services/chat_notification_service_spec.rb
+++ b/spec/models/project_services/chat_notification_service_spec.rb
@@ -26,4 +26,54 @@ describe ChatNotificationService do
end
end
end
+
+ describe '#execute' do
+ let(:chat_service) { described_class.new }
+ let(:user) { create(:user) }
+ let(:project) { create(:project, :repository) }
+ let(:webhook_url) { 'https://example.gitlab.com/' }
+
+ before do
+ allow(chat_service).to receive_messages(
+ project: project,
+ project_id: project.id,
+ service_hook: true,
+ webhook: webhook_url
+ )
+
+ WebMock.stub_request(:post, webhook_url)
+
+ subject.active = true
+ end
+
+ context 'with a repository' do
+ it 'returns true' do
+ subject.project = project
+ data = Gitlab::DataBuilder::Push.build_sample(project, user)
+
+ expect(Slack::Notifier).to receive(:new)
+ .with(webhook_url, {})
+ .and_return(
+ double(:slack_service).as_null_object
+ )
+
+ expect(chat_service.execute(data)).to be true
+ end
+ end
+
+ context 'with an empty repository' do
+ it 'returns true' do
+ subject.project = create(:project, :empty_repo)
+ data = Gitlab::DataBuilder::Push.build_sample(subject.project, user)
+
+ expect(Slack::Notifier).to receive(:new)
+ .with(webhook_url, {})
+ .and_return(
+ double(:slack_service).as_null_object
+ )
+
+ expect(chat_service.execute(data)).to be true
+ end
+ end
+ end
end