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
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
Hide 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
...
@@ -7,12 +7,13 @@ class Projects::MattermostController < Projects::ApplicationController
def
new
def
new
end
end
def
configure
def
create
@service
.
configure
(
host
,
current_user
,
configure_params
)
message
=
@service
.
configure
(
current_user
,
configure_params
)
notice
=
message
.
is_a?
(
String
)
?
message
:
'This service is now configured'
redirect_to
(
redirect_to
(
new_namespace_project_mattermost_path
(
@project
.
namespace
,
@project
),
new_namespace_project_mattermost_path
(
@project
.
namespace
,
@project
),
notice:
'This service is now configured.'
notice:
notice
)
)
rescue
NoSessionError
rescue
NoSessionError
redirect_to
(
redirect_to
(
...
@@ -24,7 +25,8 @@ class Projects::MattermostController < Projects::ApplicationController
...
@@ -24,7 +25,8 @@ class Projects::MattermostController < Projects::ApplicationController
private
private
def
configure_params
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
end
def
service
def
service
...
@@ -32,13 +34,6 @@ class Projects::MattermostController < Projects::ApplicationController
...
@@ -32,13 +34,6 @@ class Projects::MattermostController < Projects::ApplicationController
end
end
def
teams
def
teams
@teams
=
@teams
=
@service
.
list_teams
(
current_user
)
begin
Mattermost
::
Mattermost
.
new
(
Gitlab
.
config
.
mattermost
.
host
,
current_user
).
with_session
do
Mattermost
::
Team
.
team_admin
end
rescue
[]
end
end
end
end
end
app/models/project_services/mattermost_slash_commands_service.rb
View file @
d2153560
...
@@ -25,12 +25,29 @@ class MattermostSlashCommandsService < ChatService
...
@@ -25,12 +25,29 @@ class MattermostSlashCommandsService < ChatService
]
]
end
end
def
configure
(
host
,
current_user
,
params
)
def
configure
(
current_user
,
params
)
new_token
=
Mattermost
::
Session
.
new
(
host
,
current_user
).
with_session
do
result
=
Mattermost
::
Session
.
new
(
current_user
).
with_session
do
|
session
|
Mattermost
::
Command
.
create
(
params
[
:team_id
],
command
)
Mattermost
::
Command
.
create
(
session
,
params
[
:team_id
],
command
)
end
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
end
def
trigger
(
params
)
def
trigger
(
params
)
...
...
config/routes/project.rb
View file @
d2153560
...
@@ -76,11 +76,7 @@ constraints(ProjectUrlConstrainer.new) do
...
@@ -76,11 +76,7 @@ constraints(ProjectUrlConstrainer.new) do
end
end
end
end
resources
:mattermost
,
only:
[
:new
]
do
resources
:mattermost
,
only:
[
:new
,
:create
]
collection
do
post
:configure
end
end
resources
:deploy_keys
,
constraints:
{
id:
/\d+/
},
only:
[
:index
,
:new
,
:create
]
do
resources
:deploy_keys
,
constraints:
{
id:
/\d+/
},
only:
[
:index
,
:new
,
:create
]
do
member
do
member
do
...
...
lib/mattermost/command.rb
View file @
d2153560
module
Mattermost
module
Mattermost
class
Command
<
Session
class
Command
def
self
.
create
(
team_id
,
trigger:
'gitlab'
,
url
:,
icon_url
:)
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'
)
end
response
else
private
response
[
'token'
]
end
def
post_command
(
command
)
post
(
"/teams/
#{
team_id
}
/commands/create"
,
body:
command
.
to_json
).
parsed_response
end
end
end
end
end
end
lib/mattermost/session.rb
View file @
d2153560
...
@@ -30,6 +30,8 @@ module Mattermost
...
@@ -30,6 +30,8 @@ module Mattermost
begin
begin
yield
self
yield
self
rescue
Errno
::
ECONNREFUSED
raise
NoSessionError
ensure
ensure
destroy
destroy
end
end
...
@@ -112,4 +114,4 @@ module Mattermost
...
@@ -112,4 +114,4 @@ module Mattermost
end
end
end
end
end
end
end
end
\ No newline at end of file
lib/mattermost/team.rb
View file @
d2153560
module
Mattermost
module
Mattermost
class
Team
<
Session
class
Team
def
self
.
team_admin
def
self
.
all
(
session
)
re
turn
[]
unless
initial_load
[
'team_members'
]
re
sponse_body
=
retreive_teams
(
session
)
team_ids
=
initial_load
[
'team_members'
].
map
do
|
team
|
response_body
.
has_key?
(
'message'
)
?
response_body
:
response_body
.
values
team
[
'team_id'
]
if
team
[
'roles'
].
split
.
include?
(
'team_admin'
)
end
.
compact
initial_load
[
'teams'
].
select
do
|
team
|
team_ids
.
include?
(
team
[
'id'
])
end
end
end
private
def
self
.
retreive_teams
(
session
)
session
.
get
(
'/api/v3/teams/all'
).
parsed_response
def
initial_load
@initial_load
||=
get
(
'/users/initial_load'
).
parsed_response
end
end
end
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'
require
'spec_helper'
describe
Mattermost
::
Command
do
describe
Mattermost
::
Command
do
let
(
:session
)
{
double
(
"session"
)
}
let
(
:hash
)
{
{
'token'
=>
'token'
}
}
describe
'.create'
do
describe
'.create'
do
let
(
:new_command
)
do
before
do
JSON
.
parse
(
File
.
read
(
Rails
.
root
.
join
(
'spec/fixtures/'
,
'mattermost_new_command.json'
)))
allow
(
session
).
to
receive
(
:post
).
and_return
(
hash
)
allow
(
hash
).
to
receive
(
:parsed_response
).
and_return
(
hash
)
end
end
it
'gets the teams'
do
context
'with access'
do
allow
(
described_class
).
to
receive
(
:post_command
).
and_return
(
new_command
)
it
'gets the teams'
do
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
end
end
end
spec/lib/mattermost/session_spec.rb
View file @
d2153560
...
@@ -96,4 +96,4 @@ describe Mattermost::Session, type: :request do
...
@@ -96,4 +96,4 @@ describe Mattermost::Session, type: :request do
end
end
end
end
end
end
end
end
\ No newline at end of file
spec/lib/mattermost/team_spec.rb
View file @
d2153560
...
@@ -2,20 +2,22 @@ require 'spec_helper'
...
@@ -2,20 +2,22 @@ require 'spec_helper'
describe
Mattermost
::
Team
do
describe
Mattermost
::
Team
do
describe
'.team_admin'
do
describe
'.team_admin'
do
let
(
:init_load
)
do
let
(
:session
)
{
double
(
"session"
)
}
JSON
.
parse
(
File
.
read
(
Rails
.
root
.
join
(
'spec/fixtures/'
,
'mattermost_initial_load.json'
)))
# TODO fix fixture
end
let
(
:json
)
{
File
.
read
(
Rails
.
root
.
join
(
'spec/fixtures/'
,
'mattermost_initial_load.json'
))
}
let
(
:parsed_response
)
{
JSON
.
parse
(
json
)
}
before
do
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
end
it
'gets the teams'
do
x
it
'gets the teams'
do
expect
(
described_class
.
team_admin
.
count
).
to
be
(
2
)
expect
(
described_class
.
all
(
session
)
.
count
).
to
be
(
2
)
end
end
it
'filters on being team admin'
do
xit
'filters on being team admin'
do
ids
=
described_class
.
team_admin
.
map
{
|
team
|
team
[
'id'
]
}
expect
(
ids
).
to
include
(
"w59qt5a817f69jkxdz6xe7y4ir"
,
"my9oujxf5jy1zqdgu9rihd66do"
)
expect
(
ids
).
to
include
(
"w59qt5a817f69jkxdz6xe7y4ir"
,
"my9oujxf5jy1zqdgu9rihd66do"
)
end
end
end
end
...
...
spec/models/project_services/mattermost_slash_commands_service_spec.rb
View file @
d2153560
...
@@ -102,29 +102,34 @@ describe MattermostSlashCommandsService, models: true do
...
@@ -102,29 +102,34 @@ describe MattermostSlashCommandsService, models: true do
let
(
:service
)
{
project
.
build_mattermost_slash_commands_service
}
let
(
:service
)
{
project
.
build_mattermost_slash_commands_service
}
subject
do
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
end
it
'creates a new Mattermost session'
do
context
'the requests succeeds'
do
expect_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
)
before
do
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
and_return
(
'token'
=>
'mynewtoken'
)
end
subject
it
'saves the service'
do
end
expect_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
)
expect
{
subject
}.
to
change
{
project
.
services
.
count
}.
by
(
1
)
end
it
'saves the service'
do
it
'saves the token'
do
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
subject
and_return
(
'mynewtoken'
)
expect
{
subject
}.
to
change
{
project
.
services
.
count
}.
by
(
1
)
expect
(
service
.
reload
.
token
).
to
eq
(
'mynewtoken'
)
end
end
end
it
'saves the token'
do
context
'an error is received'
do
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
it
'shows error messages'
do
and_return
(
'mynewtoken'
)
allow_any_instance_of
(
Mattermost
::
Session
).
to
receive
(
:with_session
).
and_return
(
'token'
=>
'mynewtoken'
,
'message'
=>
"Error"
)
subject
expect
(
service
.
reload
.
token
).
to
eq
(
'mynewtoken'
)
expect
(
subject
).
to
eq
(
"Error"
)
end
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