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
ab38d7af
Commit
ab38d7af
authored
Mar 24, 2020
by
Vitali Tatarintev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Move Operations#reset_alerting_token to CE
Contributes to migration of Prometheus alerts to Core
parent
6b2e3070
Changes
10
Show whitespace changes
Inline
Side-by-side
Showing
10 changed files
with
212 additions
and
229 deletions
+212
-229
app/controllers/projects/settings/operations_controller.rb
app/controllers/projects/settings/operations_controller.rb
+19
-0
app/services/projects/operations/update_service.rb
app/services/projects/operations/update_service.rb
+18
-0
config/routes/project.rb
config/routes/project.rb
+6
-1
ee/app/controllers/ee/projects/settings/operations_controller.rb
...controllers/ee/projects/settings/operations_controller.rb
+0
-21
ee/app/services/ee/projects/operations/update_service.rb
ee/app/services/ee/projects/operations/update_service.rb
+0
-18
ee/config/routes/project.rb
ee/config/routes/project.rb
+0
-6
ee/spec/controllers/projects/settings/operations_controller_spec.rb
...ntrollers/projects/settings/operations_controller_spec.rb
+0
-101
ee/spec/services/projects/operations/update_service_spec.rb
ee/spec/services/projects/operations/update_service_spec.rb
+0
-82
spec/controllers/projects/settings/operations_controller_spec.rb
...ntrollers/projects/settings/operations_controller_spec.rb
+88
-0
spec/services/projects/operations/update_service_spec.rb
spec/services/projects/operations/update_service_spec.rb
+81
-0
No files found.
app/controllers/projects/settings/operations_controller.rb
View file @
ab38d7af
...
...
@@ -4,6 +4,9 @@ module Projects
module
Settings
class
OperationsController
<
Projects
::
ApplicationController
before_action
:authorize_admin_operations!
before_action
:authorize_read_prometheus_alerts!
,
only:
[
:reset_alerting_token
]
respond_to
:json
,
only:
[
:reset_alerting_token
]
helper_method
:error_tracking_setting
...
...
@@ -27,8 +30,24 @@ module Projects
end
end
def
reset_alerting_token
result
=
::
Projects
::
Operations
::
UpdateService
.
new
(
project
,
current_user
,
alerting_params
)
.
execute
if
result
[
:status
]
==
:success
render
json:
{
token:
project
.
alerting_setting
.
token
}
else
render
json:
{},
status: :unprocessable_entity
end
end
private
def
alerting_params
{
alerting_setting_attributes:
{
regenerate_token:
true
}
}
end
def
prometheus_service
project
.
find_or_initialize_service
(
::
PrometheusService
.
to_param
)
end
...
...
app/services/projects/operations/update_service.rb
View file @
ab38d7af
...
...
@@ -13,12 +13,30 @@ module Projects
def
project_update_params
error_tracking_params
.
merge
(
alerting_setting_params
)
.
merge
(
metrics_setting_params
)
.
merge
(
grafana_integration_params
)
.
merge
(
prometheus_integration_params
)
.
merge
(
incident_management_setting_params
)
end
def
alerting_setting_params
return
{}
unless
can?
(
current_user
,
:read_prometheus_alerts
,
project
)
attr
=
params
[
:alerting_setting_attributes
]
return
{}
unless
attr
regenerate_token
=
attr
.
delete
(
:regenerate_token
)
if
regenerate_token
attr
[
:token
]
=
nil
else
attr
=
attr
.
except
(
:token
)
end
{
alerting_setting_attributes:
attr
}
end
def
metrics_setting_params
attribs
=
params
[
:metrics_setting_attributes
]
return
{}
unless
attribs
...
...
config/routes/project.rb
View file @
ab38d7af
...
...
@@ -75,7 +75,12 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
put
:reset_registration_token
end
resource
:operations
,
only:
[
:show
,
:update
]
resource
:operations
,
only:
[
:show
,
:update
]
do
member
do
post
:reset_alerting_token
end
end
resource
:integrations
,
only:
[
:show
]
resource
:repository
,
only:
[
:show
],
controller: :repository
do
...
...
ee/app/controllers/ee/projects/settings/operations_controller.rb
View file @
ab38d7af
...
...
@@ -8,31 +8,10 @@ module EE
extend
ActiveSupport
::
Concern
prepended
do
before_action
:authorize_read_prometheus_alerts!
,
only:
[
:reset_alerting_token
]
respond_to
:json
,
only:
[
:reset_alerting_token
]
def
reset_alerting_token
result
=
::
Projects
::
Operations
::
UpdateService
.
new
(
project
,
current_user
,
alerting_params
)
.
execute
if
result
[
:status
]
==
:success
render
json:
{
token:
project
.
alerting_setting
.
token
}
else
render
json:
{},
status: :unprocessable_entity
end
end
helper_method
:tracing_setting
private
def
alerting_params
{
alerting_setting_attributes:
{
regenerate_token:
true
}
}
end
def
tracing_setting
@tracing_setting
||=
project
.
tracing_setting
||
project
.
build_tracing_setting
end
...
...
ee/app/services/ee/projects/operations/update_service.rb
View file @
ab38d7af
...
...
@@ -11,7 +11,6 @@ module EE
def
project_update_params
super
.
merge
(
tracing_setting_params
)
.
merge
(
alerting_setting_params
)
.
merge
(
incident_management_setting_params
)
.
merge
(
status_page_setting_params
)
end
...
...
@@ -27,23 +26,6 @@ module EE
{
tracing_setting_attributes:
attr
.
merge
(
_destroy:
destroy
)
}
end
def
alerting_setting_params
return
{}
unless
can?
(
current_user
,
:read_prometheus_alerts
,
project
)
attr
=
params
[
:alerting_setting_attributes
]
return
{}
unless
attr
regenerate_token
=
attr
.
delete
(
:regenerate_token
)
if
regenerate_token
attr
[
:token
]
=
nil
else
attr
=
attr
.
except
(
:token
)
end
{
alerting_setting_attributes:
attr
}
end
def
incident_management_setting_params
params
.
slice
(
:incident_management_setting_attributes
)
end
...
...
ee/config/routes/project.rb
View file @
ab38d7af
...
...
@@ -38,12 +38,6 @@ constraints(::Constraints::ProjectUrlConstrainer.new) do
end
namespace
:settings
do
resource
:operations
,
only:
[]
do
member
do
post
:reset_alerting_token
end
end
resource
:slack
,
only:
[
:destroy
,
:edit
,
:update
]
do
get
:slack_auth
end
...
...
ee/spec/controllers/projects/settings/operations_controller_spec.rb
View file @
ab38d7af
...
...
@@ -365,107 +365,6 @@ describe Projects::Settings::OperationsController do
end
end
describe
'POST reset_alerting_token'
do
let
(
:project
)
{
create
(
:project
)
}
before
do
stub_licensed_features
(
prometheus_alerts:
true
)
project
.
add_maintainer
(
user
)
end
context
'with existing alerting setting'
do
let!
(
:alerting_setting
)
do
create
(
:project_alerting_setting
,
project:
project
)
end
let!
(
:old_token
)
{
alerting_setting
.
token
}
it
'returns newly reset token'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'token'
]).
to
eq
(
alerting_setting
.
reload
.
token
)
expect
(
old_token
).
not_to
eq
(
alerting_setting
.
token
)
end
end
context
'without existing alerting setting'
do
it
'creates a token'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
project
.
alerting_setting
).
not_to
be_nil
expect
(
json_response
[
'token'
]).
to
eq
(
project
.
alerting_setting
.
token
)
end
end
context
'when update fails'
do
let
(
:operations_update_service
)
{
spy
(
:operations_update_service
)
}
let
(
:alerting_params
)
do
{
alerting_setting_attributes:
{
regenerate_token:
true
}
}
end
before
do
expect
(
::
Projects
::
Operations
::
UpdateService
)
.
to
receive
(
:new
).
with
(
project
,
user
,
alerting_params
)
.
and_return
(
operations_update_service
)
expect
(
operations_update_service
).
to
receive
(
:execute
)
.
and_return
(
status: :error
)
end
it
'returns unprocessable_entity'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:unprocessable_entity
)
expect
(
json_response
).
to
be_empty
end
end
context
'with insufficient permissions'
do
before
do
project
.
add_reporter
(
user
)
end
it
'returns 404'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
end
end
context
'as an anonymous user'
do
before
do
sign_out
(
user
)
end
it
'returns a redirect'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:redirect
)
end
end
context
'without a license'
do
before
do
stub_licensed_features
(
prometheus_alerts:
false
)
end
it
'returns 404'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
end
end
private
def
reset_alerting_token
post
:reset_alerting_token
,
params:
project_params
(
project
),
format: :json
end
end
private
def
project_params
(
project
,
tracing_params:
nil
,
incident_management_params:
nil
,
status_page_params:
nil
)
...
...
ee/spec/services/projects/operations/update_service_spec.rb
View file @
ab38d7af
...
...
@@ -99,88 +99,6 @@ describe Projects::Operations::UpdateService do
end
end
context
'alerting setting'
do
before
do
stub_licensed_features
(
prometheus_alerts:
true
)
project
.
add_maintainer
(
user
)
end
shared_examples
'no operation'
do
it
'does nothing'
do
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
).
to
be_nil
end
end
context
'with valid params'
do
let
(
:params
)
{
{
alerting_setting_attributes:
alerting_params
}
}
shared_examples
'setting creation'
do
it
'creates a setting'
do
expect
(
project
.
alerting_setting
).
to
be_nil
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
).
not_to
be_nil
end
end
context
'when regenerate_token is not set'
do
let
(
:alerting_params
)
{
{
token:
'some token'
}
}
context
'with an existing setting'
do
let!
(
:alerting_setting
)
do
create
(
:project_alerting_setting
,
project:
project
)
end
it
'ignores provided token'
do
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
.
token
)
.
to
eq
(
alerting_setting
.
token
)
end
end
context
'without an existing setting'
do
it_behaves_like
'setting creation'
end
end
context
'when regenerate_token is set'
do
let
(
:alerting_params
)
{
{
regenerate_token:
true
}
}
context
'with an existing setting'
do
let
(
:token
)
{
'some token'
}
let!
(
:alerting_setting
)
do
create
(
:project_alerting_setting
,
project:
project
,
token:
token
)
end
it
'regenerates token'
do
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
.
token
).
not_to
eq
(
token
)
end
end
context
'without an existing setting'
do
it_behaves_like
'setting creation'
context
'with insufficient permissions'
do
before
do
project
.
add_reporter
(
user
)
end
it_behaves_like
'no operation'
end
end
end
end
context
'with empty params'
do
let
(
:params
)
{
{}
}
it_behaves_like
'no operation'
end
end
context
'status page setting'
do
before
do
project
.
add_maintainer
(
user
)
...
...
spec/controllers/projects/settings/operations_controller_spec.rb
View file @
ab38d7af
...
...
@@ -295,6 +295,94 @@ describe Projects::Settings::OperationsController do
end
end
end
describe
'POST reset_alerting_token'
do
let
(
:project
)
{
create
(
:project
)
}
before
do
project
.
add_maintainer
(
user
)
end
context
'with existing alerting setting'
do
let!
(
:alerting_setting
)
do
create
(
:project_alerting_setting
,
project:
project
)
end
let!
(
:old_token
)
{
alerting_setting
.
token
}
it
'returns newly reset token'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
json_response
[
'token'
]).
to
eq
(
alerting_setting
.
reload
.
token
)
expect
(
old_token
).
not_to
eq
(
alerting_setting
.
token
)
end
end
context
'without existing alerting setting'
do
it
'creates a token'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:ok
)
expect
(
project
.
alerting_setting
).
not_to
be_nil
expect
(
json_response
[
'token'
]).
to
eq
(
project
.
alerting_setting
.
token
)
end
end
context
'when update fails'
do
let
(
:operations_update_service
)
{
spy
(
:operations_update_service
)
}
let
(
:alerting_params
)
do
{
alerting_setting_attributes:
{
regenerate_token:
true
}
}
end
before
do
expect
(
::
Projects
::
Operations
::
UpdateService
)
.
to
receive
(
:new
).
with
(
project
,
user
,
alerting_params
)
.
and_return
(
operations_update_service
)
expect
(
operations_update_service
).
to
receive
(
:execute
)
.
and_return
(
status: :error
)
end
it
'returns unprocessable_entity'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:unprocessable_entity
)
expect
(
json_response
).
to
be_empty
end
end
context
'with insufficient permissions'
do
before
do
project
.
add_reporter
(
user
)
end
it
'returns 404'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:not_found
)
end
end
context
'as an anonymous user'
do
before
do
sign_out
(
user
)
end
it
'returns a redirect'
do
reset_alerting_token
expect
(
response
).
to
have_gitlab_http_status
(
:redirect
)
end
end
private
def
reset_alerting_token
post
:reset_alerting_token
,
params:
project_params
(
project
),
format: :json
end
end
end
private
...
...
spec/services/projects/operations/update_service_spec.rb
View file @
ab38d7af
...
...
@@ -11,6 +11,87 @@ describe Projects::Operations::UpdateService do
subject
{
described_class
.
new
(
project
,
user
,
params
)
}
describe
'#execute'
do
context
'alerting setting'
do
before
do
project
.
add_maintainer
(
user
)
end
shared_examples
'no operation'
do
it
'does nothing'
do
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
).
to
be_nil
end
end
context
'with valid params'
do
let
(
:params
)
{
{
alerting_setting_attributes:
alerting_params
}
}
shared_examples
'setting creation'
do
it
'creates a setting'
do
expect
(
project
.
alerting_setting
).
to
be_nil
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
).
not_to
be_nil
end
end
context
'when regenerate_token is not set'
do
let
(
:alerting_params
)
{
{
token:
'some token'
}
}
context
'with an existing setting'
do
let!
(
:alerting_setting
)
do
create
(
:project_alerting_setting
,
project:
project
)
end
it
'ignores provided token'
do
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
.
token
)
.
to
eq
(
alerting_setting
.
token
)
end
end
context
'without an existing setting'
do
it_behaves_like
'setting creation'
end
end
context
'when regenerate_token is set'
do
let
(
:alerting_params
)
{
{
regenerate_token:
true
}
}
context
'with an existing setting'
do
let
(
:token
)
{
'some token'
}
let!
(
:alerting_setting
)
do
create
(
:project_alerting_setting
,
project:
project
,
token:
token
)
end
it
'regenerates token'
do
expect
(
result
[
:status
]).
to
eq
(
:success
)
expect
(
project
.
reload
.
alerting_setting
.
token
).
not_to
eq
(
token
)
end
end
context
'without an existing setting'
do
it_behaves_like
'setting creation'
context
'with insufficient permissions'
do
before
do
project
.
add_reporter
(
user
)
end
it_behaves_like
'no operation'
end
end
end
end
context
'with empty params'
do
let
(
:params
)
{
{}
}
it_behaves_like
'no operation'
end
end
context
'metrics dashboard setting'
do
let
(
:params
)
do
{
...
...
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