1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
|
#!/usr/bin/env ruby
# frozen_string_literal: true
# Generate an Elastic migration file, spec and dictionary record with the current timestamp.
require 'yaml'
require 'fileutils'
require 'uri'
require 'readline'
require 'active_support'
require 'active_support/core_ext/string'
class ElasticMigrationCreator
attr_reader :options
Options = Struct.new(
:name,
:description,
:group,
:introduced_by_url,
:milestone,
:obsolete,
:marked_obsolete_by_url,
:marked_obsolete_in_milestone
)
def initialize
@options = Options.new
end
def execute
options.name = read_name
options.description = read_description
options.group = read_group
options.introduced_by_url = read_introduced_by_url
options.milestone = read_milestone
$stdout.puts "\e[32mcreated\e[0m #{file_path}"
$stdout.puts "\e[32mcreated\e[0m #{spec_file_path}"
$stdout.puts "\e[32mcreated\e[0m #{dictionary_file_path}"
write
$stdout.puts "\n=> Please consult the documentation for Advanced Search Migrations: #{documentation_reference}"
end
private
def read_name
read_variable('name', 'Name of the migration in CamelCase').camelize
end
def read_description
read_variable('description', 'Description of what the migration does')
end
def read_group
read_variable('group', 'The group introducing a feature flag, like: `global search`')
end
def read_milestone
milestone = File.read('VERSION')
milestone.gsub(/^(\d+\.\d+).*$/, '\1').chomp
end
def read_variable(name, description)
$stdout.puts "\n>> #{description}:"
loop do
variable = Readline.readline('?> ', false)&.strip
return variable unless variable.empty?
warn "Error: #{name} is required."
end
end
def read_introduced_by_url
$stdout.puts
$stdout.puts ">> URL of the MR introducing the migration (enter to skip):"
loop do
introduced_by_url = Readline.readline('?> ', false)&.strip
introduced_by_url = nil if introduced_by_url.empty?
return introduced_by_url if introduced_by_url.nil? || introduced_by_url.start_with?('https://')
warn 'Error: URL needs to start with https://'
end
end
def write
# create migration file
FileUtils.mkdir_p(File.dirname(file_path))
File.write(file_path, file_contents)
# create spec
FileUtils.mkdir_p(File.dirname(spec_file_path))
File.write(spec_file_path, spec_contents)
# create dictionary file
FileUtils.mkdir_p(File.dirname(dictionary_file_path))
File.write(dictionary_file_path, dictionary_contents)
end
def timestamp
@timestamp ||= Time.now.strftime('%Y%m%d%H%M%S')
end
def file_name
@file_name ||= "#{timestamp}_#{options.name.dup.underscore}"
end
def file_path
"ee/elastic/migrate/#{file_name}.rb"
end
def spec_file_path
"ee/spec/elastic/migrate/#{file_name}_spec.rb"
end
def dictionary_file_path
"ee/elastic/docs/#{file_name}.yml"
end
def file_contents
"# frozen_string_literal: true
class #{options.name} < Elastic::Migration
end
"
end
def spec_contents
"# frozen_string_literal: true
require 'spec_helper'
require File.expand_path('#{file_path}')
# See https://docs.gitlab.com/ee/development/testing_guide/best_practices.html#elasticsearch-specs
# for more information on how to write search migration specs for GitLab.
RSpec.describe #{options.name}, feature_category: :#{options.group.parameterize.underscore} do
let(:version) { #{timestamp} }
end
"
end
def dictionary_contents
dictionary_config_hash.to_yaml
end
def dictionary_config_hash
{
'name' => options.name,
'version' => timestamp,
'description' => options.description,
'group' => "group::#{options.group}",
'milestone' => options.milestone.to_s,
'introduced_by_url' => options.introduced_by_url,
'obsolete' => false,
'marked_obsolete_by_url' => nil,
'marked_obsolete_in_milestone' => nil
}
end
def documentation_reference
'https://docs.gitlab.com/ee/development/search/advanced_search_migration_styleguide.html'
end
end
ElasticMigrationCreator.new.execute if $PROGRAM_NAME == __FILE__
|