2014-03-18 13:27:03 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: services
|
|
|
|
#
|
2015-02-19 00:02:57 -05:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# type :string(255)
|
|
|
|
# title :string(255)
|
2015-03-04 17:14:00 -05:00
|
|
|
# project_id :integer
|
2015-02-19 00:02:57 -05:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
# active :boolean default(FALSE), not null
|
|
|
|
# properties :text
|
2015-03-04 17:14:00 -05:00
|
|
|
# template :boolean default(FALSE)
|
|
|
|
# push_events :boolean default(TRUE)
|
|
|
|
# issues_events :boolean default(TRUE)
|
|
|
|
# merge_requests_events :boolean default(TRUE)
|
|
|
|
# tag_push_events :boolean default(TRUE)
|
2015-05-03 12:05:38 -04:00
|
|
|
# note_events :boolean default(TRUE), not null
|
2014-03-18 13:27:03 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 04:50:51 -05:00
|
|
|
describe SlackService, models: true do
|
2014-03-18 13:27:03 -04:00
|
|
|
describe "Associations" do
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to belong_to :project }
|
|
|
|
it { is_expected.to have_one :service_hook }
|
2014-03-18 13:27:03 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "Validations" do
|
|
|
|
context "active" do
|
|
|
|
before do
|
|
|
|
subject.active = true
|
|
|
|
end
|
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
it { is_expected.to validate_presence_of :webhook }
|
2014-03-18 13:27:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "Execute" do
|
2014-10-23 14:47:28 -04:00
|
|
|
let(:slack) { SlackService.new }
|
|
|
|
let(:user) { create(:user) }
|
2014-03-18 13:27:03 -04:00
|
|
|
let(:project) { create(:project) }
|
2015-02-19 00:02:57 -05:00
|
|
|
let(:push_sample_data) { Gitlab::PushDataBuilder.build_sample(project, user) }
|
2014-10-23 14:47:28 -04:00
|
|
|
let(:webhook_url) { 'https://hooks.slack.com/services/SVRWFV0VVAR97N/B02R25XN3/ZBqu7xMupaEEICInN685' }
|
2014-12-28 15:08:33 -05:00
|
|
|
let(:username) { 'slack_username' }
|
|
|
|
let(:channel) { 'slack_channel' }
|
2014-03-18 13:27:03 -04:00
|
|
|
|
|
|
|
before do
|
2015-05-21 17:49:06 -04:00
|
|
|
allow(slack).to receive_messages(
|
2014-03-18 13:27:03 -04:00
|
|
|
project: project,
|
|
|
|
project_id: project.id,
|
|
|
|
service_hook: true,
|
2014-10-23 14:47:28 -04:00
|
|
|
webhook: webhook_url
|
2014-03-18 13:27:03 -04:00
|
|
|
)
|
|
|
|
|
2014-10-23 14:47:28 -04:00
|
|
|
WebMock.stub_request(:post, webhook_url)
|
2015-02-19 00:02:57 -05:00
|
|
|
|
|
|
|
opts = {
|
|
|
|
title: 'Awesome issue',
|
|
|
|
description: 'please fix'
|
|
|
|
}
|
|
|
|
|
|
|
|
issue_service = Issues::CreateService.new(project, user, opts)
|
|
|
|
@issue = issue_service.execute
|
|
|
|
@issues_sample_data = issue_service.hook_data(@issue, 'open')
|
|
|
|
|
|
|
|
opts = {
|
|
|
|
title: 'Awesome merge_request',
|
|
|
|
description: 'please fix',
|
2015-08-11 08:33:31 -04:00
|
|
|
source_branch: 'feature',
|
2015-02-19 00:02:57 -05:00
|
|
|
target_branch: 'master'
|
|
|
|
}
|
|
|
|
merge_service = MergeRequests::CreateService.new(project,
|
|
|
|
user, opts)
|
|
|
|
@merge_request = merge_service.execute
|
|
|
|
@merge_sample_data = merge_service.hook_data(@merge_request,
|
|
|
|
'open')
|
2014-03-18 13:27:03 -04:00
|
|
|
end
|
|
|
|
|
2015-03-05 13:38:23 -05:00
|
|
|
it "should call Slack API for push events" do
|
2015-02-19 00:02:57 -05:00
|
|
|
slack.execute(push_sample_data)
|
|
|
|
|
2015-03-05 13:38:23 -05:00
|
|
|
expect(WebMock).to have_requested(:post, webhook_url).once
|
2015-02-19 00:02:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should call Slack API for issue events" do
|
|
|
|
slack.execute(@issues_sample_data)
|
|
|
|
|
2015-03-05 13:38:23 -05:00
|
|
|
expect(WebMock).to have_requested(:post, webhook_url).once
|
2015-02-19 00:02:57 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should call Slack API for merge requests events" do
|
|
|
|
slack.execute(@merge_sample_data)
|
2014-03-18 13:27:03 -04:00
|
|
|
|
2015-02-12 13:17:35 -05:00
|
|
|
expect(WebMock).to have_requested(:post, webhook_url).once
|
2014-10-21 05:33:26 -04:00
|
|
|
end
|
2014-12-28 15:08:33 -05:00
|
|
|
|
|
|
|
it 'should use the username as an option for slack when configured' do
|
2015-05-21 17:49:06 -04:00
|
|
|
allow(slack).to receive(:username).and_return(username)
|
2014-12-28 15:08:33 -05:00
|
|
|
expect(Slack::Notifier).to receive(:new).
|
2015-03-05 13:38:23 -05:00
|
|
|
with(webhook_url, username: username).
|
|
|
|
and_return(
|
|
|
|
double(:slack_service).as_null_object
|
|
|
|
)
|
2015-03-03 07:09:45 -05:00
|
|
|
slack.execute(push_sample_data)
|
2014-12-28 15:08:33 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'should use the channel as an option when it is configured' do
|
2015-05-21 17:49:06 -04:00
|
|
|
allow(slack).to receive(:channel).and_return(channel)
|
2014-12-28 15:08:33 -05:00
|
|
|
expect(Slack::Notifier).to receive(:new).
|
|
|
|
with(webhook_url, channel: channel).
|
|
|
|
and_return(
|
|
|
|
double(:slack_service).as_null_object
|
|
|
|
)
|
2015-03-03 07:09:45 -05:00
|
|
|
slack.execute(push_sample_data)
|
2014-12-28 15:08:33 -05:00
|
|
|
end
|
2014-03-18 13:27:03 -04:00
|
|
|
end
|
2015-03-05 13:38:23 -05:00
|
|
|
|
|
|
|
describe "Note events" do
|
|
|
|
let(:slack) { SlackService.new }
|
|
|
|
let(:user) { create(:user) }
|
|
|
|
let(:project) { create(:project, creator_id: user.id) }
|
|
|
|
let(:issue) { create(:issue, project: project) }
|
|
|
|
let(:merge_request) { create(:merge_request, source_project: project, target_project: project) }
|
|
|
|
let(:snippet) { create(:project_snippet, project: project) }
|
|
|
|
let(:commit_note) { create(:note_on_commit, author: user, project: project, commit_id: project.repository.commit.id, note: 'a comment on a commit') }
|
|
|
|
let(:merge_request_note) { create(:note_on_merge_request, noteable_id: merge_request.id, note: "merge request note") }
|
|
|
|
let(:issue_note) { create(:note_on_issue, noteable_id: issue.id, note: "issue note")}
|
|
|
|
let(:snippet_note) { create(:note_on_project_snippet, noteable_id: snippet.id, note: "snippet note") }
|
|
|
|
let(:webhook_url) { 'https://hooks.slack.com/services/SVRWFV0VVAR97N/B02R25XN3/ZBqu7xMupaEEICInN685' }
|
|
|
|
|
|
|
|
before do
|
2015-05-21 17:49:06 -04:00
|
|
|
allow(slack).to receive_messages(
|
|
|
|
project: project,
|
|
|
|
project_id: project.id,
|
|
|
|
service_hook: true,
|
|
|
|
webhook: webhook_url
|
2015-03-05 13:38:23 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
WebMock.stub_request(:post, webhook_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call Slack API for commit comment events" do
|
|
|
|
data = Gitlab::NoteDataBuilder.build(commit_note, user)
|
|
|
|
slack.execute(data)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, webhook_url).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call Slack API for merge request comment events" do
|
|
|
|
data = Gitlab::NoteDataBuilder.build(merge_request_note, user)
|
|
|
|
slack.execute(data)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, webhook_url).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call Slack API for issue comment events" do
|
|
|
|
data = Gitlab::NoteDataBuilder.build(issue_note, user)
|
|
|
|
slack.execute(data)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, webhook_url).once
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should call Slack API for snippet comment events" do
|
|
|
|
data = Gitlab::NoteDataBuilder.build(snippet_note, user)
|
|
|
|
slack.execute(data)
|
|
|
|
|
|
|
|
expect(WebMock).to have_requested(:post, webhook_url).once
|
|
|
|
end
|
|
|
|
end
|
2014-03-18 13:27:03 -04:00
|
|
|
end
|