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
1
Merge Requests
1
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
nexedi
gitlab-ce
Commits
01dd19a3
Commit
01dd19a3
authored
Apr 22, 2020
by
Mikolaj Wawrzyniak
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add starred dashboard create service
parent
a1fde82d
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
152 additions
and
0 deletions
+152
-0
app/services/metrics/users_starred_dashboards/create_service.rb
...rvices/metrics/users_starred_dashboards/create_service.rb
+74
-0
locale/gitlab.pot
locale/gitlab.pot
+6
-0
spec/services/metrics/users_starred_dashboards/create_service_spec.rb
...s/metrics/users_starred_dashboards/create_service_spec.rb
+72
-0
No files found.
app/services/metrics/users_starred_dashboards/create_service.rb
0 → 100644
View file @
01dd19a3
# frozen_string_literal: true
# Create Metrics::UsersStarredDashboard entry for given user based on matched dashboard_path, project
module
Metrics
module
UsersStarredDashboards
class
CreateService
<
::
BaseService
include
Stepable
steps
:authorize_create_action
,
:parse_dashboard_path
,
:create
def
initialize
(
user
,
project
,
dashboard_path
)
@user
,
@project
,
@dashboard_path
=
user
,
project
,
dashboard_path
end
def
execute
keys
=
%i[status message starred_dashboard]
status
,
message
,
dashboards
=
execute_steps
.
values_at
(
*
keys
)
if
status
!=
:success
ServiceResponse
.
error
(
message:
message
)
else
ServiceResponse
.
success
(
payload:
dashboards
)
end
end
private
attr_reader
:user
,
:project
,
:dashboard_path
def
authorize_create_action
(
_options
)
if
Ability
.
allowed?
(
user
,
:create_metrics_user_starred_dashboard
,
project
)
success
(
user:
user
,
project:
project
)
else
error
(
s_
(
'Metrics::UsersStarredDashboards|You are not authorized to add star to this dashboard'
))
end
end
def
parse_dashboard_path
(
options
)
if
dashboard_path_exists?
options
[
:dashboard_path
]
=
dashboard_path
success
(
options
)
else
error
(
s_
(
'Metrics::UsersStarredDashboards|Dashboard with requested path can not be found'
))
end
end
def
create
(
options
)
starred_dashboard
=
build_starred_dashboard_from
(
options
)
if
starred_dashboard
.
save
success
(
starred_dashboard:
starred_dashboard
)
else
error
(
starred_dashboard
.
errors
.
messages
)
end
end
def
build_starred_dashboard_from
(
options
)
Metrics
::
UsersStarredDashboard
.
new
(
user:
options
.
fetch
(
:user
),
project:
options
.
fetch
(
:project
),
dashboard_path:
options
.
fetch
(
:dashboard_path
)
)
end
def
dashboard_path_exists?
Gitlab
::
Metrics
::
Dashboard
::
Finder
.
find_all_paths
(
project
)
.
any?
{
|
dashboard
|
dashboard
[
:path
]
==
dashboard_path
}
end
end
end
end
locale/gitlab.pot
View file @
01dd19a3
...
...
@@ -13256,6 +13256,12 @@ msgstr ""
msgid "Metrics::Dashboard::Annotation|You are not authorized to delete this annotation"
msgstr ""
msgid "Metrics::UsersStarredDashboards|Dashboard with requested path can not be found"
msgstr ""
msgid "Metrics::UsersStarredDashboards|You are not authorized to add star to this dashboard"
msgstr ""
msgid "Metrics|Add metric"
msgstr ""
...
...
spec/services/metrics/users_starred_dashboards/create_service_spec.rb
0 → 100644
View file @
01dd19a3
# frozen_string_literal: true
require
'spec_helper'
describe
Metrics
::
UsersStarredDashboards
::
CreateService
do
let_it_be
(
:user
)
{
create
(
:user
)
}
let
(
:dashboard_path
)
{
'config/prometheus/common_metrics.yml'
}
let
(
:service_instance
)
{
described_class
.
new
(
user
,
project
,
dashboard_path
)
}
let
(
:project
)
{
create
(
:project
)
}
let
(
:starred_dashboard_params
)
do
{
user:
user
,
project:
project
,
dashboard_path:
dashboard_path
}
end
shared_examples
'prevented starred dashboard creation'
do
|
message
|
it
'returns error response'
,
:aggregate_failures
do
expect
(
Metrics
::
UsersStarredDashboard
).
not_to
receive
(
:new
)
response
=
service_instance
.
execute
expect
(
response
.
status
).
to
be
:error
expect
(
response
.
message
).
to
eql
message
end
end
describe
'.execute'
do
context
'with anonymous user'
do
it_behaves_like
'prevented starred dashboard creation'
,
'You are not authorized to add star to this dashboard'
end
context
'with reporter user'
do
before
do
project
.
add_reporter
(
user
)
end
context
'incorrect dashboard_path'
do
let
(
:dashboard_path
)
{
'something_incorrect.yml'
}
it_behaves_like
'prevented starred dashboard creation'
,
'Dashboard with requested path can not be found'
end
context
'with valid dashboard path'
do
it
'creates starred dashboard and returns success response'
,
:aggregate_failures
do
expect_next_instance_of
(
Metrics
::
UsersStarredDashboard
,
starred_dashboard_params
)
do
|
starred_dashboard
|
expect
(
starred_dashboard
).
to
receive
(
:save
).
and_return
true
end
response
=
service_instance
.
execute
expect
(
response
.
status
).
to
be
:success
end
context
'Metrics::UsersStarredDashboard has validation errors'
do
it
'returns error response'
,
:aggregate_failures
do
expect_next_instance_of
(
Metrics
::
UsersStarredDashboard
,
starred_dashboard_params
)
do
|
starred_dashboard
|
expect
(
starred_dashboard
).
to
receive
(
:save
).
and_return
(
false
)
expect
(
starred_dashboard
).
to
receive
(
:errors
).
and_return
(
double
(
messages:
{
base:
[
'Model validation error'
]
}))
end
response
=
service_instance
.
execute
expect
(
response
.
status
).
to
be
:error
expect
(
response
.
message
).
to
eql
(
base:
[
'Model validation error'
])
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