diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 18:40:28 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2022-07-20 18:40:28 +0300 |
commit | b595cb0c1dec83de5bdee18284abe86614bed33b (patch) | |
tree | 8c3d4540f193c5ff98019352f554e921b3a41a72 /lib/tasks | |
parent | 2f9104a328fc8a4bddeaa4627b595166d24671d0 (diff) |
Add latest changes from gitlab-org/gitlab@15-2-stable-eev15.2.0-rc42
Diffstat (limited to 'lib/tasks')
-rw-r--r-- | lib/tasks/contracts/merge_requests.rake (renamed from lib/tasks/contracts.rake) | 13 | ||||
-rw-r--r-- | lib/tasks/contracts/pipelines.rake | 39 | ||||
-rw-r--r-- | lib/tasks/dev.rake | 3 | ||||
-rw-r--r-- | lib/tasks/gems.rake | 108 | ||||
-rw-r--r-- | lib/tasks/gitlab/bulk_add_permission.rake | 12 | ||||
-rw-r--r-- | lib/tasks/gitlab/db.rake | 66 | ||||
-rw-r--r-- | lib/tasks/gitlab/db/lock_writes.rake | 6 | ||||
-rw-r--r-- | lib/tasks/gitlab/graphql.rake | 6 | ||||
-rw-r--r-- | lib/tasks/gitlab/web_hook.rake | 3 |
9 files changed, 236 insertions, 20 deletions
diff --git a/lib/tasks/contracts.rake b/lib/tasks/contracts/merge_requests.rake index 6bb7f30ad57..05ed9c30495 100644 --- a/lib/tasks/contracts.rake +++ b/lib/tasks/contracts/merge_requests.rake @@ -4,12 +4,12 @@ return if Rails.env.production? require 'pact/tasks/verification_task' -contracts = File.expand_path('../../spec/contracts', __dir__) +contracts = File.expand_path('../../../spec/contracts', __dir__) provider = File.expand_path('provider', contracts) # rubocop:disable Rails/RakeEnvironment namespace :contracts do - namespace :mr do + namespace :merge_requests do Pact::VerificationTask.new(:diffs_batch) do |pact| pact.uri( "#{contracts}/contracts/project/merge_request/show/mergerequest#show-merge_request_diffs_batch_endpoint.json", @@ -33,14 +33,11 @@ namespace :contracts do end desc 'Run all merge request contract tests' - task 'test:merge_request', :contract_mr do |_t, arg| - raise(ArgumentError, 'Merge request contract tests require contract_mr to be set') unless arg[:contract_mr] - - ENV['CONTRACT_MR'] = arg[:contract_mr] - errors = %w[metadata discussions diffs].each_with_object([]) do |task, err| + task 'test:merge_requests', :contract_mr do |_t, arg| + errors = %w[diffs_batch diffs_metadata discussions].each_with_object([]) do |task, err| Rake::Task["contracts:mr:pact:verify:#{task}"].execute rescue StandardError, SystemExit - err << "contracts:mr:pact:verify:#{task}" + err << "contracts:merge_requests:pact:verify:#{task}" end raise StandardError, "Errors in tasks #{errors.join(', ')}" unless errors.empty? diff --git a/lib/tasks/contracts/pipelines.rake b/lib/tasks/contracts/pipelines.rake new file mode 100644 index 00000000000..c018645722e --- /dev/null +++ b/lib/tasks/contracts/pipelines.rake @@ -0,0 +1,39 @@ +# frozen_string_literal: true + +return if Rails.env.production? + +require 'pact/tasks/verification_task' + +contracts = File.expand_path('../../../spec/contracts', __dir__) +provider = File.expand_path('provider', contracts) + +# rubocop:disable Rails/RakeEnvironment +namespace :contracts do + namespace :pipelines do + Pact::VerificationTask.new(:get_list_project_pipelines) do |pact| + pact.uri( + "#{contracts}/contracts/project/pipeline/index/pipelines#index-get_list_project_pipelines.json", + pact_helper: "#{provider}/pact_helpers/project/pipeline/get_list_project_pipelines_helper.rb" + ) + end + + Pact::VerificationTask.new(:get_pipeline_header_data) do |pact| + pact.uri( + "#{contracts}/contracts/project/pipeline/show/pipelines#show-get_pipeline_header_data.json", + pact_helper: "#{provider}/pact_helpers/project/pipeline/get_pipeline_header_data_helper.rb" + ) + end + + desc 'Run all pipeline contract tests' + task 'test:pipelines', :contract_mr do |_t, arg| + errors = %w[get_list_project_pipelines get_pipeline_header_data].each_with_object([]) do |task, err| + Rake::Task["contracts:pipelines:pact:verify:#{task}"].execute + rescue StandardError, SystemExit + err << "contracts:pipelines:pact:verify:#{task}" + end + + raise StandardError, "Errors in tasks #{errors.join(', ')}" unless errors.empty? + end + end +end +# rubocop:enable Rails/RakeEnvironment diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 08a11100431..48bf49ff284 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -50,6 +50,9 @@ namespace :dev do connection.execute(cmd) rescue ActiveRecord::NoDatabaseError end + + # Clear connections opened by this rake task too + ActiveRecord::Base.clear_all_connections! # rubocop:disable Database/MultipleDatabases end end diff --git a/lib/tasks/gems.rake b/lib/tasks/gems.rake new file mode 100644 index 00000000000..c6be6d9eead --- /dev/null +++ b/lib/tasks/gems.rake @@ -0,0 +1,108 @@ +# frozen_string_literal: true + +namespace :gems do + # :nocov: + namespace :error_tracking_open_api do + desc 'Generate OpenAPI client for Error Tracking' + # rubocop:disable Rails/RakeEnvironment + task :generate do |task| + # Configuration + api_url = 'https://gitlab.com/gitlab-org/opstrace/opstrace/-/raw/main/go/pkg/errortracking/swagger.yaml' + gem_name = 'error_tracking_open_api' + module_name = 'ErrorTrackingOpenAPI' # Namespacing is not supported like `ErrorTracking::OpenAPI` + docker_image = 'openapitools/openapi-generator-cli:v6.0.0' + + vendor_gem_dir = Pathname.new(root_directory) + gem_dir = vendor_gem_dir / gem_name + + # Always start with a clean state. + rm_rf(gem_dir) + + generate_gem( + vendor_gem_dir: vendor_gem_dir, + api_url: api_url, + gem_name: gem_name, + module_name: module_name, + docker_image: docker_image + ) + + post_process(gem_dir: gem_dir, gem_name: gem_name, task: task) + end + # rubocop:enable Rails/RakeEnvironment + + def root_directory + File.expand_path('../../vendor/gems', __dir__) + end + + def generate_gem(vendor_gem_dir:, api_url:, gem_name:, module_name:, docker_image:) + user_id = File.stat(vendor_gem_dir).uid + + Kernel.system('docker', 'run', + "--user=#{user_id}", '--rm', "--volume=#{vendor_gem_dir}:/code", docker_image, + 'generate', + '--input-spec', api_url, + '--generator-name', 'ruby', + '--output', "/code/#{gem_name}", + "--additional-properties=moduleName=#{module_name}" + ) + end + + def post_process(gem_dir:, gem_name:, task:) + write_file(gem_dir / 'README.md') do |content| + readme_banner(task) + content + end + + write_file(gem_dir / 'LICENSE', license) + write_file(gem_dir / "#{gem_name}.gemspec") do |content| + replace_string(content, 'Unlicense', 'MIT') + replace_string(content, /(\.files\s*=).*/, '\1 Dir.glob("lib/**/*")') + replace_string(content, /(\.test_files\s*=).*/, '\1 []') + end + + remove_entry_secure(gem_dir / 'Gemfile') + remove_entry_secure(gem_dir / '.rubocop.yml') + remove_entry_secure(gem_dir / '.travis.yml') + remove_entry_secure(gem_dir / 'git_push.sh') + remove_entry_secure(gem_dir / 'spec') + remove_entry_secure(gem_dir / '.rspec') + end + + def write_file(full_path, content = nil, &block) + content ||= yield(File.read(full_path)) + + File.write(full_path, content) + end + + def replace_string(content, from, to) + raise "#{from.inspect} not found" unless content.gsub!(from, to) + + content + end + + def readme_banner(task) + # rubocop:disable Rails/TimeZone + <<~BANNER + # Generated by `rake #{task.name}` on #{Time.now.strftime('%Y-%m-%d')} + + See https://gitlab.com/gitlab-org/gitlab/-/blob/master/doc/development/rake_tasks.md#update-openapi-client-for-error-tracking-feature + + BANNER + # rubocop:enable Rails/TimeZone + end + + def license + year = [2022, Date.today.year].uniq.join('-') + + <<~LICENSE + Copyright #{year} GitLab B.V. + + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + LICENSE + end + end + # :nocov: +end diff --git a/lib/tasks/gitlab/bulk_add_permission.rake b/lib/tasks/gitlab/bulk_add_permission.rake index df0c6a260a2..a903c743ea2 100644 --- a/lib/tasks/gitlab/bulk_add_permission.rake +++ b/lib/tasks/gitlab/bulk_add_permission.rake @@ -9,10 +9,10 @@ namespace :gitlab do project_ids = Project.pluck(:id) puts "Importing #{user_ids.size} users into #{project_ids.size} projects" - ProjectMember.add_users_to_projects(project_ids, user_ids, ProjectMember::DEVELOPER) + ProjectMember.add_members_to_projects(project_ids, user_ids, ProjectMember::DEVELOPER) puts "Importing #{admin_ids.size} admins into #{project_ids.size} projects" - ProjectMember.add_users_to_projects(project_ids, admin_ids, ProjectMember::MAINTAINER) + ProjectMember.add_members_to_projects(project_ids, admin_ids, ProjectMember::MAINTAINER) end desc "GitLab | Import | Add a specific user to all projects (as a developer)" @@ -20,7 +20,7 @@ namespace :gitlab do user = User.find_by(email: args.email) project_ids = Project.pluck(:id) puts "Importing #{user.email} users into #{project_ids.size} projects" - ProjectMember.add_users_to_projects(project_ids, Array.wrap(user.id), ProjectMember::DEVELOPER) + ProjectMember.add_members_to_projects(project_ids, Array.wrap(user.id), ProjectMember::DEVELOPER) end desc "GitLab | Import | Add all users to all groups (admin users are added as owners)" @@ -32,8 +32,8 @@ namespace :gitlab do puts "Importing #{user_ids.size} users into #{groups.size} groups" puts "Importing #{admin_ids.size} admins into #{groups.size} groups" groups.each do |group| - group.add_users(user_ids, GroupMember::DEVELOPER) - group.add_users(admin_ids, GroupMember::OWNER) + group.add_members(user_ids, GroupMember::DEVELOPER) + group.add_members(admin_ids, GroupMember::OWNER) end end @@ -43,7 +43,7 @@ namespace :gitlab do groups = Group.all puts "Importing #{user.email} users into #{groups.size} groups" groups.each do |group| - group.add_users(Array.wrap(user.id), GroupMember::DEVELOPER) + group.add_members(Array.wrap(user.id), GroupMember::DEVELOPER) end end end diff --git a/lib/tasks/gitlab/db.rake b/lib/tasks/gitlab/db.rake index a446a17dfc3..5ed54bb6921 100644 --- a/lib/tasks/gitlab/db.rake +++ b/lib/tasks/gitlab/db.rake @@ -94,7 +94,7 @@ namespace :gitlab do connection = Gitlab::Database.database_base_models['main'].connection databases_loaded << configure_database(connection) else - Gitlab::Database.database_base_models.each do |name, model| + Gitlab::Database.database_base_models_with_gitlab_shared.each do |name, model| next unless databases_with_tasks.any? { |db_with_tasks| db_with_tasks.name == name } databases_loaded << configure_database(model.connection, database_name: name) @@ -367,5 +367,69 @@ namespace :gitlab do Rake::Task['gitlab:db:execute_batched_migrations'].invoke end end + + namespace :dictionary do + DB_DOCS_PATH = File.join(Rails.root, 'db', 'docs') + + desc 'Generate database docs yaml' + task generate: :environment do + FileUtils.mkdir_p(DB_DOCS_PATH) unless Dir.exist?(DB_DOCS_PATH) + + Rails.application.eager_load! + + tables = Gitlab::Database.database_base_models.flat_map { |_, m| m.connection.tables } + classes = tables.to_h { |t| [t, []] } + + Gitlab::Database.database_base_models.each do |_, model_class| + model_class + .descendants + .reject(&:abstract_class) + .reject { |c| c.name =~ /^(?:EE::)?Gitlab::(?:BackgroundMigration|DatabaseImporters)::/ } + .reject { |c| c.name =~ /^HABTM_/ } + .each { |c| classes[c.table_name] << c.name if classes.has_key?(c.table_name) } + end + + version = Gem::Version.new(File.read('VERSION')) + milestone = version.release.segments[0..1].join('.') + + tables.each do |table_name| + file = File.join(DB_DOCS_PATH, "#{table_name}.yml") + + table_metadata = { + 'table_name' => table_name, + 'classes' => classes[table_name]&.sort&.uniq, + 'feature_categories' => [], + 'description' => nil, + 'introduced_by_url' => nil, + 'milestone' => milestone + } + + if File.exist?(file) + outdated = false + + existing_metadata = YAML.safe_load(File.read(file)) + + if existing_metadata['table_name'] != table_metadata['table_name'] + existing_metadata['table_name'] = table_metadata['table_name'] + outdated = true + end + + if existing_metadata['classes'].difference(table_metadata['classes']).any? + existing_metadata['classes'] = table_metadata['classes'] + outdated = true + end + + File.write(file, existing_metadata.to_yaml) if outdated + else + File.write(file, table_metadata.to_yaml) + end + end + end + + # Temporary disable this, see https://gitlab.com/gitlab-org/gitlab/-/merge_requests/85760#note_998452069 + # Rake::Task['db:migrate'].enhance do + # Rake::Task['gitlab:db:dictionary:generate'].invoke if Rails.env.development? + # end + end end end diff --git a/lib/tasks/gitlab/db/lock_writes.rake b/lib/tasks/gitlab/db/lock_writes.rake index b57c2860fe3..3a083036781 100644 --- a/lib/tasks/gitlab/db/lock_writes.rake +++ b/lib/tasks/gitlab/db/lock_writes.rake @@ -11,6 +11,9 @@ namespace :gitlab do schemas_for_connection = Gitlab::Database.gitlab_schemas_for_connection(connection) Gitlab::Database::GitlabSchema.tables_to_schema.each do |table_name, schema_name| + # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/366834 + next if schema_name == :gitlab_geo + if schemas_for_connection.include?(schema_name.to_sym) drop_write_trigger(database_name, connection, table_name) else @@ -24,6 +27,9 @@ namespace :gitlab do task unlock_writes: :environment do Gitlab::Database::EachDatabase.each_database_connection do |connection, database_name| Gitlab::Database::GitlabSchema.tables_to_schema.each do |table_name, schema_name| + # TODO: https://gitlab.com/gitlab-org/gitlab/-/issues/366834 + next if schema_name == :gitlab_geo + drop_write_trigger(database_name, connection, table_name) end drop_write_trigger_function(connection) diff --git a/lib/tasks/gitlab/graphql.rake b/lib/tasks/gitlab/graphql.rake index b9137aa0d4c..a05b749a60e 100644 --- a/lib/tasks/gitlab/graphql.rake +++ b/lib/tasks/gitlab/graphql.rake @@ -15,10 +15,8 @@ namespace :gitlab do # Also avoids pipeline failures in case developer # dumps schema with flags disabled locally before pushing task enable_feature_flags: :environment do - class Feature - def self.enabled?(*args) - true - end + def Feature.enabled?(*args) + true end end diff --git a/lib/tasks/gitlab/web_hook.rake b/lib/tasks/gitlab/web_hook.rake index 091743485c9..fc17c7d0177 100644 --- a/lib/tasks/gitlab/web_hook.rake +++ b/lib/tasks/gitlab/web_hook.rake @@ -39,7 +39,8 @@ namespace :gitlab do web_hooks.find_each do |hook| next unless hook.url == web_hook_url - result = WebHooks::DestroyService.new(nil).sync_destroy(hook) + user = hook.parent.owners.first + result = WebHooks::DestroyService.new(user).execute(hook) raise "Unable to destroy Web hook" unless result[:status] == :success |