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
166ee096
Commit
166ee096
authored
Nov 17, 2016
by
Z.J. van de Weg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More refactoring, push present to base command
parent
1607efa4
Changes
12
Hide whitespace changes
Inline
Side-by-side
Showing
12 changed files
with
85 additions
and
54 deletions
+85
-54
app/models/project.rb
app/models/project.rb
+1
-1
app/models/project_services/mattermost_command_service.rb
app/models/project_services/mattermost_command_service.rb
+4
-4
lib/api/services.rb
lib/api/services.rb
+2
-2
lib/gitlab/chat_commands/base_command.rb
lib/gitlab/chat_commands/base_command.rb
+8
-10
lib/gitlab/chat_commands/command.rb
lib/gitlab/chat_commands/command.rb
+18
-12
lib/gitlab/chat_commands/issue_command.rb
lib/gitlab/chat_commands/issue_command.rb
+1
-1
lib/gitlab/chat_commands/issue_create.rb
lib/gitlab/chat_commands/issue_create.rb
+4
-2
lib/mattermost/presenter.rb
lib/mattermost/presenter.rb
+34
-19
spec/lib/gitlab/chat_commands/command_spec.rb
spec/lib/gitlab/chat_commands/command_spec.rb
+9
-0
spec/lib/gitlab/chat_name_token_spec.rb
spec/lib/gitlab/chat_name_token_spec.rb
+2
-2
spec/models/project_spec.rb
spec/models/project_spec.rb
+1
-0
spec/services/chat_names/authorize_user_service_spec.rb
spec/services/chat_names/authorize_user_service_spec.rb
+1
-1
No files found.
app/models/project.rb
View file @
166ee096
...
@@ -91,7 +91,7 @@ class Project < ActiveRecord::Base
...
@@ -91,7 +91,7 @@ class Project < ActiveRecord::Base
has_one
:assembla_service
,
dependent: :destroy
has_one
:assembla_service
,
dependent: :destroy
has_one
:asana_service
,
dependent: :destroy
has_one
:asana_service
,
dependent: :destroy
has_one
:gemnasium_service
,
dependent: :destroy
has_one
:gemnasium_service
,
dependent: :destroy
has_one
:mattermost_c
hat
_service
,
dependent: :destroy
has_one
:mattermost_c
ommand
_service
,
dependent: :destroy
has_one
:slack_service
,
dependent: :destroy
has_one
:slack_service
,
dependent: :destroy
has_one
:buildkite_service
,
dependent: :destroy
has_one
:buildkite_service
,
dependent: :destroy
has_one
:bamboo_service
,
dependent: :destroy
has_one
:bamboo_service
,
dependent: :destroy
...
...
app/models/project_services/mattermost_command_service.rb
View file @
166ee096
...
@@ -38,19 +38,19 @@ class MattermostCommandService < ChatService
...
@@ -38,19 +38,19 @@ class MattermostCommandService < ChatService
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
Mattermost
::
Presenter
.
authorize_
user
(
url
)
return
Mattermost
::
Presenter
.
authorize_
chat_name
(
url
)
end
end
Mattermost
::
CommandService
.
new
(
project
,
user
,
params
).
execute
Gitlab
::
ChatCommands
::
Command
.
new
(
project
,
user
,
params
).
execute
end
end
private
private
def
find_chat_user
(
params
)
def
find_chat_user
(
params
)
ChatNames
::
FindUserService
.
new
(
chat_names
,
params
).
execute
ChatNames
::
FindUserService
.
new
(
self
,
params
).
execute
end
end
def
authorize_chat_name_url
(
params
)
def
authorize_chat_name_url
(
params
)
ChatNames
::
Request
Service
.
new
(
self
,
params
).
execute
ChatNames
::
AuthorizeUser
Service
.
new
(
self
,
params
).
execute
end
end
end
end
lib/api/services.rb
View file @
166ee096
...
@@ -61,7 +61,6 @@ module API
...
@@ -61,7 +61,6 @@ module API
end
end
resource
:projects
do
resource
:projects
do
desc
'Trigger a slash command'
do
desc
'Trigger a slash command'
do
detail
'Added in GitLab 8.13'
detail
'Added in GitLab 8.13'
end
end
...
@@ -78,7 +77,8 @@ module API
...
@@ -78,7 +77,8 @@ module API
result
=
service
.
try
(
:active?
)
&&
service
.
try
(
:trigger
,
params
)
result
=
service
.
try
(
:active?
)
&&
service
.
try
(
:trigger
,
params
)
if
result
if
result
present
result
,
status:
result
[
:status
]
||
200
status
result
[
:status
]
||
200
present
result
else
else
not_found!
(
'Service'
)
not_found!
(
'Service'
)
end
end
...
...
lib/gitlab/chat_commands/base_command.rb
View file @
166ee096
...
@@ -15,6 +15,14 @@ module Gitlab
...
@@ -15,6 +15,14 @@ module Gitlab
raise
NotImplementedError
raise
NotImplementedError
end
end
def
self
.
allowed?
(
_
,
_
)
true
end
def
self
.
can?
(
object
,
action
,
subject
)
Ability
.
allowed?
(
object
,
action
,
subject
)
end
def
execute
(
_
)
def
execute
(
_
)
raise
NotImplementedError
raise
NotImplementedError
end
end
...
@@ -31,21 +39,11 @@ module Gitlab
...
@@ -31,21 +39,11 @@ module Gitlab
private
private
def
can?
(
object
,
action
,
subject
)
Ability
.
allowed?
(
object
,
action
,
subject
)
end
def
find_by_iid
(
iid
)
def
find_by_iid
(
iid
)
resource
=
collection
.
find_by
(
iid:
iid
)
resource
=
collection
.
find_by
(
iid:
iid
)
readable?
(
resource
)
?
resource
:
nil
readable?
(
resource
)
?
resource
:
nil
end
end
def
search_results
(
query
)
collection
.
search
(
query
).
limit
(
QUERY_LIMIT
).
select
do
|
resource
|
readable?
(
resource
)
end
end
end
end
end
end
end
end
lib/gitlab/chat_commands/command.rb
View file @
166ee096
...
@@ -7,10 +7,14 @@ module Gitlab
...
@@ -7,10 +7,14 @@ module Gitlab
].
freeze
].
freeze
def
execute
def
execute
klass
,
match
=
fetch_klass
command
,
match
=
match_command
if
klass
if
command
present
klass
.
new
(
project
,
current_user
,
params
).
execute
(
match
)
if
command
.
allowed?
(
project
,
current_user
)
present
command
.
new
(
project
,
current_user
,
params
).
execute
(
match
)
else
access_denied
end
else
else
help
(
help_messages
)
help
(
help_messages
)
end
end
...
@@ -18,7 +22,7 @@ module Gitlab
...
@@ -18,7 +22,7 @@ module Gitlab
private
private
def
fetch_klass
def
match_command
match
=
nil
match
=
nil
service
=
available_commands
.
find
do
|
klass
|
service
=
available_commands
.
find
do
|
klass
|
match
=
klass
.
match
(
command
)
match
=
klass
.
match
(
command
)
...
@@ -28,9 +32,7 @@ module Gitlab
...
@@ -28,9 +32,7 @@ module Gitlab
end
end
def
help_messages
def
help_messages
available_commands
.
map
do
|
klass
|
available_commands
.
map
(
&
:help_message
)
klass
.
help_message
end
end
end
def
available_commands
def
available_commands
...
@@ -43,13 +45,17 @@ module Gitlab
...
@@ -43,13 +45,17 @@ module Gitlab
params
[
:text
]
params
[
:text
]
end
end
def
present
(
resource
)
Mattermost
::
Presenter
.
present
(
resource
)
end
def
help
(
messages
)
def
help
(
messages
)
Mattermost
::
Presenter
.
help
(
messages
,
params
[
:command
])
Mattermost
::
Presenter
.
help
(
messages
,
params
[
:command
])
end
end
def
access_denied
Mattermost
::
Presenter
.
access_denied
end
def
present
(
resource
)
Mattermost
::
Presenter
.
present
(
resource
)
end
end
end
end
end
end
end
lib/gitlab/chat_commands/issue_command.rb
View file @
166ee096
...
@@ -10,7 +10,7 @@ module Gitlab
...
@@ -10,7 +10,7 @@ module Gitlab
end
end
def
readable?
(
issue
)
def
readable?
(
issue
)
can?
(
current_user
,
:read_issue
,
issue
)
self
.
class
.
can?
(
current_user
,
:read_issue
,
issue
)
end
end
end
end
end
end
...
...
lib/gitlab/chat_commands/issue_create.rb
View file @
166ee096
...
@@ -9,9 +9,11 @@ module Gitlab
...
@@ -9,9 +9,11 @@ module Gitlab
'issue create <title>\n<description>'
'issue create <title>\n<description>'
end
end
def
execute
(
match
)
def
self
.
allowed?
(
project
,
user
)
return
nil
unless
can?
(
current_user
,
:create_issue
,
project
)
can?
(
user
,
:create_issue
,
project
)
end
def
execute
(
match
)
title
=
match
[
:title
]
title
=
match
[
:title
]
description
=
match
[
:description
]
description
=
match
[
:description
]
...
...
lib/mattermost/presenter.rb
View file @
166ee096
...
@@ -4,24 +4,19 @@ module Mattermost
...
@@ -4,24 +4,19 @@ module Mattermost
include
Rails
.
application
.
routes
.
url_helpers
include
Rails
.
application
.
routes
.
url_helpers
def
authorize_chat_name
(
url
)
def
authorize_chat_name
(
url
)
message
=
"
Hi there! We've yet to get acquainted! Please introduce yourself by [connection your GitLab profile](
#{
url
}
)!
"
message
=
"
:wave: Hi there! Before I do anything for you, please [connect your GitLab account](
#{
url
}
).
"
ephemeral_response
(
message
)
ephemeral_response
(
message
)
end
end
def
help
(
messages
,
command
)
def
help
(
commands
,
trigger
)
return
ephemeral_response
(
"No commands configured"
)
unless
messages
.
count
>
1
if
commands
.
count
==
0
message
=
[
"Available commands:"
]
ephemeral_response
(
"No commands configured"
)
unless
messages
.
count
>
1
else
message
=
header_with_list
(
"Available commands"
,
commands
)
messages
.
each
do
|
messsage
|
ephemeral_response
(
message
)
message
<<
"-
#{
command
}
#{
message
}
"
end
end
ephemeral_response
(
message
.
join
(
"
\n
"
))
end
def
not_found
ephemeral_response
(
"404 not found! GitLab couldn't find what your were looking for! :boom:"
)
end
end
def
present
(
resource
)
def
present
(
resource
)
...
@@ -40,8 +35,16 @@ module Mattermost
...
@@ -40,8 +35,16 @@ module Mattermost
single_resource
(
resource
)
single_resource
(
resource
)
end
end
def
access_denied
ephemeral_response
(
"Whoops! That action is not allowed. This incident will be [reported](https://xkcd.com/838/)."
)
end
private
private
def
not_found
ephemeral_response
(
"404 not found! GitLab couldn't find what your were looking for! :boom:"
)
end
def
single_resource
(
resource
)
def
single_resource
(
resource
)
return
error
(
resource
)
if
resource
.
errors
.
any?
return
error
(
resource
)
if
resource
.
errors
.
any?
...
@@ -52,23 +55,33 @@ module Mattermost
...
@@ -52,23 +55,33 @@ module Mattermost
end
end
def
multiple_resources
(
resources
)
def
multiple_resources
(
resources
)
message
=
"Multiple results were found:
\n
"
resources
.
map!
{
|
resource
|
title
(
resource
)
}
message
<<
resources
.
map
{
|
resource
|
"-
#{
title
(
resource
)
}
"
}.
join
(
"
\n
"
)
message
=
header_with_list
(
"Multiple results were found:"
,
resources
)
ephemeral_response
(
message
)
ephemeral_response
(
message
)
end
end
def
error
(
resource
)
def
error
(
resource
)
message
=
"The action was not succesfull because:
\n
"
message
=
header_with_list
(
"The action was not succesful, because:"
,
resource
.
errors
.
messages
)
message
<<
resource
.
errors
.
messages
.
map
{
|
message
|
"-
#{
message
}
"
}.
join
(
"
\n
"
)
ephemeral_response
(
resource
.
errors
.
messages
.
join
(
"
\n
"
)
)
ephemeral_response
(
message
)
end
end
def
title
(
resource
)
def
title
(
resource
)
"[
#{
resource
.
to_reference
}
#{
resource
.
title
}
](
#{
url
(
resource
)
}
)"
"[
#{
resource
.
to_reference
}
#{
resource
.
title
}
](
#{
url
(
resource
)
}
)"
end
end
def
header_with_list
(
header
,
items
)
message
=
[
header
]
items
.
each
do
|
item
|
message
<<
"-
#{
item
}
"
end
message
.
join
(
"
\n
"
)
end
def
url
(
resource
)
def
url
(
resource
)
url_for
(
url_for
(
[
[
...
@@ -82,14 +95,16 @@ module Mattermost
...
@@ -82,14 +95,16 @@ module Mattermost
def
ephemeral_response
(
message
)
def
ephemeral_response
(
message
)
{
{
response_type: :ephemeral
,
response_type: :ephemeral
,
text:
message
text:
message
,
status:
200
}
}
end
end
def
in_channel_response
(
message
)
def
in_channel_response
(
message
)
{
{
response_type: :in_channel
,
response_type: :in_channel
,
text:
message
text:
message
,
status:
200
}
}
end
end
end
end
...
...
spec/lib/gitlab/chat_commands/command_spec.rb
View file @
166ee096
...
@@ -26,6 +26,15 @@ describe Gitlab::ChatCommands::Command, service: true do
...
@@ -26,6 +26,15 @@ describe Gitlab::ChatCommands::Command, service: true do
end
end
end
end
context
'the user can not create an issue'
do
let
(
:params
)
{
{
text:
"issue create my new issue"
}
}
it
'rejects the actions'
do
expect
(
subject
[
:response_type
]).
to
be
(
:ephemeral
)
expect
(
subject
[
:text
]).
to
start_with
(
'Whoops! That action is not allowed'
)
end
end
context
'issue is succesfully created'
do
context
'issue is succesfully created'
do
let
(
:params
)
{
{
text:
"issue create my new issue"
}
}
let
(
:params
)
{
{
text:
"issue create my new issue"
}
}
...
...
spec/lib/gitlab/chat_name_token_spec.rb
View file @
166ee096
...
@@ -12,9 +12,9 @@ describe Gitlab::ChatNameToken, lib: true do
...
@@ -12,9 +12,9 @@ describe Gitlab::ChatNameToken, lib: true do
end
end
context
'when storing data'
do
context
'when storing data'
do
let
(
:data
)
{
let
(
:data
)
do
{
key:
'value'
}
{
key:
'value'
}
}
end
subject
{
described_class
.
new
(
@token
)
}
subject
{
described_class
.
new
(
@token
)
}
...
...
spec/models/project_spec.rb
View file @
166ee096
...
@@ -35,6 +35,7 @@ describe Project, models: true do
...
@@ -35,6 +35,7 @@ describe Project, models: true do
it
{
is_expected
.
to
have_one
(
:hipchat_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:hipchat_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:flowdock_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:flowdock_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:assembla_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:assembla_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:mattermost_command_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:gemnasium_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:gemnasium_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:buildkite_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:buildkite_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:bamboo_service
).
dependent
(
:destroy
)
}
it
{
is_expected
.
to
have_one
(
:bamboo_service
).
dependent
(
:destroy
)
}
...
...
spec/services/chat_names/authorize_user_service_spec.rb
View file @
166ee096
...
@@ -17,7 +17,7 @@ describe ChatNames::AuthorizeUserService, services: true do
...
@@ -17,7 +17,7 @@ describe ChatNames::AuthorizeUserService, services: true do
end
end
context
'when there are missing parameters'
do
context
'when there are missing parameters'
do
let
(
:params
)
{
{
}
}
let
(
:params
)
{
{}
}
it
'does not request a new token'
do
it
'does not request a new token'
do
is_expected
.
to
be_nil
is_expected
.
to
be_nil
...
...
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