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
Tatuya Kamada
gitlab-ce
Commits
cc83aded
Commit
cc83aded
authored
Dec 16, 2016
by
Kamil Trzcinski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Render format dependent links
parent
dc995daf
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
132 additions
and
99 deletions
+132
-99
app/models/project_services/chat_slash_commands_service.rb
app/models/project_services/chat_slash_commands_service.rb
+11
-2
app/models/project_services/mattermost_slash_commands_service.rb
...els/project_services/mattermost_slash_commands_service.rb
+4
-0
app/models/project_services/slack_slash_commands_service.rb
app/models/project_services/slack_slash_commands_service.rb
+4
-0
lib/gitlab/chat_commands/base_command.rb
lib/gitlab/chat_commands/base_command.rb
+4
-0
lib/gitlab/chat_commands/command.rb
lib/gitlab/chat_commands/command.rb
+3
-3
lib/gitlab/chat_commands/presenter.rb
lib/gitlab/chat_commands/presenter.rb
+106
-94
No files found.
app/models/project_services/chat_slash_commands_service.rb
View file @
cc83aded
...
@@ -33,10 +33,11 @@ class ChatSlashCommandsService < Service
...
@@ -33,10 +33,11 @@ class ChatSlashCommandsService < Service
user
=
find_chat_user
(
params
)
user
=
find_chat_user
(
params
)
unless
user
unless
user
url
=
authorize_chat_name_url
(
params
)
url
=
authorize_chat_name_url
(
params
)
return
Gitlab
::
ChatCommands
::
P
resenter
.
authorize_chat_name
(
url
)
return
p
resenter
.
authorize_chat_name
(
url
)
end
end
Gitlab
::
ChatCommands
::
Command
.
new
(
project
,
user
,
params
).
execute
Gitlab
::
ChatCommands
::
Command
.
new
(
project
,
user
,
params
.
merge
(
presenter_format:
presenter_format
)).
execute
end
end
private
private
...
@@ -48,4 +49,12 @@ class ChatSlashCommandsService < Service
...
@@ -48,4 +49,12 @@ class ChatSlashCommandsService < Service
def
authorize_chat_name_url
(
params
)
def
authorize_chat_name_url
(
params
)
ChatNames
::
AuthorizeUserService
.
new
(
self
,
params
).
execute
ChatNames
::
AuthorizeUserService
.
new
(
self
,
params
).
execute
end
end
def
presenter
Gitlab
::
ChatCommands
::
Presenter
.
new
(
presenter_format
)
end
def
presenter_format
throw
NotImplementedError
end
end
end
app/models/project_services/mattermost_slash_commands_service.rb
View file @
cc83aded
...
@@ -18,4 +18,8 @@ class MattermostSlashCommandsService < ChatService
...
@@ -18,4 +18,8 @@ class MattermostSlashCommandsService < ChatService
def
to_param
def
to_param
'mattermost_slash_commands'
'mattermost_slash_commands'
end
end
def
presenter_format
'mattermost'
end
end
end
app/models/project_services/slack_slash_commands_service.rb
View file @
cc83aded
...
@@ -12,4 +12,8 @@ class SlackSlashCommandsService < ChatSlashCommandsService
...
@@ -12,4 +12,8 @@ class SlackSlashCommandsService < ChatSlashCommandsService
def
to_param
def
to_param
'slack_slash_commands'
'slack_slash_commands'
end
end
def
presenter_format
'slack'
end
end
end
lib/gitlab/chat_commands/base_command.rb
View file @
cc83aded
...
@@ -42,6 +42,10 @@ module Gitlab
...
@@ -42,6 +42,10 @@ module Gitlab
def
find_by_iid
(
iid
)
def
find_by_iid
(
iid
)
collection
.
find_by
(
iid:
iid
)
collection
.
find_by
(
iid:
iid
)
end
end
def
presenter
Gitlab
::
ChatCommands
::
Presenter
.
new
(
params
[
:presenter_format
])
end
end
end
end
end
end
end
lib/gitlab/chat_commands/command.rb
View file @
cc83aded
...
@@ -48,15 +48,15 @@ module Gitlab
...
@@ -48,15 +48,15 @@ module Gitlab
end
end
def
help
(
messages
)
def
help
(
messages
)
Mattermost
::
P
resenter
.
help
(
messages
,
params
[
:command
])
p
resenter
.
help
(
messages
,
params
[
:command
])
end
end
def
access_denied
def
access_denied
Mattermost
::
P
resenter
.
access_denied
p
resenter
.
access_denied
end
end
def
present
(
resource
)
def
present
(
resource
)
Mattermost
::
P
resenter
.
present
(
resource
)
p
resenter
.
present
(
resource
)
end
end
end
end
end
end
...
...
lib/gitlab/chat_commands/presenter.rb
View file @
cc83aded
module
Gitlab
module
Gitlab
class
ChatCommands
class
ChatCommands
class
Presenter
class
Presenter
class
<<
self
include
Gitlab
::
Routing
.
url_helpers
include
Gitlab
::
Routing
.
url_helpers
def
authorize_chat_name
(
url
)
attr_reader
:format
message
=
if
url
":wave: Hi there! Before I do anything for you, please <
#{
url
}
|connect your GitLab account>."
else
":sweat_smile: Couldn't identify you, nor can I autorize you!"
end
ephemeral_response
(
message
)
def
initialize
(
format
)
end
@format
=
format
end
def
help
(
commands
,
trigger
)
def
authorize_chat_name
(
url
)
if
commands
.
none?
message
=
if
url
ephemeral_response
(
"No commands configured"
)
":wave: Hi there! Before I do anything for you, please
#{
link
(
url
,
'connect your GitLab account'
)
}
."
else
else
commands
.
map!
{
|
command
|
"
#{
trigger
}
#{
command
}
"
}
":sweat_smile: Couldn't identify you, nor can I autorize you!"
message
=
header_with_list
(
"Available commands"
,
commands
)
end
ephemeral_response
(
message
)
ephemeral_response
(
message
)
end
end
def
help
(
commands
,
trigger
)
if
commands
.
none?
ephemeral_response
(
"No commands configured"
)
else
commands
.
map!
{
|
command
|
"
#{
trigger
}
#{
command
}
"
}
message
=
header_with_list
(
"Available commands"
,
commands
)
ephemeral_response
(
message
)
end
end
end
def
present
(
subject
)
def
present
(
subject
)
return
not_found
unless
subject
return
not_found
unless
subject
if
subject
.
is_a?
(
Gitlab
::
ChatCommands
::
Result
)
if
subject
.
is_a?
(
Gitlab
::
ChatCommands
::
Result
)
show_result
(
subject
)
show_result
(
subject
)
elsif
subject
.
respond_to?
(
:count
)
elsif
subject
.
respond_to?
(
:count
)
if
subject
.
many?
if
subject
.
many?
multiple_resources
(
subject
)
multiple_resources
(
subject
)
elsif
subject
.
none?
elsif
subject
.
none?
not_found
not_found
else
single_resource
(
subject
)
end
else
else
single_resource
(
subject
)
single_resource
(
subject
)
end
end
else
single_resource
(
subject
)
end
end
end
def
access_denied
def
access_denied
ephemeral_response
(
"Whoops! That action is not allowed. This incident will be <https://xkcd.com/838/|reported>
."
)
ephemeral_response
(
"Whoops! That action is not allowed. This incident will be
#{
link
(
'https://xkcd.com/838/'
,
'reported'
)
}
."
)
end
end
private
private
def
show_result
(
result
)
def
show_result
(
result
)
case
result
.
type
case
result
.
type
when
:success
when
:success
in_channel_response
(
result
.
message
)
in_channel_response
(
result
.
message
)
else
else
ephemeral_response
(
result
.
message
)
ephemeral_response
(
result
.
message
)
end
end
end
end
def
not_found
def
not_found
ephemeral_response
(
"404 not found! GitLab couldn't find what you were looking for! :boom:"
)
ephemeral_response
(
"404 not found! GitLab couldn't find what you were looking for! :boom:"
)
end
end
def
single_resource
(
resource
)
def
single_resource
(
resource
)
return
error
(
resource
)
if
resource
.
errors
.
any?
||
!
resource
.
persisted?
return
error
(
resource
)
if
resource
.
errors
.
any?
||
!
resource
.
persisted?
message
=
"
#{
title
(
resource
)
}
:"
message
=
"
#{
title
(
resource
)
}
:"
message
<<
"
\n\n
#{
resource
.
description
}
"
if
resource
.
try
(
:description
)
message
<<
"
\n\n
#{
resource
.
description
}
"
if
resource
.
try
(
:description
)
in_channel_response
(
message
)
in_channel_response
(
message
)
end
end
def
multiple_resources
(
resources
)
def
multiple_resources
(
resources
)
resources
.
map!
{
|
resource
|
title
(
resource
)
}
resources
.
map!
{
|
resource
|
title
(
resource
)
}
message
=
header_with_list
(
"Multiple results were found:"
,
resources
)
message
=
header_with_list
(
"Multiple results were found:"
,
resources
)
ephemeral_response
(
message
)
ephemeral_response
(
message
)
end
end
def
error
(
resource
)
def
error
(
resource
)
message
=
header_with_list
(
"The action was not successful, because:"
,
resource
.
errors
.
messages
)
message
=
header_with_list
(
"The action was not successful, because:"
,
resource
.
errors
.
messages
)
ephemeral_response
(
message
)
ephemeral_response
(
message
)
end
end
def
title
(
resource
)
def
title
(
resource
)
reference
=
resource
.
try
(
:to_reference
)
||
resource
.
try
(
:id
)
reference
=
resource
.
try
(
:to_reference
)
||
resource
.
try
(
:id
)
title
=
resource
.
try
(
:title
)
||
resource
.
try
(
:name
)
title
=
resource
.
try
(
:title
)
||
resource
.
try
(
:name
)
"<
#{
url
(
resource
)
}
|
#{
reference
}
#{
title
}
>"
link
(
url
(
resource
),
"
#{
reference
}
#{
title
}
"
)
end
end
def
header_with_list
(
header
,
items
)
def
header_with_list
(
header
,
items
)
message
=
[
header
]
message
=
[
header
]
items
.
each
do
|
item
|
items
.
each
do
|
item
|
message
<<
"-
#{
item
}
"
message
<<
"-
#{
item
}
"
end
message
.
join
(
"
\n
"
)
end
end
def
url
(
resource
)
message
.
join
(
"
\n
"
)
url_for
(
end
[
resource
.
project
.
namespace
.
becomes
(
Namespace
),
resource
.
project
,
resource
]
)
end
def
ephemeral_response
(
message
)
def
url
(
resource
)
{
url_for
(
response_type: :ephemeral
,
[
text:
message
,
resource
.
project
.
namespace
.
becomes
(
Namespace
),
status:
200
resource
.
project
,
}
resource
end
]
)
end
def
ephemeral_response
(
message
)
{
response_type: :ephemeral
,
text:
message
,
status:
200
}
end
def
in_channel_response
(
message
)
{
response_type: :in_channel
,
text:
message
,
status:
200
}
end
def
in_channel_response
(
message
)
def
link
(
url
,
title
)
{
case
format
response_type: :in_channel
,
when
'slack'
then
"<
#{
url
}
|
#{
title
}
>"
text:
message
,
when
'mattermost'
then
"[
#{
title
}
](
#{
url
}
)"
status:
200
else
then
title
}
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