Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Boxiang Sun
gitlab-ce
Commits
bc40efa1
Commit
bc40efa1
authored
Apr 06, 2014
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #6705 from razielgn/slack-service-attachments
Improved Slack integration with message attachments.
parents
3740597f
6a9eb4b2
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
38 additions
and
14 deletions
+38
-14
app/models/project_services/slack_message.rb
app/models/project_services/slack_message.rb
+20
-5
app/models/project_services/slack_service.rb
app/models/project_services/slack_service.rb
+1
-1
spec/models/slack_message_spec.rb
spec/models/slack_message_spec.rb
+17
-8
No files found.
app/models/project_services/slack_message.rb
View file @
bc40efa1
...
@@ -11,10 +11,16 @@ class SlackMessage
...
@@ -11,10 +11,16 @@ class SlackMessage
@username
=
params
.
fetch
(
:user_name
)
@username
=
params
.
fetch
(
:user_name
)
end
end
def
compose
def
pretext
format
(
message
)
format
(
message
)
end
end
def
attachments
return
[]
if
new_branch?
||
removed_branch?
commit_message_attachments
end
private
private
attr_reader
:after
attr_reader
:after
...
@@ -31,7 +37,7 @@ class SlackMessage
...
@@ -31,7 +37,7 @@ class SlackMessage
elsif
removed_branch?
elsif
removed_branch?
removed_branch_message
removed_branch_message
else
else
push_message
<<
commit_messages
push_message
end
end
end
end
...
@@ -54,15 +60,20 @@ class SlackMessage
...
@@ -54,15 +60,20 @@ class SlackMessage
def
commit_messages
def
commit_messages
commits
.
each_with_object
(
''
)
do
|
commit
,
str
|
commits
.
each_with_object
(
''
)
do
|
commit
,
str
|
str
<<
compose_commit_message
(
commit
)
str
<<
compose_commit_message
(
commit
)
end
.
chomp
end
end
def
commit_message_attachments
[{
text:
format
(
commit_messages
),
color:
attachment_color
}]
end
end
def
compose_commit_message
(
commit
)
def
compose_commit_message
(
commit
)
id
=
commit
.
fetch
(
:id
)[
0
..
5
]
author
=
commit
.
fetch
(
:author
).
fetch
(
:name
)
id
=
commit
.
fetch
(
:id
)[
0
..
8
]
message
=
commit
.
fetch
(
:message
)
message
=
commit
.
fetch
(
:message
)
url
=
commit
.
fetch
(
:url
)
url
=
commit
.
fetch
(
:url
)
"
\n
-
#{
message
}
([
#{
id
}
](
#{
url
}
))
"
"
[
#{
id
}
](
#{
url
}
):
#{
message
}
-
#{
author
}
\n
"
end
end
def
new_branch?
def
new_branch?
...
@@ -92,4 +103,8 @@ class SlackMessage
...
@@ -92,4 +103,8 @@ class SlackMessage
def
compare_link
def
compare_link
"[Compare changes](
#{
compare_url
}
)"
"[Compare changes](
#{
compare_url
}
)"
end
end
def
attachment_color
'#345'
end
end
end
app/models/project_services/slack_service.rb
View file @
bc40efa1
...
@@ -53,7 +53,7 @@ class SlackService < Service
...
@@ -53,7 +53,7 @@ class SlackService < Service
notifier
=
Slack
::
Notifier
.
new
(
subdomain
,
token
)
notifier
=
Slack
::
Notifier
.
new
(
subdomain
,
token
)
notifier
.
channel
=
room
notifier
.
channel
=
room
notifier
.
username
=
'GitLab'
notifier
.
username
=
'GitLab'
notifier
.
ping
(
message
.
compose
)
notifier
.
ping
(
message
.
pretext
,
attachments:
message
.
attachments
)
end
end
private
private
...
...
spec/models/slack_message_spec.rb
View file @
bc40efa1
...
@@ -14,20 +14,27 @@ describe SlackMessage do
...
@@ -14,20 +14,27 @@ describe SlackMessage do
}
}
}
}
let
(
:color
)
{
'#345'
}
context
'push'
do
context
'push'
do
before
do
before
do
args
[
:commits
]
=
[
args
[
:commits
]
=
[
{
message:
'message1'
,
url:
'url1'
,
id:
'abcdefghi
'
},
{
message:
'message1'
,
url:
'url1'
,
id:
'abcdefghi
jkl'
,
author:
{
name:
'author1'
}
},
{
message:
'message2'
,
url:
'url2'
,
id:
'123456789
'
},
{
message:
'message2'
,
url:
'url2'
,
id:
'123456789
012'
,
author:
{
name:
'author2'
}
},
]
]
end
end
it
'returns a message regarding pushes'
do
it
'returns a message regarding pushes'
do
subject
.
compose
.
should
==
subject
.
pretext
.
should
==
'user_name pushed to branch <url/commits/master|master> of '
<<
'user_name pushed to branch <url/commits/master|master> of '
<<
'<url|project_name> (<url/compare/before...after|Compare changes>)'
<<
'<url|project_name> (<url/compare/before...after|Compare changes>)'
"
\n
- message1 (<url1|abcdef>)"
<<
subject
.
attachments
.
should
==
[
"
\n
- message2 (<url2|123456>)"
{
text:
"<url1|abcdefghi>: message1 - author1
\n
"
<<
"<url2|123456789>: message2 - author2"
,
color:
color
,
}
]
end
end
end
end
...
@@ -37,9 +44,10 @@ describe SlackMessage do
...
@@ -37,9 +44,10 @@ describe SlackMessage do
end
end
it
'returns a message regarding a new branch'
do
it
'returns a message regarding a new branch'
do
subject
.
compose
.
should
==
subject
.
pretext
.
should
==
'user_name pushed new branch <url/commits/master|master> to '
<<
'user_name pushed new branch <url/commits/master|master> to '
<<
'<url|project_name>'
'<url|project_name>'
subject
.
attachments
.
should
be_empty
end
end
end
end
...
@@ -49,8 +57,9 @@ describe SlackMessage do
...
@@ -49,8 +57,9 @@ describe SlackMessage do
end
end
it
'returns a message regarding a removed branch'
do
it
'returns a message regarding a removed branch'
do
subject
.
compose
.
should
==
subject
.
pretext
.
should
==
'user_name removed branch master from <url|project_name>'
'user_name removed branch master from <url|project_name>'
subject
.
attachments
.
should
be_empty
end
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