Commit 972168d4 authored by Alex Kalderimis's avatar Alex Kalderimis

Merge branch 'force-flag-generator' into 'master'

Add --force flag to force definition override

See merge request gitlab-org/gitlab!60695
parents 6445df0b 07a8fd55
......@@ -86,6 +86,7 @@ The generator takes three options:
- `--ee`: Indicates if the event is for EE.
- `--category=CATEGORY`: Indicates the `category` of the event.
- `--action=ACTION`: Indicates the `action` of the event.
- `--force`: Overwrites the existing event definition, if one already exists.
```shell
bundle exec rails generate gitlab:snowplow_event_definition --category Groups::EmailCampaignsController --action click
......
......@@ -14,11 +14,12 @@ module Gitlab
class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if event is for ee'
class_option :category, type: :string, optional: false, desc: 'Category of the event'
class_option :action, type: :string, optional: false, desc: 'Action of the event'
class_option :force, type: :boolean, optional: true, default: false, desc: 'Overwrite existing definition'
def create_event_file
raise 'Event definition already exists' if definition_exists?
raise "Event definition already exists at #{file_path}" if definition_exists? && !force_definition_override?
template "event_definition.yml", file_path
template "event_definition.yml", file_path, force: force_definition_override?
end
def distributions
......@@ -41,6 +42,10 @@ module Gitlab
options[:ee]
end
def force_definition_override?
options[:force]
end
private
def definition_exists?
......
......@@ -32,6 +32,30 @@ RSpec.describe Gitlab::SnowplowEventDefinitionGenerator do
expect(::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!).to eq(sample_event)
end
context 'event definition already exists' do
before do
stub_const('Gitlab::VERSION', '12.11.0-pre')
described_class.new([], generator_options).invoke_all
end
it 'overwrites event definition --force flag set to true' do
sample_event = ::Gitlab::Config::Loader::Yaml.new(fixture_file(File.join(sample_event_dir, 'sample_event.yml'))).load_raw!
stub_const('Gitlab::VERSION', '13.11.0-pre')
described_class.new([], generator_options.merge('force' => true)).invoke_all
event_definition_path = File.join(ce_temp_dir, 'groups__email_campaigns_controller_click.yml')
event_data = ::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!
expect(event_data).to eq(sample_event)
end
it 'raises error when --force flag set to false' do
expect { described_class.new([], generator_options.merge('force' => false)).invoke_all }
.to raise_error(StandardError, /Event definition already exists at/)
end
end
it 'creates EE event definition file using the template' do
sample_event = ::Gitlab::Config::Loader::Yaml.new(fixture_file(File.join(sample_event_dir, 'sample_event_ee.yml'))).load_raw!
......
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