BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
7209ea4f
Commit
7209ea4f
authored
Aug 01, 2016
by
Lin Jen-Shin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Slack pipeline events test and fix some issues along the way
parent
cc4b0a55
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
80 additions
and
16 deletions
+80
-16
slack_service.rb
app/models/project_services/slack_service.rb
+10
-6
pipeline_message.rb
...models/project_services/slack_service/pipeline_message.rb
+8
-6
slack_service_spec.rb
spec/models/project_services/slack_service_spec.rb
+62
-4
No files found.
app/models/project_services/slack_service.rb
View file @
7209ea4f
...
@@ -66,16 +66,20 @@ class SlackService < Service
...
@@ -66,16 +66,20 @@ class SlackService < Service
message
=
get_message
(
object_kind
,
data
)
message
=
get_message
(
object_kind
,
data
)
opt
=
{}
if
message
opt
=
{}
event_channel
=
get_channel_field
(
object_kind
)
||
channel
event_channel
=
get_channel_field
(
object_kind
)
||
channel
opt
[
:channel
]
=
event_channel
if
event_channel
opt
[
:channel
]
=
event_channel
if
event_channel
opt
[
:username
]
=
username
if
username
opt
[
:username
]
=
username
if
username
if
message
notifier
=
Slack
::
Notifier
.
new
(
webhook
,
opt
)
notifier
=
Slack
::
Notifier
.
new
(
webhook
,
opt
)
notifier
.
ping
(
message
.
pretext
,
attachments:
message
.
attachments
,
fallback:
message
.
fallback
)
notifier
.
ping
(
message
.
pretext
,
attachments:
message
.
attachments
,
fallback:
message
.
fallback
)
true
else
false
end
end
end
end
...
@@ -153,7 +157,7 @@ class SlackService < Service
...
@@ -153,7 +157,7 @@ class SlackService < Service
def
should_pipeline_be_notified?
(
data
)
def
should_pipeline_be_notified?
(
data
)
case
data
[
:object_attributes
][
:status
]
case
data
[
:object_attributes
][
:status
]
when
'success'
when
'success'
!
notify_only_broken_
build
s?
!
notify_only_broken_
pipeline
s?
when
'failed'
when
'failed'
true
true
else
else
...
...
app/models/project_services/slack_service/pipeline_message.rb
View file @
7209ea4f
...
@@ -4,15 +4,17 @@ class SlackService
...
@@ -4,15 +4,17 @@ class SlackService
:user_name
,
:duration
,
:pipeline_id
:user_name
,
:duration
,
:pipeline_id
def
initialize
(
data
)
def
initialize
(
data
)
@sha
=
data
[
:sha
]
pipeline_attributes
=
data
[
:object_attributes
]
@ref_type
=
data
[
:tag
]
?
'tag'
:
'branch'
@sha
=
pipeline_attributes
[
:sha
]
@ref
=
data
[
:ref
]
@ref_type
=
pipeline_attributes
[
:tag
]
?
'tag'
:
'branch'
@status
=
data
[
:status
]
@ref
=
pipeline_attributes
[
:ref
]
@status
=
pipeline_attributes
[
:status
]
@duration
=
pipeline_attributes
[
:duration
]
@pipeline_id
=
pipeline_attributes
[
:id
]
@project_name
=
data
[
:project
][
:path_with_namespace
]
@project_name
=
data
[
:project
][
:path_with_namespace
]
@project_url
=
data
[
:project
][
:web_url
]
@project_url
=
data
[
:project
][
:web_url
]
@user_name
=
data
[
:commit
]
&&
data
[
:commit
][
:author_name
]
@user_name
=
data
[
:commit
]
&&
data
[
:commit
][
:author_name
]
@duration
=
data
[
:object_attributes
][
:duration
]
@pipeline_id
=
data
[
:object_attributes
][
:id
]
end
end
def
pretext
def
pretext
...
...
spec/models/project_services/slack_service_spec.rb
View file @
7209ea4f
...
@@ -21,6 +21,9 @@
...
@@ -21,6 +21,9 @@
require
'spec_helper'
require
'spec_helper'
describe
SlackService
,
models:
true
do
describe
SlackService
,
models:
true
do
let
(
:slack
)
{
SlackService
.
new
}
let
(
:webhook_url
)
{
'https://hooks.slack.com/services/SVRWFV0VVAR97N/B02R25XN3/ZBqu7xMupaEEICInN685'
}
describe
"Associations"
do
describe
"Associations"
do
it
{
is_expected
.
to
belong_to
:project
}
it
{
is_expected
.
to
belong_to
:project
}
it
{
is_expected
.
to
have_one
:service_hook
}
it
{
is_expected
.
to
have_one
:service_hook
}
...
@@ -42,11 +45,9 @@ describe SlackService, models: true do
...
@@ -42,11 +45,9 @@ describe SlackService, models: true do
end
end
describe
"Execute"
do
describe
"Execute"
do
let
(
:slack
)
{
SlackService
.
new
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:push_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
(
:username
)
{
'slack_username'
}
let
(
:channel
)
{
'slack_channel'
}
let
(
:channel
)
{
'slack_channel'
}
...
@@ -210,10 +211,8 @@ describe SlackService, models: true do
...
@@ -210,10 +211,8 @@ describe SlackService, models: true do
end
end
describe
"Note events"
do
describe
"Note events"
do
let
(
:slack
)
{
SlackService
.
new
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:project
)
{
create
(
:project
,
creator_id:
user
.
id
)
}
let
(
:project
)
{
create
(
:project
,
creator_id:
user
.
id
)
}
let
(
:webhook_url
)
{
'https://hooks.slack.com/services/SVRWFV0VVAR97N/B02R25XN3/ZBqu7xMupaEEICInN685'
}
before
do
before
do
allow
(
slack
).
to
receive_messages
(
allow
(
slack
).
to
receive_messages
(
...
@@ -283,4 +282,63 @@ describe SlackService, models: true do
...
@@ -283,4 +282,63 @@ describe SlackService, models: true do
end
end
end
end
end
end
describe
'Pipeline events'
do
let
(
:user
)
{
create
(
:user
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:pipeline
)
do
create
(
:ci_pipeline
,
project:
project
,
status:
status
,
sha:
project
.
commit
.
sha
,
ref:
project
.
default_branch
)
end
let
(
:status
)
{
raise
NotImplementedError
}
before
do
allow
(
slack
).
to
receive_messages
(
project:
project
,
service_hook:
true
,
webhook:
webhook_url
)
end
shared_examples
'call Slack API'
do
before
do
WebMock
.
stub_request
(
:post
,
webhook_url
)
end
it
'calls Slack API for pipeline events'
do
data
=
Gitlab
::
DataBuilder
::
PipelineDataBuilder
.
build
(
pipeline
)
slack
.
execute
(
data
)
expect
(
WebMock
).
to
have_requested
(
:post
,
webhook_url
).
once
end
end
context
'with failed pipeline'
do
let
(
:status
)
{
'failed'
}
it_behaves_like
'call Slack API'
end
context
'with succeeded pipeline'
do
let
(
:status
)
{
'success'
}
context
'with default to notify_only_broken_pipelines'
do
it
'does not call Slack API for pipeline events'
do
data
=
Gitlab
::
DataBuilder
::
PipelineDataBuilder
.
build
(
pipeline
)
result
=
slack
.
execute
(
data
)
expect
(
result
).
to
be_falsy
end
end
context
'with setting notify_only_broken_pipelines to false'
do
before
do
slack
.
notify_only_broken_pipelines
=
false
end
it_behaves_like
'call Slack API'
end
end
end
end
end
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment