Skip to content
代码片段 群组 项目
提交 639e19ed 编辑于 作者: Diogo Frazão's avatar Diogo Frazão 提交者: Prabakaran Murugesan
浏览文件

Validate the batched background migrations dictionary

- Add a new rubocop to validate the `introduced_by_url`
  url

Changelog: added
Relates to: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/137970
上级 2a591929
No related branches found
No related tags found
无相关合并请求
......@@ -2,7 +2,7 @@
migration_job_name: BackfillFindingIdInVulnerabilities
description: Backfills finding_id column on vulnerabilities table for a proper 1:1 relation
feature_category: vulnerability_management
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/issues/418971
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/130058
queued_migration_version: 20231116105945
milestone: 16.4
finalize_after: '2023-12-15'
......@@ -3,6 +3,8 @@
require_relative '../../migration_helpers'
require_relative '../../batched_background_migrations_dictionary'
URL_PATTERN = %r{\Ahttps://gitlab\.com/gitlab-org/gitlab/-/merge_requests/\d+\z}
module RuboCop
module Cop
module BackgroundMigration
......@@ -11,6 +13,7 @@ class DictionaryFile < RuboCop::Cop::Base
include MigrationHelpers
MSG = {
invalid_url: "Invalid `%{key}` url for the dictionary. Please use the following format: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/XXX",
missing_key: "Mandatory key '%{key}' is missing from the dictionary. Please add with an appropriate value.",
missing_dictionary: <<-MESSAGE.delete("\n").squeeze(' ').strip
Missing %{file_name}.
......@@ -49,6 +52,10 @@ def on_class(node)
private
def valid_url?(url)
url.match?(URL_PATTERN)
end
def dictionary_file?(migration_class_name)
File.exist?(dictionary_file_path(migration_class_name))
end
......@@ -67,6 +74,8 @@ def validate_dictionary_file(migration_name, node)
return [:missing_key, { key: :finalize_after }] unless bbm_dictionary.finalize_after.present?
return [:missing_key, { key: :introduced_by_url }] unless bbm_dictionary.introduced_by_url.present?
return [:invalid_url, { key: :introduced_by_url }] unless valid_url?(bbm_dictionary.introduced_by_url)
end
def rails_root
......
......@@ -134,7 +134,7 @@ def up
end
context 'with dictionary file' do
let(:introduced_by_url) { 'https://test_url' }
let(:introduced_by_url) { 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/132639' }
let(:finalize_after) { '20230507160251' }
before do
......@@ -158,6 +158,25 @@ def up
end
end
context 'when the `introduced_by_url` is not correct' do
let(:introduced_by_url) { 'https://gitlab.com/gitlab-org/gitlab/-/merge_requests/132639/invalid' }
it 'throws offense on having a correct url' do
expect_offense(<<~RUBY)
class QueueMyMigration < Gitlab::Database::Migration[2.1]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ #{format('Invalid `introduced_by_url` url for the dictionary. Please use the following format: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/XXX')}
def up
queue_batched_background_migration(
'MyMigration',
:users,
:id
)
end
end
RUBY
end
end
context 'with required dictionary keys' do
it 'does not throw offense with appropriate dictionary file' do
expect_no_offenses(<<~RUBY)
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册