Commit 87996a74 authored by Nikolay Belokolodov's avatar Nikolay Belokolodov Committed by Michael Kozono

Improve filenames creation for Snowplow event definitions generator

parent 5057bc17
......@@ -65,7 +65,12 @@ module Gitlab
end
def file_name
"#{event_category}_#{event_action}.yml".underscore.gsub("/", "__")
name = remove_special_chars("#{Time.current.to_i}_#{event_category}_#{event_action}")
"#{name[0..95]}.yml" # max 100 chars, see https://gitlab.com/gitlab-com/gl-infra/delivery/-/issues/2030#note_679501200
end
def remove_special_chars(input)
input.gsub("::", "__").gsub(/[^A-Za-z0-9_]/, '')
end
end
end
......@@ -5,6 +5,7 @@ require 'spec_helper'
RSpec.describe Gitlab::SnowplowEventDefinitionGenerator, :silence_stdout do
let(:ce_temp_dir) { Dir.mktmpdir }
let(:ee_temp_dir) { Dir.mktmpdir }
let(:timestamp) { Time.current.to_i }
let(:generator_options) { { 'category' => 'Groups::EmailCampaignsController', 'action' => 'click' } }
before do
......@@ -12,6 +13,10 @@ RSpec.describe Gitlab::SnowplowEventDefinitionGenerator, :silence_stdout do
stub_const("#{described_class}::EE_DIR", ee_temp_dir)
end
around do |example|
freeze_time { example.run }
end
after do
FileUtils.rm_rf([ce_temp_dir, ee_temp_dir])
end
......@@ -22,16 +27,41 @@ RSpec.describe Gitlab::SnowplowEventDefinitionGenerator, :silence_stdout do
end
let(:sample_event_dir) { 'lib/generators/gitlab/snowplow_event_definition_generator' }
let(:file_name) { Dir.children(ce_temp_dir).first }
it 'creates CE event definition file using the template' do
sample_event = ::Gitlab::Config::Loader::Yaml.new(fixture_file(File.join(sample_event_dir, 'sample_event.yml'))).load_raw!
described_class.new([], generator_options).invoke_all
event_definition_path = File.join(ce_temp_dir, 'groups__email_campaigns_controller_click.yml')
event_definition_path = File.join(ce_temp_dir, file_name)
expect(::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!).to eq(sample_event)
end
describe 'generated filename' do
it 'includes timestamp' do
described_class.new([], generator_options).invoke_all
expect(file_name).to include(timestamp.to_s)
end
it 'removes special characters' do
generator_options = { 'category' => '"`ui:[mavenpackages | t5%348()-=@ ]`"', 'action' => 'click' }
described_class.new([], generator_options).invoke_all
expect(file_name).to include('uimavenpackagest')
end
it 'cuts name if longer than 100 characters' do
generator_options = { 'category' => 'a' * 100, 'action' => 'click' }
described_class.new([], generator_options).invoke_all
expect(file_name.length).to eq(100)
end
end
context 'event definition already exists' do
before do
stub_const('Gitlab::VERSION', '12.11.0-pre')
......@@ -44,7 +74,7 @@ RSpec.describe Gitlab::SnowplowEventDefinitionGenerator, :silence_stdout do
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_definition_path = File.join(ce_temp_dir, file_name)
event_data = ::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!
expect(event_data).to eq(sample_event)
......@@ -56,13 +86,17 @@ RSpec.describe Gitlab::SnowplowEventDefinitionGenerator, :silence_stdout do
end
end
describe 'EE' do
let(:file_name) { Dir.children(ee_temp_dir).first }
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!
described_class.new([], generator_options.merge('ee' => true)).invoke_all
event_definition_path = File.join(ee_temp_dir, 'groups__email_campaigns_controller_click.yml')
event_definition_path = File.join(ee_temp_dir, file_name)
expect(::Gitlab::Config::Loader::Yaml.new(File.read(event_definition_path)).load_raw!).to eq(sample_event)
end
end
end
end
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