Read about our upcoming Code of Conduct on this issue

Commit afe5d7d2 authored by Stan Hu's avatar Stan Hu Committed by Douwe Maan
Browse files

Issue #595: Support Slack notifications upon issue and merge request events

1) Adds a DB migration for all services to toggle on push, issue, and merge events.

2) Upon an issue or merge request event, fire service hooks.

3) Slack service supports custom messages for each of these events. Other services
not supported at the moment.

4) Label merge request hooks with their corresponding actions.
parent 7fb58d4fc844
......@@ -2,14 +2,18 @@
#
# Table name: services
#
# id :integer not null, primary key
# type :string(255)
# title :string(255)
# project_id :integer not null
# created_at :datetime
# updated_at :datetime
# active :boolean default(FALSE), not null
# properties :text
# id :integer not null, primary key
# type :string(255)
# title :string(255)
# project_id :integer not null
# created_at :datetime
# updated_at :datetime
# active :boolean default(FALSE), not null
# properties :text
# push_events :boolean
# issues_events :boolean
# merge_requests_events :boolean
# tag_push_events :boolean
#
require 'spec_helper'
......
require 'spec_helper'
describe SlackMessages::SlackIssueMessage do
subject { SlackMessages::SlackIssueMessage.new(args) }
let(:args) {
{
user: {
username: 'username'
},
project_name: 'project_name',
project_url: 'somewhere.com',
object_attributes: {
title: 'Issue title',
id: 10,
iid: 100,
assignee_id: 1,
url: 'url',
action: 'open',
state: 'opened',
description: 'issue description'
}
}
}
let(:color) { '#345' }
context 'open' do
it 'returns a message regarding opening of issues' do
expect(subject.pretext).to eq(
'username opened issue <url|#100> in <somewhere.com|project_name>: '\
'Issue title')
expect(subject.attachments).to eq([
{
text: "issue description",
color: color,
}
])
end
end
context 'close' do
before do
args[:object_attributes][:action] = 'close'
args[:object_attributes][:state] = 'closed'
end
it 'returns a message regarding closing of issues' do
expect(subject.pretext). to eq(
'username closed issue <url|#100> in <somewhere.com|project_name>: '\
'Issue title')
expect(subject.attachments).to be_empty
end
end
end
require 'spec_helper'
describe SlackMessages::SlackMergeMessage do
subject { SlackMessages::SlackMergeMessage.new(args) }
let(:args) {
{
user: {
username: 'username'
},
project_name: 'project_name',
project_url: 'somewhere.com',
object_attributes: {
title: 'Issue title',
id: 10,
iid: 100,
assignee_id: 1,
url: 'url',
state: 'opened',
description: 'issue description',
source_branch: 'source_branch',
target_branch: 'target_branch',
}
}
}
let(:color) { '#345' }
context 'open' do
it 'returns a message regarding opening of merge requests' do
expect(subject.pretext).to eq(
'username opened merge request <somewhere.com/merge_requests/100|#100> '\
'in <somewhere.com|project_name>')
expect(subject.attachments).to be_empty
end
end
context 'close' do
before do
args[:object_attributes][:state] = 'closed'
end
it 'returns a message regarding closing of merge requests' do
expect(subject.pretext).to eq(
'username closed merge request <somewhere.com/merge_requests/100|#100> '\
'in <somewhere.com|project_name>')
expect(subject.attachments).to be_empty
end
end
end
require 'spec_helper'
describe SlackMessage do
subject { SlackMessage.new(args) }
describe SlackMessages::SlackPushMessage do
subject { SlackMessages::SlackPushMessage.new(args) }
let(:args) {
{
......
......@@ -2,14 +2,18 @@
#
# Table name: services
#
# id :integer not null, primary key
# type :string(255)
# title :string(255)
# project_id :integer not null
# created_at :datetime
# updated_at :datetime
# active :boolean default(FALSE), not null
# properties :text
# id :integer not null, primary key
# type :string(255)
# title :string(255)
# project_id :integer not null
# created_at :datetime
# updated_at :datetime
# active :boolean default(FALSE), not null
# properties :text
# push_events :boolean
# issues_events :boolean
# merge_requests_events :boolean
# tag_push_events :boolean
#
require 'spec_helper'
......@@ -34,7 +38,7 @@
let(:slack) { SlackService.new }
let(:user) { create(:user) }
let(:project) { create(:project) }
let(:sample_data) { Gitlab::PushDataBuilder.build_sample(project, user) }
let(:push_sample_data) { Gitlab::PushDataBuilder.build_sample(project, user) }
let(:webhook_url) { 'https://hooks.slack.com/services/SVRWFV0VVAR97N/B02R25XN3/ZBqu7xMupaEEICInN685' }
let(:username) { 'slack_username' }
let(:channel) { 'slack_channel' }
......@@ -48,10 +52,43 @@
)
WebMock.stub_request(:post, webhook_url)
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',
source_branch: 'stable',
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')
end
it "should call Slack API" do
slack.execute(sample_data)
it "should call Slack API for pull requests" do
slack.execute(push_sample_data)
WebMock.should have_requested(:post, webhook_url).once
end
it "should call Slack API for issue events" do
slack.execute(@issues_sample_data)
WebMock.should have_requested(:post, webhook_url).once
end
it "should call Slack API for merge requests events" do
slack.execute(@merge_sample_data)
expect(WebMock).to have_requested(:post, webhook_url).once
end
......
......@@ -11,6 +11,10 @@
# active :boolean default(FALSE), not null
# properties :text
# template :boolean default(FALSE)
# push_events :boolean
# issues_events :boolean
# merge_requests_events :boolean
# tag_push_events :boolean
#
require 'spec_helper'
......
......@@ -49,6 +49,7 @@
subject { @push_data }
it { is_expected.to include(object_kind: 'push') }
it { is_expected.to include(before: @oldrev) }
it { is_expected.to include(after: @newrev) }
it { is_expected.to include(ref: @ref) }
......
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