diff --git a/notifier/Gemfile b/notifier/Gemfile index 8be5c7ef1a005ce5bd2c922fd397fde8f51c92ec..64daf8d960afa3c7cbfdb32327d7ccf880671d89 100644 --- a/notifier/Gemfile +++ b/notifier/Gemfile @@ -8,6 +8,7 @@ gem 'niceql' gem 'pg_query' gem 'pry' gem 'thor' +gem 'activesupport' group :test do gem 'rspec' diff --git a/notifier/Gemfile.lock b/notifier/Gemfile.lock index fad342df7f58950447f61746b09c9a4878eb99a8..6200b32be3777d26fdf8e73b021a1b75d93105b8 100644 --- a/notifier/Gemfile.lock +++ b/notifier/Gemfile.lock @@ -95,6 +95,7 @@ PLATFORMS ruby DEPENDENCIES + activesupport filesize gitlab gitlab-styles diff --git a/notifier/feedback.rb b/notifier/feedback.rb index d3dd8a7219c94090f341cf171456ccd5321d3519..99e95b30e390c1aaad00548bd2291c142dfc332a 100644 --- a/notifier/feedback.rb +++ b/notifier/feedback.rb @@ -1,6 +1,7 @@ # frozen_string_literal: true require 'filesize' require 'erb' +require 'active_support/core_ext/module/delegation' require_relative 'niceql' class Feedback @@ -8,6 +9,8 @@ class Feedback attr_reader :result + delegate :migrations_from_branch, :other_migrations, to: :result + def initialize(result) @result = result end @@ -16,29 +19,8 @@ class Feedback erb('feedback').result(binding) end - def migrations_from_branch - all_migrations.select(&:intro_on_current_branch?) - end - - def other_migrations - all_migrations.reject(&:intro_on_current_branch) - end - private - def all_migrations - migrations = result.migrations.values - - regular_migrations = migrations - .select { |m| m.type == Migration::TYPE_REGULAR } - .sort_by(&:version) - post_migrations = migrations - .select { |m| m.type == Migration::TYPE_POST_DEPLOY } - .sort_by(&:version) - - regular_migrations.concat(post_migrations) - end - def render_details(migration) erb('detail').result(binding) end diff --git a/notifier/migration.rb b/notifier/migration.rb index 47c7b10d58d6ade5840b6c7b3478453b07b999d9..ad167b6b007691b4315dbfeca51c8ac079d06bdf 100644 --- a/notifier/migration.rb +++ b/notifier/migration.rb @@ -95,4 +95,8 @@ class Migration Query.new(query) end end + + def sort_key + [type == TYPE_REGULAR ? 0 : 1, version] + end end diff --git a/notifier/result.rb b/notifier/result.rb index 76a0624a571f1e5f9fbd5a7b91a6f7f1f061ecf5..3cd8e64ebc3d514c65dba0173b46e54a57a11a12 100644 --- a/notifier/result.rb +++ b/notifier/result.rb @@ -35,4 +35,18 @@ class Result @migrations = migrations @clone_details = clone_details end + + def migrations_from_branch + sorted_migrations.select(&:intro_on_current_branch?) + end + + def other_migrations + sorted_migrations.reject(&:intro_on_current_branch) + end + + private + + def sorted_migrations + migrations.values.sort_by(&:sort_key) + end end diff --git a/notifier/spec/feedback_spec.rb b/notifier/spec/feedback_spec.rb deleted file mode 100644 index b7d233d39941ee70055f94f04ebee76e17855bfc..0000000000000000000000000000000000000000 --- a/notifier/spec/feedback_spec.rb +++ /dev/null @@ -1,36 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -RSpec.describe Feedback do - subject(:feedback) { described_class.new(Result.new(migrations, nil)) } - - let(:migrations) do - # rubocop:disable Metrics/LineLength - { - 4 => Migration.new({ 'version' => 4, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => true }, nil), - 3 => Migration.new({ 'version' => 3, 'type' => Migration::TYPE_REGULAR, 'intro_on_current_branch' => true }, nil), - 1 => Migration.new({ 'version' => 1, 'type' => Migration::TYPE_REGULAR, 'intro_on_current_branch' => true }, nil), - 2 => Migration.new({ 'version' => 2, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => true }, nil), - 5 => Migration.new({ 'version' => 5, 'type' => Migration::TYPE_REGULAR, 'intro_on_current_branch' => false }, nil), - 6 => Migration.new({ 'version' => 6, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => false }, nil) - } - # rubocop:enable Metrics/LineLength - end - - describe '#migrations_from_branch' do - it 'orders by type and version' do - expect(feedback.migrations_from_branch.map(&:version)).to eq([1, 3, 2, 4]) - end - end - - describe '#other_migrations' do - subject(:feedback) { described_class.new(Result.new(other_migrations, nil)) } - - let(:other_migrations) { migrations.each { |_, v| v.intro_on_current_branch = !v.intro_on_current_branch } } - - it 'orders by type and version' do - expect(feedback.other_migrations.map(&:version)).to eq([1, 3, 2, 4]) - end - end -end diff --git a/notifier/spec/migration_spec.rb b/notifier/spec/migration_spec.rb index f15c3233c1e71444db9cbfd5f3cf642a4925bc40..aea997830d75fe70fc6cb8a62a9cc205b7cb0022 100644 --- a/notifier/spec/migration_spec.rb +++ b/notifier/spec/migration_spec.rb @@ -155,4 +155,16 @@ RSpec.describe Migration do expect(subject.important_queries.size).to eq(2) end end + + describe '#sort_key' do + it 'returns a representation of type and version' do + post_migration = described_class.new( + { 'version' => 42, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => true }, + nil + ) + + expect(subject.sort_key).to eq([0, 20210602144718]) + expect(post_migration.sort_key).to eq([1, 42]) + end + end end diff --git a/notifier/spec/result_spec.rb b/notifier/spec/result_spec.rb index c5ab20c499b76621e71a1a6af38df1fc76122bfb..2be3683c2e47919f354910e16479437e35a6a4fc 100644 --- a/notifier/spec/result_spec.rb +++ b/notifier/spec/result_spec.rb @@ -16,4 +16,37 @@ RSpec.describe Result do it 'loads migrations' do expect(result.migrations).not_to be_empty end + + describe 'sorting and filtering' do + let(:migrations) do + # rubocop:disable Metrics/LineLength + { + 4 => Migration.new({ 'version' => 4, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => true }, nil), + 3 => Migration.new({ 'version' => 3, 'type' => Migration::TYPE_REGULAR, 'intro_on_current_branch' => true }, nil), + 1 => Migration.new({ 'version' => 1, 'type' => Migration::TYPE_REGULAR, 'intro_on_current_branch' => true }, nil), + 2 => Migration.new({ 'version' => 2, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => true }, nil), + 8 => Migration.new({ 'version' => 8, 'type' => Migration::TYPE_REGULAR, 'intro_on_current_branch' => false }, nil), + 7 => Migration.new({ 'version' => 7, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => false }, nil), + 5 => Migration.new({ 'version' => 5, 'type' => Migration::TYPE_POST_DEPLOY, 'intro_on_current_branch' => false }, nil), + 6 => Migration.new({ 'version' => 6, 'type' => Migration::TYPE_REGULAR, 'intro_on_current_branch' => false }, nil) + } + # rubocop:enable Metrics/LineLength + end + + describe '#migrations_from_branch' do + subject(:result) { described_class.new(migrations, clone_details) } + + it 'returns migrations from the branch ordered by type and version' do + expect(result.migrations_from_branch.map(&:version)).to eq([1, 3, 2, 4]) + end + end + + describe '#other_migrations' do + subject(:result) { described_class.new(migrations, clone_details) } + + it 'returns other migrations ordered by type and version' do + expect(result.other_migrations.map(&:version)).to eq([6, 8, 5, 7]) + end + end + end end