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
Jérome Perrin
gitlab-ce
Commits
1b4fdb98
Commit
1b4fdb98
authored
Nov 16, 2016
by
Z.J. van de Weg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rename from service, and move to lib/gitlab
parent
8c8bc07d
Changes
21
Hide whitespace changes
Inline
Side-by-side
Showing
21 changed files
with
233 additions
and
109 deletions
+233
-109
app/services/mattermost/commands/issue_search_service.rb
app/services/mattermost/commands/issue_search_service.rb
+0
-9
app/services/mattermost/commands/issue_show_service.rb
app/services/mattermost/commands/issue_show_service.rb
+0
-9
app/services/mattermost/commands/merge_request_search_service.rb
...vices/mattermost/commands/merge_request_search_service.rb
+0
-9
app/services/mattermost/commands/merge_request_show_service.rb
...ervices/mattermost/commands/merge_request_show_service.rb
+0
-9
app/services/mattermost/slash_command_service.rb
app/services/mattermost/slash_command_service.rb
+0
-39
lib/gitlab/chat_commands/base_command.rb
lib/gitlab/chat_commands/base_command.rb
+55
-0
lib/gitlab/chat_commands/command.rb
lib/gitlab/chat_commands/command.rb
+49
-0
lib/gitlab/chat_commands/issue_command.rb
lib/gitlab/chat_commands/issue_command.rb
+3
-3
lib/gitlab/chat_commands/issue_create.rb
lib/gitlab/chat_commands/issue_create.rb
+16
-0
lib/gitlab/chat_commands/issue_search.rb
lib/gitlab/chat_commands/issue_search.rb
+17
-0
lib/gitlab/chat_commands/issue_show.rb
lib/gitlab/chat_commands/issue_show.rb
+17
-0
lib/gitlab/chat_commands/merge_request_command.rb
lib/gitlab/chat_commands/merge_request_command.rb
+3
-3
lib/gitlab/chat_commands/merge_request_search.rb
lib/gitlab/chat_commands/merge_request_search.rb
+17
-0
lib/gitlab/chat_commands/merge_request_show.rb
lib/gitlab/chat_commands/merge_request_show.rb
+17
-0
lib/mattermost/presenter.rb
lib/mattermost/presenter.rb
+3
-4
spec/lib/gitlab/chat_commands/command_spec.rb
spec/lib/gitlab/chat_commands/command_spec.rb
+3
-3
spec/lib/gitlab/chat_commands/issue_create_spec.rb
spec/lib/gitlab/chat_commands/issue_create_spec.rb
+4
-4
spec/lib/gitlab/chat_commands/issue_search_spec.rb
spec/lib/gitlab/chat_commands/issue_search_spec.rb
+4
-4
spec/lib/gitlab/chat_commands/issue_show_spec.rb
spec/lib/gitlab/chat_commands/issue_show_spec.rb
+4
-5
spec/lib/gitlab/chat_commands/merge_request_search_spec.rb
spec/lib/gitlab/chat_commands/merge_request_search_spec.rb
+10
-4
spec/lib/gitlab/chat_commands/merge_request_show_spec.rb
spec/lib/gitlab/chat_commands/merge_request_show_spec.rb
+11
-4
No files found.
app/services/mattermost/commands/issue_search_service.rb
deleted
100644 → 0
View file @
8c8bc07d
module
Mattermost
module
Commands
class
IssueSearchService
<
IssueService
def
execute
present
search_results
end
end
end
end
app/services/mattermost/commands/issue_show_service.rb
deleted
100644 → 0
View file @
8c8bc07d
module
Mattermost
module
Commands
class
IssueShowService
<
IssueService
def
execute
present
find_by_iid
end
end
end
end
app/services/mattermost/commands/merge_request_search_service.rb
deleted
100644 → 0
View file @
8c8bc07d
module
Mattermost
module
Commands
class
MergeRequestSearchService
<
MergeRequestService
def
execute
present
search_results
end
end
end
end
app/services/mattermost/commands/merge_request_show_service.rb
deleted
100644 → 0
View file @
8c8bc07d
module
Mattermost
module
Commands
class
MergeRequestShowService
<
MergeRequestService
def
execute
present
find_by_iid
end
end
end
end
app/services/mattermost/slash_command_service.rb
deleted
100644 → 0
View file @
8c8bc07d
module
Mattermost
class
SlashCommandService
<
BaseService
def
self
.
registry
@registry
||=
Hash
.
new
({})
end
def
self
.
command
(
command
,
sub_command
,
klass
,
help_message
)
registry
[
command
][
sub_command
]
=
{
klass:
klass
,
help_message:
help_message
}
end
command
'issue'
,
'show'
,
Mattermost
::
Commands
::
IssueShowService
,
'issue show <id>'
command
'issue'
,
'search'
,
Mattermost
::
Commands
::
IssueSearchService
,
'issue search <query>'
command
'issue'
,
'create'
,
Mattermost
::
Commands
::
IssueCreateService
,
'issue create my title'
command
'mergerequest'
,
'show'
,
Mattermost
::
Commands
::
MergeRequestShowService
,
'mergerequest show <id>'
command
'mergerequest'
,
'search'
,
Mattermost
::
Commands
::
MergeRequestSearchService
,
'mergerequest search <query>'
def
execute
command
,
subcommand
=
parse_command
#TODO think how to do this to support ruby 2.1
service
=
registry
.
dig
(
command
,
subcommand
,
:klass
)
return
help_messages
(
registry
)
unless
service
.
try
(
:available?
,
project
)
service
.
new
(
project
,
current_user
,
params
).
execute
end
private
def
parse_command
params
[
:text
].
split
.
first
(
2
)
end
def
registry
self
.
class
.
registry
end
end
end
app/services/mattermost/commands/base_service
.rb
→
lib/gitlab/chat_commands/base_command
.rb
View file @
1b4fdb98
module
Mattermost
module
Commands
class
Base
Service
<
::
BaseService
module
Gitlab
module
C
hatC
ommands
class
Base
Command
QUERY_LIMIT
=
5
def
execute
def
self
.
match
(
_
)
raise
NotImplementedError
end
def
available?
def
self
.
help_message
raise
NotImplementedError
end
def
self
.
available?
(
_
)
raise
NotImplementedError
end
def
execute
(
_
)
raise
NotImplementedError
end
...
...
@@ -15,33 +23,33 @@ module Mattermost
raise
NotImplementedError
end
attr_accessor
:project
,
:current_user
,
:params
def
initialize
(
project
,
user
,
params
=
{})
@project
,
@current_user
,
@params
=
project
,
user
,
params
.
dup
end
private
def
can?
(
object
,
action
,
subject
)
Ability
.
allowed?
(
object
,
action
,
subject
)
end
def
present
(
resource
)
Mattermost
::
Presenter
.
present
(
resource
)
end
def
find_by_iid
def
find_by_iid
(
iid
)
resource
=
collection
.
find_by
(
iid:
iid
)
readable?
(
resource
)
?
resource
:
nil
end
def
search_results
def
search_results
(
query
)
collection
.
search
(
query
).
limit
(
QUERY_LIMIT
).
select
do
|
resource
|
readable?
(
resource
)
end
end
# params[:text] = issue search <search query>
def
query
params
[
:text
].
split
[
2
..-
1
].
join
(
' '
)
end
# params[:text] = 'mergerequest show 123'
def
iid
params
[
:text
].
split
[
2
]
end
end
end
end
lib/gitlab/chat_commands/command.rb
0 → 100644
View file @
1b4fdb98
module
Gitlab
module
ChatCommands
class
Command
<
BaseCommand
COMMANDS
=
[
Gitlab
::
ChatCommands
::
IssueShow
,
Gitlab
::
ChatCommands
::
IssueSearch
,
Gitlab
::
ChatCommands
::
IssueCreate
,
Gitlab
::
ChatCommands
::
MergeRequestShow
,
Gitlab
::
ChatCommands
::
MergeRequestSearch
,
].
freeze
def
execute
klass
,
match
=
fetch_klass
return
help
(
help_messages
)
unless
klass
.
try
(
:available?
,
project
)
klass
.
new
(
project
,
current_user
,
params
).
execute
(
match
)
end
private
def
fetch_klass
match
=
nil
service
=
COMMANDS
.
find
do
|
klass
|
if
klass
.
available?
(
project
)
false
else
match
=
klass
.
match
(
command
)
end
end
[
service
,
match
]
end
def
help_messages
COMMANDS
.
map
do
|
klass
|
next
unless
klass
.
available?
(
project
)
klass
.
help_message
end
.
compact
end
def
command
params
[
:text
]
end
end
end
end
app/services/mattermost/commands/issue_service
.rb
→
lib/gitlab/chat_commands/issue_command
.rb
View file @
1b4fdb98
module
Mattermost
module
Commands
class
Issue
Service
<
Mattermost
::
Commands
::
BaseService
module
Gitlab
module
C
hatC
ommands
class
Issue
Command
<
BaseCommand
def
self
.
available?
(
project
)
project
.
issues_enabled?
&&
project
.
default_issues_tracker?
end
...
...
app/services/mattermost/commands/issue_create_servic
e.rb
→
lib/gitlab/chat_commands/issue_creat
e.rb
View file @
1b4fdb98
module
Mattermost
module
Commands
class
IssueCreateService
<
IssueService
def
execute
title
,
description
=
parse_command
present
Issues
::
CreateService
.
new
(
project
,
current_user
,
title:
title
,
description:
description
).
execute
module
Gitlab
module
ChatCommands
class
IssueCreate
<
BaseCommand
def
self
.
match
(
text
)
/\Aissue\s+create\s+(?<title>[^\n]*)\n*(?<description>.*)\z/
.
match
(
text
)
end
private
def
parse_command
match
=
params
[
:text
].
match
(
/\Aissue create (?<title>.*)\n*/
)
def
execute
(
match
)
title
=
match
[
:title
]
description
=
match
.
post_match
description
=
match
[
:description
]
[
title
,
description
]
present
Issues
::
CreateService
.
new
(
project
,
current_user
,
title:
title
,
description:
description
).
execute
end
end
end
...
...
lib/gitlab/chat_commands/issue_search.rb
0 → 100644
View file @
1b4fdb98
module
Gitlab
module
ChatCommands
class
IssueSearch
<
IssueCommand
def
self
.
match
(
text
)
/\Aissue\s+search\s+(?<query>.*)/
.
match
(
text
)
end
def
self
.
help_message
"issue search <query>"
end
def
execute
(
match
)
present
search_results
(
match
[
:query
])
end
end
end
end
lib/gitlab/chat_commands/issue_show.rb
0 → 100644
View file @
1b4fdb98
module
Gitlab
module
ChatCommands
class
IssueShow
<
IssueCommand
def
self
.
match
(
text
)
/\Aissue\s+show\s+(?<iid>\d+)/
.
match
(
text
)
end
def
self
.
help_message
"issue show <id>"
end
def
execute
(
match
)
present
find_by_iid
(
match
[
:iid
])
end
end
end
end
app/services/mattermost/commands/merge_request_service
.rb
→
lib/gitlab/chat_commands/merge_request_command
.rb
View file @
1b4fdb98
module
Mattermost
module
Commands
class
MergeRequest
Service
<
Mattermost
::
Commands
::
BaseService
module
Gitlab
module
C
hatC
ommands
class
MergeRequest
Command
<
BaseCommand
def
self
.
available?
(
project
)
project
.
merge_requests_enabled?
end
...
...
lib/gitlab/chat_commands/merge_request_search.rb
0 → 100644
View file @
1b4fdb98
module
Gitlab
module
ChatCommands
class
MergeRequestSearch
<
MergeRequestCommand
def
self
.
match
(
text
)
/\Amergerequest\s+search\s+(?<query>.*)/
.
match
(
text
)
end
def
self
.
help_message
"mergerequest search <query>"
end
def
execute
(
match
)
present
search_results
(
match
[
:query
])
end
end
end
end
lib/gitlab/chat_commands/merge_request_show.rb
0 → 100644
View file @
1b4fdb98
module
Gitlab
module
ChatCommands
class
MergeRequestShow
<
MergeRequestCommand
def
self
.
match
(
text
)
/\Amergerequest\s+show\s+(?<iid>\d+)/
.
match
(
text
)
end
def
self
.
help_message
"mergerequest show <id>"
end
def
execute
(
match
)
present
find_by_iid
(
match
[
:iid
])
end
end
end
end
lib/mattermost/presenter.rb
View file @
1b4fdb98
...
...
@@ -12,12 +12,11 @@ module Mattermost
}
end
# TODO figure out how I know which are available or not
def
help_message
(
commands
)
def
help
(
messages
)
messages
=
[
"Available commands:"
]
commands
.
each
do
|
sub_command
,
attrs
|
messages
<<
"
\t
#{
COMMAND_PREFIX
}
#{
attrs
[
:help_message
]
}
"
messages
.
each
do
|
messsage
|
messages
<<
"
-
#{
message
}
"
end
{
...
...
spec/
services/mattermost/slash_command_service
_spec.rb
→
spec/
lib/gitlab/chat_commands/command
_spec.rb
View file @
1b4fdb98
require
'spec_helper'
describe
Mattermost
::
SlashCommandService
,
service:
true
do
let
(
:project
)
{
build
(
:project
)
}
let
(
:user
)
{
build
(
:user
)
}
describe
Gitlab
::
ChatCommands
::
Command
,
service:
true
do
let
(
:project
)
{
create
(
:project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:params
)
{
{
text:
'issue show 1'
}
}
subject
{
described_class
.
new
(
project
,
user
,
params
).
execute
}
...
...
spec/
services/mattermost/commands/issue_create_servic
e_spec.rb
→
spec/
lib/gitlab/chat_commands/issue_creat
e_spec.rb
View file @
1b4fdb98
require
'spec_helper'
describe
Mattermost
::
Commands
::
IssueCreateServic
e
,
service:
true
do
describe
Gitlab
::
ChatCommands
::
IssueCreat
e
,
service:
true
do
describe
'#execute'
do
let
(
:project
)
{
create
(
:empty_project
)
}
let
(
:user
)
{
create
(
:user
)
}
let
(
:
params
)
{
{
text:
"issue create bird is the word"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"issue create bird is the word"
)
}
before
{
project
.
team
<<
[
user
,
:master
]
}
subject
{
described_class
.
new
(
project
,
user
,
params
).
execute
}
subject
{
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
}
context
'without description'
do
it
'creates the issue'
do
...
...
@@ -23,7 +23,7 @@ describe Mattermost::Commands::IssueCreateService, service: true do
context
'with description'
do
let
(
:description
)
{
"Surfin bird"
}
let
(
:
params
)
{
{
text:
"issue create The bird is the word
\n
#{
description
}
"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"issue create bird is the word
\n
#{
description
}
"
)
}
before
{
subject
}
...
...
spec/
services/mattermost/commands/issue_search_service
_spec.rb
→
spec/
lib/gitlab/chat_commands/issue_search
_spec.rb
View file @
1b4fdb98
require
'spec_helper'
describe
Mattermost
::
Commands
::
IssueSearchService
,
service:
true
do
describe
Gitlab
::
ChatCommands
::
IssueSearch
,
service:
true
do
describe
'#execute'
do
let!
(
:issue
)
{
create
(
:issue
,
title:
'The bird is the word'
)
}
let
(
:project
)
{
issue
.
project
}
let
(
:user
)
{
issue
.
author
}
let
(
:
params
)
{
{
text:
"issue search bird is the"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"issue search bird is the"
)
}
before
{
project
.
team
<<
[
user
,
:master
]
}
subject
{
described_class
.
new
(
project
,
user
,
params
).
execute
}
subject
{
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
}
context
'without results'
do
let
(
:
params
)
{
{
text:
"issue search no results for this one"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"issue search no results for this one"
)
}
it
"returns nil"
do
expect
(
subject
[
:response_type
]).
to
be
:ephemeral
...
...
spec/
services/mattermost/commands/issue_show_service
_spec.rb
→
spec/
lib/gitlab/chat_commands/issue_show
_spec.rb
View file @
1b4fdb98
require
'spec_helper'
describe
Mattermost
::
Commands
::
IssueShowService
,
service:
true
do
describe
Gitlab
::
ChatCommands
::
IssueShow
,
service:
true
do
describe
'#execute'
do
let
(
:issue
)
{
create
(
:issue
)
}
let
(
:project
)
{
issue
.
project
}
let
(
:user
)
{
issue
.
author
}
let
(
:
params
)
{
{
text:
"issue show
#{
issue
.
iid
}
"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"issue show
#{
issue
.
iid
}
"
)
}
before
{
project
.
team
<<
[
user
,
:master
]
}
subject
{
described_class
.
new
(
project
,
user
,
params
).
execute
}
subject
{
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
}
context
'the issue exists'
do
it
'returns the issue'
do
expect
(
subject
[
:response_type
]).
to
be
:in_channel
expect
(
subject
[
:text
]).
to
match
issue
.
title
end
end
context
'the issue does not exist'
do
let
(
:
params
)
{
{
text:
"issue show 12345"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"issue show 1234"
)
}
it
"returns nil"
do
expect
(
subject
[
:response_type
]).
to
be
:ephemeral
...
...
spec/
services/mattermost/commands/merge_request_search_service
_spec.rb
→
spec/
lib/gitlab/chat_commands/merge_request_search
_spec.rb
View file @
1b4fdb98
require
'spec_helper'
describe
Mattermost
::
Commands
::
MergeRequestSearchService
,
service:
true
do
describe
Gitlab
::
ChatCommands
::
MergeRequestSearch
,
service:
true
do
describe
'#execute'
do
let!
(
:merge_request
)
{
create
(
:merge_request
,
title:
'The bird is the word'
)
}
let
(
:project
)
{
merge_request
.
source_project
}
let
(
:user
)
{
merge_request
.
author
}
let
(
:
params
)
{
{
text:
"mergerequest search
#{
merge_request
.
title
}
"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"mergerequest search
#{
merge_request
.
title
}
"
)
}
before
{
project
.
team
<<
[
user
,
:master
]
}
subject
{
described_class
.
new
(
project
,
user
,
params
).
execute
}
subject
{
described_class
.
new
(
project
,
user
,
{}).
execute
(
regex_match
)
}
context
'the merge request exists'
do
it
'returns the merge request'
do
...
...
@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do
end
context
'no results can be found'
do
let
(
:
params
)
{
{
text:
"mergerequest search 12345"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"mergerequest search 12334"
)
}
it
"returns a 404 message"
do
expect
(
subject
[
:response_type
]).
to
be
:ephemeral
...
...
@@ -27,4 +27,10 @@ describe Mattermost::Commands::MergeRequestSearchService, service: true do
end
end
end
describe
'self.match'
do
it
'matches a valid query'
do
expect
(
described_class
.
match
(
"mergerequest search my title here"
)).
to
be_truthy
end
end
end
spec/
services/mattermost/commands/merge_request_show_service
_spec.rb
→
spec/
lib/gitlab/chat_commands/merge_request_show
_spec.rb
View file @
1b4fdb98
require
'spec_helper'
describe
Mattermost
::
Commands
::
MergeRequestShowService
,
service:
true
do
describe
Gitlab
::
ChatCommands
::
MergeRequestShow
,
service:
true
do
describe
'#execute'
do
let!
(
:merge_request
)
{
create
(
:merge_request
)
}
let
(
:project
)
{
merge_request
.
source_project
}
let
(
:user
)
{
merge_request
.
author
}
let
(
:
params
)
{
{
text:
"mergerequest show
#{
merge_request
.
iid
}
"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"mergerequest show
#{
merge_request
.
iid
}
"
)
}
before
{
project
.
team
<<
[
user
,
:master
]
}
subject
{
described_class
.
new
(
project
,
user
,
params
).
execute
}
subject
{
described_class
.
new
(
project
,
user
).
execute
(
regex_match
)
}
context
'the merge request exists'
do
it
'returns the merge request'
do
...
...
@@ -19,7 +19,7 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do
end
context
'the merge request does not exist'
do
let
(
:
params
)
{
{
text:
"mergerequest show 12345"
}
}
let
(
:
regex_match
)
{
described_class
.
match
(
"mergerequest show 12345"
)
}
it
"returns nil"
do
expect
(
subject
[
:response_type
]).
to
be
:ephemeral
...
...
@@ -27,4 +27,11 @@ describe Mattermost::Commands::MergeRequestShowService, service: true do
end
end
end
describe
"self.match"
do
it
'matches valid strings'
do
expect
(
described_class
.
match
(
"mergerequest show 123"
)).
to
be_truthy
expect
(
described_class
.
match
(
"mergerequest show sdf23"
)).
to
be_falsy
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