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
d2153560
Commit
d2153560
authored
Dec 19, 2016
by
Z.J. van de Weg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor adjustments API Mattermost
[ci skip]
parent
4213bd56
Changes
11
Show whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
91 additions
and
76 deletions
+91
-76
app/controllers/projects/mattermost_controller.rb
app/controllers/projects/mattermost_controller.rb
+7
-12
app/models/project_services/mattermost_slash_commands_service.rb
...els/project_services/mattermost_slash_commands_service.rb
+21
-4
config/routes/project.rb
config/routes/project.rb
+1
-5
lib/mattermost/command.rb
lib/mattermost/command.rb
+8
-9
lib/mattermost/session.rb
lib/mattermost/session.rb
+3
-1
lib/mattermost/team.rb
lib/mattermost/team.rb
+6
-14
spec/fixtures/mattermost_new_command.json
spec/fixtures/mattermost_new_command.json
+0
-1
spec/lib/mattermost/command_spec.rb
spec/lib/mattermost/command_spec.rb
+14
-6
spec/lib/mattermost/session_spec.rb
spec/lib/mattermost/session_spec.rb
+1
-1
spec/lib/mattermost/team_spec.rb
spec/lib/mattermost/team_spec.rb
+10
-8
spec/models/project_services/mattermost_slash_commands_service_spec.rb
...roject_services/mattermost_slash_commands_service_spec.rb
+20
-15
No files found.
app/controllers/projects/mattermost_controller.rb
View file @
d2153560
...
...
@@ -7,12 +7,13 @@ class Projects::MattermostController < Projects::ApplicationController
def
new
end
def
configure
@service
.
configure
(
host
,
current_user
,
configure_params
)
def
create
message
=
@service
.
configure
(
current_user
,
configure_params
)
notice
=
message
.
is_a?
(
String
)
?
message
:
'This service is now configured'
redirect_to
(
new_namespace_project_mattermost_path
(
@project
.
namespace
,
@project
),
notice:
'This service is now configured.'
notice:
notice
)
rescue
NoSessionError
redirect_to
(
...
...
@@ -24,7 +25,8 @@ class Projects::MattermostController < Projects::ApplicationController
private
def
configure_params
params
.
permit
(
:trigger
,
:team_id
).
merge
(
url:
service_trigger_url
(
@service
),
icon_url:
asset_url
(
'gitlab_logo.png'
))
params
.
permit
(
:trigger
,
:team_id
).
merge
(
url:
service_trigger_url
(
@service
),
icon_url:
asset_url
(
'gitlab_logo.png'
))
end
def
service
...
...
@@ -32,13 +34,6 @@ class Projects::MattermostController < Projects::ApplicationController
end
def
teams
@teams
=
begin
Mattermost
::
Mattermost
.
new
(
Gitlab
.
config
.
mattermost
.
host
,
current_user
).
with_session
do
Mattermost
::
Team
.
team_admin
end
rescue
[]
end
@teams
=
@service
.
list_teams
(
current_user
)
end
end
app/models/project_services/mattermost_slash_commands_service.rb
View file @
d2153560
...
...
@@ -25,12 +25,29 @@ class MattermostSlashCommandsService < ChatService
]
end
def
configure
(
host
,
current_user
,
params
)
new_token
=
Mattermost
::
Session
.
new
(
host
,
current_user
).
with_session
do
Mattermost
::
Command
.
create
(
params
[
:team_id
],
command
)
def
configure
(
current_user
,
params
)
result
=
Mattermost
::
Session
.
new
(
current_user
).
with_session
do
|
session
|
Mattermost
::
Command
.
create
(
session
,
params
[
:team_id
],
command
)
end
update!
(
token:
new_token
,
active:
true
)
if
result
.
has_key?
(
'message'
)
result
[
'message'
]
else
update!
(
token:
result
[
'token'
],
active:
true
)
end
end
def
list_teams
begin
response
=
Mattermost
::
Mattermost
.
new
(
current_user
).
with_session
do
|
session
|
Mattermost
::
Team
.
teams
(
session
)
end
# We ignore the error message as we can't display it
response
.
has_key?
(
'message'
)
?
[]
:
response
rescue
Mattermost
::
NoSessionError
[]
end
end
def
trigger
(
params
)
...
...
config/routes/project.rb
View file @
d2153560
...
...
@@ -76,11 +76,7 @@ constraints(ProjectUrlConstrainer.new) do
end
end
resources
:mattermost
,
only:
[
:new
]
do
collection
do
post
:configure
end
end
resources
:mattermost
,
only:
[
:new
,
:create
]
resources
:deploy_keys
,
constraints:
{
id:
/\d+/
},
only:
[
:index
,
:new
,
:create
]
do
member
do
...
...
lib/mattermost/command.rb
View file @
d2153560
module
Mattermost
class
Command
<
Session
def
self
.
create
(
team_id
,
trigger:
'gitlab'
,
url
:,
icon_url
:)
class
Command
def
self
.
create
(
session
,
team_id
,
command
)
response
=
session
.
post
(
"/api/v3/teams/
#{
team_id
}
/commands/create"
,
body:
command
.
to_json
).
parsed_response
post_command
(
command
)[
'token'
]
if
response
.
has_key?
(
'message'
)
response
else
response
[
'token'
]
end
private
def
post_command
(
command
)
post
(
"/teams/
#{
team_id
}
/commands/create"
,
body:
command
.
to_json
).
parsed_response
end
end
end
lib/mattermost/session.rb
View file @
d2153560
...
...
@@ -30,6 +30,8 @@ module Mattermost
begin
yield
self
rescue
Errno
::
ECONNREFUSED
raise
NoSessionError
ensure
destroy
end
...
...
lib/mattermost/team.rb
View file @
d2153560
module
Mattermost
class
Team
<
Session
def
self
.
team_admin
re
turn
[]
unless
initial_load
[
'team_members'
]
class
Team
def
self
.
all
(
session
)
re
sponse_body
=
retreive_teams
(
session
)
team_ids
=
initial_load
[
'team_members'
].
map
do
|
team
|
team
[
'team_id'
]
if
team
[
'roles'
].
split
.
include?
(
'team_admin'
)
end
.
compact
initial_load
[
'teams'
].
select
do
|
team
|
team_ids
.
include?
(
team
[
'id'
])
end
response_body
.
has_key?
(
'message'
)
?
response_body
:
response_body
.
values
end
private
def
initial_load
@initial_load
||=
get
(
'/users/initial_load'
).
parsed_response
def
self
.
retreive_teams
(
session
)
session
.
get
(
'/api/v3/teams/all'
).
parsed_response
end
end
end
spec/fixtures/mattermost_new_command.json
deleted
100644 → 0
View file @
4213bd56
{
"id"
:
"y8j1nexrdirj5nubq5uzdwwidr"
,
"token"
:
"pzajm5hfbtni3r49ujpt8betpc"
,
"create_at"
:
1481897117122
,
"update_at"
:
1481897117122
,
"delete_at"
:
0
,
"creator_id"
:
"78nm4euoc7dypergdc13ekxgpo"
,
"team_id"
:
"w59qt5a817f69jkxdz6xe7y4ir"
,
"trigger"
:
"display"
,
"method"
:
"P"
,
"username"
:
"GitLab"
,
"icon_url"
:
""
,
"auto_complete"
:
false
,
"auto_complete_desc"
:
""
,
"auto_complete_hint"
:
""
,
"display_name"
:
"Display name"
,
"description"
:
"the description"
,
"url"
:
"http://trigger.url/trigger"
}
spec/lib/mattermost/command_spec.rb
View file @
d2153560
require
'spec_helper'
describe
Mattermost
::
Command
do
let
(
:session
)
{
double
(
"session"
)
}
let
(
:hash
)
{
{
'token'
=>
'token'
}
}
describe
'.create'
do
let
(
:new_command
)
do
JSON
.
parse
(
File
.
read
(
Rails
.
root
.
join
(
'spec/fixtures/'
,
'mattermost_new_command.json'
)))
before
do
allow
(
session
).
to
receive
(
:post
).
and_return
(
hash
)
allow
(
hash
).
to
receive
(
:parsed_response
).
and_return
(
hash
)
end
context
'with access'
do
it
'gets the teams'
do
allow
(
described_class
).
to
receive
(
:post_command
).
and_return
(
new_command
)
expect
(
session
).
to
receive
(
:post
)
described_class
.
create
(
session
,
'abc'
,
url:
'http://trigger.com'
)
end
end
token
=
described_class
.
create
(
'abc'
,
url:
'http://trigger.url/trigger'
,
icon_url:
'http://myicon.com/icon.png'
)
context
'on an error'
do
expect
(
token
).
to
eq
(
'pzajm5hfbtni3r49ujpt8betpc'
)
end
end
end
spec/lib/mattermost/session_spec.rb
View file @
d2153560
spec/lib/mattermost/team_spec.rb
View file @
d2153560
...
...
@@ -2,20 +2,22 @@ require 'spec_helper'
describe
Mattermost
::
Team
do
describe
'.team_admin'
do
let
(
:init_load
)
do
JSON
.
parse
(
File
.
read
(
Rails
.
root
.
join
(
'spec/fixtures/'
,
'mattermost_initial_load.json'
)))
end
let
(
:session
)
{
double
(
"session"
)
}
# TODO fix fixture
let
(
:json
)
{
File
.
read
(
Rails
.
root
.
join
(
'spec/fixtures/'
,
'mattermost_initial_load.json'
))
}
let
(
:parsed_response
)
{
JSON
.
parse
(
json
)
}
before
do
allow
(
described_class
).
to
receive
(
:initial_load
).
and_return
(
init_load
)
allow
(
session
).
to
receive
(
:get
).
with
(
'/api/v3/teams/all'
).
and_return
(
json
)
allow
(
json
).
to
receive
(
:parsed_response
).
and_return
(
parsed_response
)
end
it
'gets the teams'
do
expect
(
described_class
.
team_admin
.
count
).
to
be
(
2
)
x
it
'gets the teams'
do
expect
(
described_class
.
all
(
session
)
.
count
).
to
be
(
2
)
end
it
'filters on being team admin'
do
ids
=
described_class
.
team_admin
.
map
{
|
team
|
team
[
'id'
]
}
xit
'filters on being team admin'
do
expect
(
ids
).
to
include
(
"w59qt5a817f69jkxdz6xe7y4ir"
,
"my9oujxf5jy1zqdgu9rihd66do"
)
end
end
...
...
spec/models/project_services/mattermost_slash_commands_service_spec.rb
View file @
d2153560
...
...
@@ -102,29 +102,34 @@ describe MattermostSlashCommandsService, models: true do
let
(
:service
)
{
project
.
build_mattermost_slash_commands_service
}
subject
do
service
.
configure
(
'http://localhost:8065'
,
nil
,
team_id:
'abc'
,
trigger:
'gitlab'
,
url:
'http://trigger.url'
,
icon_url:
'http://icon.url/icon.png'
)
service
.
configure
(
'http://localhost:8065'
,
team_id:
'abc'
,
trigger:
'gitlab'
,
url:
'http://trigger.url'
,
icon_url:
'http://icon.url/icon.png'
)
end
it
'creates a new Mattermost session
'
do
expect_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
)
subject
context
'the requests succeeds
'
do
before
do
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
and_return
(
'token'
=>
'mynewtoken'
)
end
it
'saves the service'
do
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
and_return
(
'mynewtoken'
)
expect_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
)
expect
{
subject
}.
to
change
{
project
.
services
.
count
}.
by
(
1
)
end
it
'saves the token'
do
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
and_return
(
'mynewtoken'
)
subject
expect
(
service
.
reload
.
token
).
to
eq
(
'mynewtoken'
)
end
end
context
'an error is received'
do
it
'shows error messages'
do
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
and_return
(
'token'
=>
'mynewtoken'
,
'message'
=>
"Error"
)
expect
(
subject
).
to
eq
(
"Error"
)
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