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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
b4742f97
Commit
b4742f97
authored
Apr 20, 2020
by
Brian Kintz
Committed by
Mayra Cabrera
Apr 20, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add chatops responder for mattermost
parent
2bbc2053
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
264 additions
and
0 deletions
+264
-0
app/models/project_services/mattermost_slash_commands_service.rb
...els/project_services/mattermost_slash_commands_service.rb
+4
-0
changelogs/unreleased/mattermost-chat-responder.yml
changelogs/unreleased/mattermost-chat-responder.yml
+5
-0
lib/gitlab/chat/responder/mattermost.rb
lib/gitlab/chat/responder/mattermost.rb
+131
-0
spec/lib/gitlab/chat/responder/mattermost_spec.rb
spec/lib/gitlab/chat/responder/mattermost_spec.rb
+117
-0
spec/models/project_services/mattermost_slash_commands_service_spec.rb
...roject_services/mattermost_slash_commands_service_spec.rb
+7
-0
No files found.
app/models/project_services/mattermost_slash_commands_service.rb
View file @
b4742f97
...
@@ -36,6 +36,10 @@ class MattermostSlashCommandsService < SlashCommandsService
...
@@ -36,6 +36,10 @@ class MattermostSlashCommandsService < SlashCommandsService
[[],
e
.
message
]
[[],
e
.
message
]
end
end
def
chat_responder
::
Gitlab
::
Chat
::
Responder
::
Mattermost
end
private
private
def
command
(
params
)
def
command
(
params
)
...
...
changelogs/unreleased/mattermost-chat-responder.yml
0 → 100644
View file @
b4742f97
---
title
:
Add responding to ChatOps jobs triggered in Mattermost
merge_request
:
29366
author
:
Brian Kintz
type
:
added
lib/gitlab/chat/responder/mattermost.rb
0 → 100644
View file @
b4742f97
# frozen_string_literal: true
module
Gitlab
module
Chat
module
Responder
class
Mattermost
<
Responder
::
Base
SUCCESS_COLOR
=
'#00c100'
FAILURE_COLOR
=
'#e40303'
# Slack breaks messages apart if they're around 4 KB in size. We use a
# slightly smaller limit here to account for user mentions.
MESSAGE_SIZE_LIMIT
=
3.5
.
kilobytes
# Sends a response back to Mattermost
#
# body - The message payload to send back to Mattermost, as a Hash.
def
send_response
(
body
)
Gitlab
::
HTTP
.
post
(
pipeline
.
chat_data
.
response_url
,
{
headers:
{
'Content-Type'
:
'application/json'
},
body:
body
.
to_json
}
)
end
# Sends the output for a build that completed successfully.
#
# output - The output produced by the chat command.
def
success
(
output
)
return
if
output
.
empty?
send_response
(
response_type: :in_channel
,
attachments:
[
{
color:
SUCCESS_COLOR
,
text:
"ChatOps job started by
#{
user_ref
}
completed successfully"
,
fields:
[
{
short:
true
,
title:
"ID"
,
value:
"
#{
build_ref
}
"
},
{
short:
true
,
title:
"Name"
,
value:
build
.
name
},
{
short:
false
,
title:
"Output"
,
value:
success_message
(
output
)
}
]
}
]
)
end
# Sends the output for a build that failed.
def
failure
send_response
(
response_type: :in_channel
,
attachments:
[
{
color:
FAILURE_COLOR
,
text:
"ChatOps job started by
#{
user_ref
}
failed!"
,
fields:
[
{
short:
true
,
title:
"ID"
,
value:
"
#{
build_ref
}
"
},
{
short:
true
,
title:
"Name"
,
value:
build
.
name
}
]
}
]
)
end
# Returns the output to send back after a command has been scheduled.
def
scheduled_output
{
response_type: :ephemeral
,
text:
"Your ChatOps job
#{
build_ref
}
has been created!"
}
end
private
def
success_message
(
output
)
<<~
HEREDOC
.
chomp
```shell
#{
strip_ansi_colorcodes
(
limit_output
(
output
))
}
```
HEREDOC
end
def
limit_output
(
output
)
if
output
.
bytesize
<=
MESSAGE_SIZE_LIMIT
output
else
"The output is too large to be sent back directly!"
end
end
def
strip_ansi_colorcodes
(
output
)
output
.
gsub
(
/\x1b\[[0-9;]*m/
,
''
)
end
def
user_ref
user
=
pipeline
.
chat_data
.
chat_name
.
user
user_url
=
::
Gitlab
::
Routing
.
url_helpers
.
user_url
(
user
)
"[
#{
user
.
name
}
](
#{
user_url
}
)"
end
def
build_ref
build_url
=
::
Gitlab
::
Routing
.
url_helpers
.
project_build_url
(
project
,
build
)
"[#
#{
build
.
id
}
](
#{
build_url
}
)"
end
end
end
end
end
spec/lib/gitlab/chat/responder/mattermost_spec.rb
0 → 100644
View file @
b4742f97
# frozen_string_literal: true
require
'spec_helper'
describe
Gitlab
::
Chat
::
Responder
::
Mattermost
do
let
(
:chat_name
)
{
create
(
:chat_name
,
chat_id:
'U123'
)
}
let
(
:pipeline
)
do
pipeline
=
create
(
:ci_pipeline
)
pipeline
.
create_chat_data!
(
response_url:
'http://example.com'
,
chat_name_id:
chat_name
.
id
)
pipeline
end
let
(
:build
)
{
create
(
:ci_build
,
pipeline:
pipeline
)
}
let
(
:responder
)
{
described_class
.
new
(
build
)
}
describe
'#send_response'
do
it
'sends a response back to Slack'
do
expect
(
Gitlab
::
HTTP
).
to
receive
(
:post
).
with
(
'http://example.com'
,
{
headers:
{
'Content-Type'
:
'application/json'
},
body:
'hello'
.
to_json
}
)
responder
.
send_response
(
'hello'
)
end
end
describe
'#success'
do
it
'returns the output for a successful build'
do
expect
(
responder
)
.
to
receive
(
:send_response
)
.
with
(
hash_including
(
response_type: :in_channel
,
attachments:
array_including
(
a_hash_including
(
text:
/
#{
pipeline
.
chat_data
.
chat_name
.
user
.
name
}
.*completed successfully/
,
fields:
array_including
(
a_hash_including
(
value:
/#
#{
build
.
id
}
/
),
a_hash_including
(
value:
build
.
name
),
a_hash_including
(
value:
"```shell
\n
script output
\n
```"
)
)
)
)
)
)
responder
.
success
(
'[0;m[32;1mscript output[0;m'
)
end
it
'limits the output to a fixed size'
do
expect
(
responder
)
.
to
receive
(
:send_response
)
.
with
(
hash_including
(
response_type: :in_channel
,
attachments:
array_including
(
a_hash_including
(
fields:
array_including
(
a_hash_including
(
value:
/The output is too large/
)
)
)
)
)
)
responder
.
success
(
'a'
*
4000
)
end
it
'does not send a response if the output is empty'
do
expect
(
responder
).
not_to
receive
(
:send_response
)
responder
.
success
(
''
)
end
end
describe
'#failure'
do
it
'returns the output for a failed build'
do
expect
(
responder
)
.
to
receive
(
:send_response
)
.
with
(
hash_including
(
response_type: :in_channel
,
attachments:
array_including
(
a_hash_including
(
text:
/
#{
pipeline
.
chat_data
.
chat_name
.
user
.
name
}
.*failed/
,
fields:
array_including
(
a_hash_including
(
value:
/#
#{
build
.
id
}
/
),
a_hash_including
(
value:
build
.
name
)
)
)
)
)
)
responder
.
failure
end
end
describe
'#scheduled_output'
do
it
'returns the output for a scheduled build'
do
output
=
responder
.
scheduled_output
expect
(
output
).
to
match
(
hash_including
(
response_type: :ephemeral
,
text:
/#
#{
build
.
id
}
/
)
)
end
end
end
spec/models/project_services/mattermost_slash_commands_service_spec.rb
View file @
b4742f97
...
@@ -121,5 +121,12 @@ describe MattermostSlashCommandsService do
...
@@ -121,5 +121,12 @@ describe MattermostSlashCommandsService do
end
end
end
end
end
end
describe
'#chat_responder'
do
it
'returns the responder to use for Mattermost'
do
expect
(
described_class
.
new
.
chat_responder
)
.
to
eq
(
Gitlab
::
Chat
::
Responder
::
Mattermost
)
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