Commit 9117c2eb authored by allison.browne's avatar allison.browne

Change default of create_issue for alerts

Add a migration to change the incident settings create_issue
column default. Add incident settings to existing projects by
adding a data migration which adds project incident settings
settings to projects with issues that have the incident label.
parent 246f7469
---
title: Turn off alert issue creation by default
merge_request: 34107
author:
type: added
# frozen_string_literal: true
class ChangeColumnDefaultProjectIncidentManagementSettings < ActiveRecord::Migration[6.0]
DOWNTIME = false
def up
change_column_default(:project_incident_management_settings, :create_issue, from: true, to: false)
end
def down
change_column_default(:project_incident_management_settings, :create_issue, from: false, to: true)
end
end
# frozen_string_literal: true
class AddIncidentSettingsToAllExistingProjects < ActiveRecord::Migration[6.0]
DOWNTIME = false
def up
# Add records to projects project_incident_management_settings
# to preserve behavior for existing projects that
# are using the create issue functionality with the default setting of true
query = <<-SQL
WITH project_ids AS (
SELECT DISTINCT issues.project_id AS id
FROM issues
LEFT OUTER JOIN project_incident_management_settings
ON project_incident_management_settings.project_id = issues.project_id
INNER JOIN label_links
ON label_links.target_type = 'Issue'
AND label_links.target_id = issues.id
INNER JOIN labels
ON labels.id = label_links.label_id
WHERE ( project_incident_management_settings.project_id IS NULL )
-- Use incident labels even though they could be manually added by users who
-- are not using alert funtionality.
AND labels.title = 'incident'
AND labels.color = '#CC0033'
AND labels.description = 'Denotes a disruption to IT services and the associated issues require immediate attention'
)
INSERT INTO project_incident_management_settings (project_id, create_issue, send_email, issue_template_key)
SELECT project_ids.id, TRUE, FALSE, NULL
FROM project_ids
ON CONFLICT (project_id) DO NOTHING;
SQL
execute(query)
end
def down
# no-op
end
end
...@@ -5223,7 +5223,7 @@ ALTER SEQUENCE public.project_import_data_id_seq OWNED BY public.project_import_ ...@@ -5223,7 +5223,7 @@ ALTER SEQUENCE public.project_import_data_id_seq OWNED BY public.project_import_
CREATE TABLE public.project_incident_management_settings ( CREATE TABLE public.project_incident_management_settings (
project_id integer NOT NULL, project_id integer NOT NULL,
create_issue boolean DEFAULT true NOT NULL, create_issue boolean DEFAULT false NOT NULL,
send_email boolean DEFAULT false NOT NULL, send_email boolean DEFAULT false NOT NULL,
issue_template_key text issue_template_key text
); );
...@@ -13888,6 +13888,8 @@ COPY "schema_migrations" (version) FROM STDIN; ...@@ -13888,6 +13888,8 @@ COPY "schema_migrations" (version) FROM STDIN;
20200604174558 20200604174558
20200608072931 20200608072931
20200608075553 20200608075553
20200608214008
20200609002841 20200609002841
20200609212701
\. \.
...@@ -998,7 +998,7 @@ In GitLab versions 13.1 and greater, you can configure your manually configured ...@@ -998,7 +998,7 @@ In GitLab versions 13.1 and greater, you can configure your manually configured
>- [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/4925) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 11.11. >- [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/4925) in [GitLab Ultimate](https://about.gitlab.com/pricing/) 11.11.
>- [From GitLab Ultimate 12.5](https://gitlab.com/gitlab-org/gitlab/-/issues/13401), when GitLab receives a recovery alert, it will automatically close the associated issue. >- [From GitLab Ultimate 12.5](https://gitlab.com/gitlab-org/gitlab/-/issues/13401), when GitLab receives a recovery alert, it will automatically close the associated issue.
Alerts can be used to trigger actions, like opening an issue automatically (enabled by default since `12.1`). To configure the actions: Alerts can be used to trigger actions, like opening an issue automatically (disabled by default since `13.1`). To configure the actions:
1. Navigate to your project's **Settings > Operations > Incidents**. 1. Navigate to your project's **Settings > Operations > Incidents**.
1. Enable the option to create issues. 1. Enable the option to create issues.
......
...@@ -32,8 +32,8 @@ describe ProjectsHelper do ...@@ -32,8 +32,8 @@ describe ProjectsHelper do
setting = helper.project_incident_management_setting setting = helper.project_incident_management_setting
expect(setting).not_to be_persisted expect(setting).not_to be_persisted
expect(setting.create_issue).to be_falsey
expect(setting.send_email).to be_falsey expect(setting.send_email).to be_falsey
expect(setting.create_issue).to be_truthy
expect(setting.issue_template_key).to be_nil expect(setting.issue_template_key).to be_nil
end end
end end
......
# frozen_string_literal: true
require 'spec_helper'
require Rails.root.join('db', 'migrate', '20200609212701_add_incident_settings_to_all_existing_projects.rb')
describe AddIncidentSettingsToAllExistingProjects, :migration do
let(:project_incident_management_settings) { table(:project_incident_management_settings) }
let(:labels) { table(:labels) }
let(:label_links) { table(:label_links) }
let(:issues) { table(:issues) }
let(:projects) { table(:projects) }
let(:namespaces) { table(:namespaces) }
RSpec.shared_examples 'setting not added' do
it 'does not add settings' do
migrate!
expect { migrate! }.not_to change { IncidentManagement::ProjectIncidentManagementSetting.count }
end
end
RSpec.shared_examples 'project has no incident settings' do
it 'has no settings' do
migrate!
expect(settings).to eq(nil)
end
end
RSpec.shared_examples 'no change to incident settings' do
it 'does not change existing settings' do
migrate!
expect(settings.create_issue).to eq(existing_create_issue)
end
end
RSpec.shared_context 'with incident settings' do
let(:existing_create_issue) { false }
before do
project_incident_management_settings.create(
project_id: project.id,
create_issue: existing_create_issue
)
end
end
describe 'migrate!' do
let(:namespace) { namespaces.create!(name: 'foo', path: 'foo') }
let!(:project) { projects.create!(namespace_id: namespace.id) }
let(:settings) { project_incident_management_settings.find_by(project_id: project.id) }
context 'when project does not have incident label' do
context 'does not have incident settings' do
include_examples 'setting not added'
include_examples 'project has no incident settings'
end
context 'and has incident settings' do
include_context 'with incident settings'
include_examples 'setting not added'
include_examples 'no change to incident settings'
end
end
context 'when project has incident labels' do
before do
issue = issues.create!(project_id: project.id)
incident_label_attrs = IncidentManagement::CreateIssueService::INCIDENT_LABEL
incident_label = labels.create!(project_id: project.id, **incident_label_attrs)
label_links.create!(target_id: issue.id, label_id: incident_label.id, target_type: 'Issue')
end
context 'when project has incident settings' do
include_context 'with incident settings'
include_examples 'setting not added'
include_examples 'no change to incident settings'
end
context 'does not have incident settings' do
it 'adds incident settings with old defaults' do
migrate!
expect(settings.create_issue).to eq(true)
expect(settings.send_email).to eq(false)
expect(settings.issue_template_key).to eq(nil)
end
end
end
end
end
...@@ -4,6 +4,7 @@ require 'spec_helper' ...@@ -4,6 +4,7 @@ require 'spec_helper'
describe IncidentManagement::ProcessAlertWorker do describe IncidentManagement::ProcessAlertWorker do
let_it_be(:project) { create(:project) } let_it_be(:project) { create(:project) }
let_it_be(:settings) { create(:project_incident_management_setting, project: project, create_issue: true) }
describe '#perform' do describe '#perform' do
let(:alert_management_alert_id) { nil } let(:alert_management_alert_id) { nil }
......
...@@ -8,6 +8,7 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do ...@@ -8,6 +8,7 @@ describe IncidentManagement::ProcessPrometheusAlertWorker do
let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) } let_it_be(:prometheus_alert) { create(:prometheus_alert, project: project) }
let(:payload_key) { Gitlab::Alerting::Alert.new(project: project, payload: alert_params).gitlab_fingerprint } let(:payload_key) { Gitlab::Alerting::Alert.new(project: project, payload: alert_params).gitlab_fingerprint }
let!(:prometheus_alert_event) { create(:prometheus_alert_event, prometheus_alert: prometheus_alert, payload_key: payload_key) } let!(:prometheus_alert_event) { create(:prometheus_alert_event, prometheus_alert: prometheus_alert, payload_key: payload_key) }
let!(:settings) { create(:project_incident_management_setting, project: project, create_issue: true) }
let(:alert_params) do let(:alert_params) do
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment