Commit c956d96f authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch '323228-ambiguous-error-message-when-missing-public-dir' into 'master'

Handle missing `public` pages directory as successful case

See merge request gitlab-org/gitlab!55829
parents 9887ba2b e46cec3a
...@@ -1848,7 +1848,7 @@ class Project < ApplicationRecord ...@@ -1848,7 +1848,7 @@ class Project < ApplicationRecord
# where().update_all to perform update in the single transaction with check for null # where().update_all to perform update in the single transaction with check for null
ProjectPagesMetadatum ProjectPagesMetadatum
.where(project_id: id, pages_deployment_id: nil) .where(project_id: id, pages_deployment_id: nil)
.update_all(pages_deployment_id: deployment.id) .update_all(deployed: deployment.present?, pages_deployment_id: deployment&.id)
end end
def write_repository_config(gl_full_path: full_path) def write_repository_config(gl_full_path: full_path)
......
...@@ -64,7 +64,7 @@ module Pages ...@@ -64,7 +64,7 @@ module Pages
end end
if result[:status] == :success if result[:status] == :success
@logger.info("project_id: #{project.id} #{project.pages_path} has been migrated in #{time.round(2)} seconds") @logger.info("project_id: #{project.id} #{project.pages_path} has been migrated in #{time.round(2)} seconds: #{result[:message]}")
@counters_lock.synchronize { @migrated += 1 } @counters_lock.synchronize { @migrated += 1 }
else else
@logger.error("project_id: #{project.id} #{project.pages_path} failed to be migrated in #{time.round(2)} seconds: #{result[:message]}") @logger.error("project_id: #{project.id} #{project.pages_path} failed to be migrated in #{time.round(2)} seconds: #{result[:message]}")
......
...@@ -30,16 +30,18 @@ module Pages ...@@ -30,16 +30,18 @@ module Pages
zip_result = ::Pages::ZipDirectoryService.new(project.pages_path, ignore_invalid_entries: @ignore_invalid_entries).execute zip_result = ::Pages::ZipDirectoryService.new(project.pages_path, ignore_invalid_entries: @ignore_invalid_entries).execute
if zip_result[:status] == :error if zip_result[:status] == :error
if !project.pages_metadatum&.reload&.pages_deployment &&
Feature.enabled?(:pages_migration_mark_as_not_deployed, project)
project.mark_pages_as_not_deployed
end
return error("Can't create zip archive: #{zip_result[:message]}") return error("Can't create zip archive: #{zip_result[:message]}")
end end
archive_path = zip_result[:archive_path] archive_path = zip_result[:archive_path]
unless archive_path
project.set_first_pages_deployment!(nil)
return success(
message: "Archive not created. Missing public directory in #{project.pages_path} ? Marked project as not deployed")
end
deployment = nil deployment = nil
File.open(archive_path) do |file| File.open(archive_path) do |file|
deployment = project.pages_deployments.create!( deployment = project.pages_deployments.create!(
......
...@@ -19,6 +19,10 @@ module Pages ...@@ -19,6 +19,10 @@ module Pages
def execute def execute
unless resolve_public_dir unless resolve_public_dir
if Feature.enabled?(:pages_migration_mark_as_not_deployed)
return success
end
return error("Can not find valid public dir in #{@input_dir}") return error("Can not find valid public dir in #{@input_dir}")
end end
......
...@@ -6016,12 +6016,15 @@ RSpec.describe Project, factory_default: :keep do ...@@ -6016,12 +6016,15 @@ RSpec.describe Project, factory_default: :keep do
project.set_first_pages_deployment!(deployment) project.set_first_pages_deployment!(deployment)
expect(project.pages_metadatum.reload.pages_deployment).to eq(deployment) expect(project.pages_metadatum.reload.pages_deployment).to eq(deployment)
expect(project.pages_metadatum.reload.deployed).to eq(true)
end end
it "updates the existing metadara record with deployment" do it "updates the existing metadara record with deployment" do
expect do expect do
project.set_first_pages_deployment!(deployment) project.set_first_pages_deployment!(deployment)
end.to change { project.pages_metadatum.reload.pages_deployment }.from(nil).to(deployment) end.to change { project.pages_metadatum.reload.pages_deployment }.from(nil).to(deployment)
expect(project.pages_metadatum.reload.deployed).to eq(true)
end end
it 'only updates metadata for this project' do it 'only updates metadata for this project' do
...@@ -6030,6 +6033,8 @@ RSpec.describe Project, factory_default: :keep do ...@@ -6030,6 +6033,8 @@ RSpec.describe Project, factory_default: :keep do
expect do expect do
project.set_first_pages_deployment!(deployment) project.set_first_pages_deployment!(deployment)
end.not_to change { other_project.pages_metadatum.reload.pages_deployment }.from(nil) end.not_to change { other_project.pages_metadatum.reload.pages_deployment }.from(nil)
expect(other_project.pages_metadatum.reload.deployed).to eq(false)
end end
it 'does nothing if metadata already references some deployment' do it 'does nothing if metadata already references some deployment' do
...@@ -6040,6 +6045,14 @@ RSpec.describe Project, factory_default: :keep do ...@@ -6040,6 +6045,14 @@ RSpec.describe Project, factory_default: :keep do
project.set_first_pages_deployment!(deployment) project.set_first_pages_deployment!(deployment)
end.not_to change { project.pages_metadatum.reload.pages_deployment }.from(existing_deployment) end.not_to change { project.pages_metadatum.reload.pages_deployment }.from(existing_deployment)
end end
it 'marks project as not deployed if deployment is nil' do
project.mark_pages_as_deployed
expect do
project.set_first_pages_deployment!(nil)
end.to change { project.pages_metadatum.reload.deployed }.from(true).to(false)
end
end end
describe '#has_pool_repsitory?' do describe '#has_pool_repsitory?' do
......
...@@ -48,12 +48,12 @@ RSpec.describe Pages::MigrateFromLegacyStorageService do ...@@ -48,12 +48,12 @@ RSpec.describe Pages::MigrateFromLegacyStorageService do
end end
context 'when pages directory does not exist' do context 'when pages directory does not exist' do
it 'tries to migrate the project, but does not crash' do it 'counts project as migrated' do
expect_next_instance_of(::Pages::MigrateLegacyStorageToDeploymentService, project, ignore_invalid_entries: false) do |service| expect_next_instance_of(::Pages::MigrateLegacyStorageToDeploymentService, project, ignore_invalid_entries: false) do |service|
expect(service).to receive(:execute).and_call_original expect(service).to receive(:execute).and_call_original
end end
expect(service.execute).to eq(migrated: 0, errored: 1) expect(service.execute).to eq(migrated: 1, errored: 0)
end end
end end
......
...@@ -11,7 +11,7 @@ RSpec.describe Pages::MigrateLegacyStorageToDeploymentService do ...@@ -11,7 +11,7 @@ RSpec.describe Pages::MigrateLegacyStorageToDeploymentService do
expect(zip_service).to receive(:execute).and_call_original expect(zip_service).to receive(:execute).and_call_original
end end
expect(described_class.new(project, ignore_invalid_entries: true).execute[:status]).to eq(:error) expect(described_class.new(project, ignore_invalid_entries: true).execute[:status]).to eq(:success)
end end
it 'marks pages as not deployed if public directory is absent' do it 'marks pages as not deployed if public directory is absent' do
...@@ -20,8 +20,8 @@ RSpec.describe Pages::MigrateLegacyStorageToDeploymentService do ...@@ -20,8 +20,8 @@ RSpec.describe Pages::MigrateLegacyStorageToDeploymentService do
expect(project.pages_metadatum.reload.deployed).to eq(true) expect(project.pages_metadatum.reload.deployed).to eq(true)
expect(service.execute).to( expect(service.execute).to(
eq(status: :error, eq(status: :success,
message: "Can't create zip archive: Can not find valid public dir in #{project.pages_path}") message: "Archive not created. Missing public directory in #{project.pages_path} ? Marked project as not deployed")
) )
expect(project.pages_metadatum.reload.deployed).to eq(false) expect(project.pages_metadatum.reload.deployed).to eq(false)
...@@ -35,8 +35,8 @@ RSpec.describe Pages::MigrateLegacyStorageToDeploymentService do ...@@ -35,8 +35,8 @@ RSpec.describe Pages::MigrateLegacyStorageToDeploymentService do
expect(project.pages_metadatum.reload.deployed).to eq(true) expect(project.pages_metadatum.reload.deployed).to eq(true)
expect(service.execute).to( expect(service.execute).to(
eq(status: :error, eq(status: :success,
message: "Can't create zip archive: Can not find valid public dir in #{project.pages_path}") message: "Archive not created. Missing public directory in #{project.pages_path} ? Marked project as not deployed")
) )
expect(project.pages_metadatum.reload.deployed).to eq(true) expect(project.pages_metadatum.reload.deployed).to eq(true)
......
...@@ -12,8 +12,10 @@ RSpec.describe Pages::ZipDirectoryService do ...@@ -12,8 +12,10 @@ RSpec.describe Pages::ZipDirectoryService do
let(:ignore_invalid_entries) { false } let(:ignore_invalid_entries) { false }
let(:service_directory) { @work_dir }
let(:service) do let(:service) do
described_class.new(@work_dir, ignore_invalid_entries: ignore_invalid_entries) described_class.new(service_directory, ignore_invalid_entries: ignore_invalid_entries)
end end
let(:result) do let(:result) do
...@@ -25,32 +27,41 @@ RSpec.describe Pages::ZipDirectoryService do ...@@ -25,32 +27,41 @@ RSpec.describe Pages::ZipDirectoryService do
let(:archive) { result[:archive_path] } let(:archive) { result[:archive_path] }
let(:entries_count) { result[:entries_count] } let(:entries_count) { result[:entries_count] }
it 'returns error if project pages dir does not exist' do shared_examples 'handles invalid public directory' do
expect(Gitlab::ErrorTracking).not_to receive(:track_exception) it 'returns success' do
expect(status).to eq(:success)
expect( expect(archive).to be_nil
described_class.new("/tmp/not/existing/dir").execute expect(entries_count).to be_nil
).to eq(status: :error, message: "Can not find valid public dir in /tmp/not/existing/dir")
end end
it 'returns nils if there is no public directory and does not leave archive' do it 'returns error if pages_migration_mark_as_not_deployed is disabled' do
stub_feature_flags(pages_migration_mark_as_not_deployed: false)
expect(status).to eq(:error) expect(status).to eq(:error)
expect(message).to eq("Can not find valid public dir in #{@work_dir}") expect(message).to eq("Can not find valid public dir in #{service_directory}")
expect(archive).to eq(nil) expect(archive).to be_nil
expect(entries_count).to eq(nil) expect(entries_count).to be_nil
end
end
context "when work direcotry doesn't exist" do
let(:service_directory) { "/tmp/not/existing/dir" }
expect(File.exist?(File.join(@work_dir, '@migrated.zip'))).to eq(false) include_examples 'handles invalid public directory'
end end
it 'returns nils if public directory is a symlink' do context 'when public directory is absent' do
include_examples 'handles invalid public directory'
end
context 'when public directory is a symlink' do
before do
create_dir('target') create_dir('target')
create_file('./target/index.html', 'hello') create_file('./target/index.html', 'hello')
create_link("public", "./target") create_link("public", "./target")
end
expect(status).to eq(:error) include_examples 'handles invalid public directory'
expect(message).to eq("Can not find valid public dir in #{@work_dir}")
expect(archive).to eq(nil)
expect(entries_count).to eq(nil)
end end
context 'when there is a public directory' do context 'when there is a public directory' do
......
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