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

Merge branch '281174-add-source-sha256-to-the-internal-pages-api' into 'master'

Add additional fields to pages API

See merge request gitlab-org/gitlab!47423
parents 3e64c76b 3afe1e07
...@@ -40,37 +40,35 @@ module Pages ...@@ -40,37 +40,35 @@ module Pages
def artifacts_archive def artifacts_archive
return unless Feature.enabled?(:pages_serve_from_artifacts_archive, project) return unless Feature.enabled?(:pages_serve_from_artifacts_archive, project)
archive = project.pages_metadatum.artifacts_archive project.pages_metadatum.artifacts_archive
archive&.file
end end
def deployment def deployment
return unless Feature.enabled?(:pages_serve_from_deployments, project) return unless Feature.enabled?(:pages_serve_from_deployments, project)
deployment = project.pages_metadatum.pages_deployment project.pages_metadatum.pages_deployment
deployment&.file
end end
def zip_source def zip_source
source = deployment || artifacts_archive source = deployment || artifacts_archive
return unless source return unless source&.file
if source.file_storage? return if source.file.file_storage? && !Feature.enabled?(:pages_serve_with_zip_file_protocol, project)
return unless Feature.enabled?(:pages_serve_with_zip_file_protocol, project)
{ # artifacts archive doesn't support this
type: 'zip', file_count = source.file_count if source.respond_to?(:file_count)
path: 'file://' + source.path
} global_id = ::Gitlab::GlobalId.build(source, id: source.id).to_s
else
{ {
type: 'zip', type: 'zip',
path: source.url(expire_at: 1.day.from_now) path: source.file.url_or_file_path(expire_at: 1.day.from_now),
} global_id: global_id,
end sha256: source.file_sha256,
file_size: source.size,
file_count: file_count
}
end end
def file_source def file_source
......
...@@ -118,6 +118,14 @@ class GitlabUploader < CarrierWave::Uploader::Base ...@@ -118,6 +118,14 @@ class GitlabUploader < CarrierWave::Uploader::Base
storage.store!(file) storage.store!(file)
end end
def url_or_file_path(url_options = {})
if file_storage?
'file://' + path
else
url(url_options)
end
end
private private
# Designed to be overridden by child uploaders that have a dynamic path # Designed to be overridden by child uploaders that have a dynamic path
......
...@@ -15,7 +15,11 @@ ...@@ -15,7 +15,11 @@
"required": ["type", "path"], "required": ["type", "path"],
"properties" : { "properties" : {
"type": { "type": "string", "enum": ["file", "zip"] }, "type": { "type": "string", "enum": ["file", "zip"] },
"path": { "type": "string" } "path": { "type": "string" },
"global_id": { "type": "string" },
"sha256": { "type": "string" },
"file_size": { "type": "integer" },
"file_count": { "type": ["integer", "null"] }
}, },
"additionalProperties": false "additionalProperties": false
}, },
......
...@@ -65,11 +65,18 @@ RSpec.describe Pages::LookupPath do ...@@ -65,11 +65,18 @@ RSpec.describe Pages::LookupPath do
project.pages_metadatum.update!(pages_deployment: deployment) project.pages_metadatum.update!(pages_deployment: deployment)
end end
it 'uses deployment from object storage', :aggregate_failures do it 'uses deployment from object storage' do
Timecop.freeze do Timecop.freeze do
expect(source[:type]).to eq('zip') expect(source).to(
expect(source[:path]).to eq(deployment.file.url(expire_at: 1.day.from_now)) eq({
expect(source[:path]).to include("Expires=86400") type: 'zip',
path: deployment.file.url(expire_at: 1.day.from_now),
global_id: "gid://gitlab/PagesDeployment/#{deployment.id}",
sha256: deployment.file_sha256,
file_size: deployment.size,
file_count: deployment.file_count
})
)
end end
end end
...@@ -78,10 +85,18 @@ RSpec.describe Pages::LookupPath do ...@@ -78,10 +85,18 @@ RSpec.describe Pages::LookupPath do
deployment.file.migrate!(::ObjectStorage::Store::LOCAL) deployment.file.migrate!(::ObjectStorage::Store::LOCAL)
end end
it 'uses file protocol', :aggregate_failures do it 'uses file protocol' do
Timecop.freeze do Timecop.freeze do
expect(source[:type]).to eq('zip') expect(source).to(
expect(source[:path]).to eq('file://' + deployment.file.path) eq({
type: 'zip',
path: 'file://' + deployment.file.path,
global_id: "gid://gitlab/PagesDeployment/#{deployment.id}",
sha256: deployment.file_sha256,
file_size: deployment.size,
file_count: deployment.file_count
})
)
end end
end end
...@@ -110,11 +125,18 @@ RSpec.describe Pages::LookupPath do ...@@ -110,11 +125,18 @@ RSpec.describe Pages::LookupPath do
project.mark_pages_as_deployed(artifacts_archive: artifacts_archive) project.mark_pages_as_deployed(artifacts_archive: artifacts_archive)
end end
it 'uses artifacts object storage', :aggregate_failures do it 'uses artifacts object storage' do
Timecop.freeze do Timecop.freeze do
expect(source[:type]).to eq('zip') expect(source).to(
expect(source[:path]).to eq(artifacts_archive.file.url(expire_at: 1.day.from_now)) eq({
expect(source[:path]).to include("Expires=86400") type: 'zip',
path: artifacts_archive.file.url(expire_at: 1.day.from_now),
global_id: "gid://gitlab/Ci::JobArtifact/#{artifacts_archive.id}",
sha256: artifacts_archive.file_sha256,
file_size: artifacts_archive.size,
file_count: nil
})
)
end end
end end
...@@ -123,8 +145,16 @@ RSpec.describe Pages::LookupPath do ...@@ -123,8 +145,16 @@ RSpec.describe Pages::LookupPath do
it 'uses file protocol', :aggregate_failures do it 'uses file protocol', :aggregate_failures do
Timecop.freeze do Timecop.freeze do
expect(source[:type]).to eq('zip') expect(source).to(
expect(source[:path]).to eq('file://' + artifacts_archive.file.path) eq({
type: 'zip',
path: 'file://' + artifacts_archive.file.path,
global_id: "gid://gitlab/Ci::JobArtifact/#{artifacts_archive.id}",
sha256: artifacts_archive.file_sha256,
file_size: artifacts_archive.size,
file_count: nil
})
)
end end
end end
......
...@@ -191,6 +191,7 @@ RSpec.describe API::Internal::Pages do ...@@ -191,6 +191,7 @@ RSpec.describe API::Internal::Pages do
expect(json_response['certificate']).to eq(pages_domain.certificate) expect(json_response['certificate']).to eq(pages_domain.certificate)
expect(json_response['key']).to eq(pages_domain.key) expect(json_response['key']).to eq(pages_domain.key)
deployment = project.pages_metadatum.pages_deployment
expect(json_response['lookup_paths']).to eq( expect(json_response['lookup_paths']).to eq(
[ [
{ {
...@@ -200,7 +201,11 @@ RSpec.describe API::Internal::Pages do ...@@ -200,7 +201,11 @@ RSpec.describe API::Internal::Pages do
'prefix' => '/', 'prefix' => '/',
'source' => { 'source' => {
'type' => 'zip', 'type' => 'zip',
'path' => project.pages_metadatum.pages_deployment.file.url(expire_at: 1.day.from_now) 'path' => deployment.file.url(expire_at: 1.day.from_now),
'global_id' => "gid://gitlab/PagesDeployment/#{deployment.id}",
'sha256' => deployment.file_sha256,
'file_size' => deployment.size,
'file_count' => deployment.file_count
} }
} }
] ]
...@@ -227,6 +232,7 @@ RSpec.describe API::Internal::Pages do ...@@ -227,6 +232,7 @@ RSpec.describe API::Internal::Pages do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('internal/pages/virtual_domain') expect(response).to match_response_schema('internal/pages/virtual_domain')
deployment = project.pages_metadatum.pages_deployment
expect(json_response['lookup_paths']).to eq( expect(json_response['lookup_paths']).to eq(
[ [
{ {
...@@ -236,7 +242,11 @@ RSpec.describe API::Internal::Pages do ...@@ -236,7 +242,11 @@ RSpec.describe API::Internal::Pages do
'prefix' => '/myproject/', 'prefix' => '/myproject/',
'source' => { 'source' => {
'type' => 'zip', 'type' => 'zip',
'path' => project.pages_metadatum.pages_deployment.file.url(expire_at: 1.day.from_now) 'path' => deployment.file.url(expire_at: 1.day.from_now),
'global_id' => "gid://gitlab/PagesDeployment/#{deployment.id}",
'sha256' => deployment.file_sha256,
'file_size' => deployment.size,
'file_count' => deployment.file_count
} }
} }
] ]
...@@ -268,6 +278,7 @@ RSpec.describe API::Internal::Pages do ...@@ -268,6 +278,7 @@ RSpec.describe API::Internal::Pages do
expect(response).to have_gitlab_http_status(:ok) expect(response).to have_gitlab_http_status(:ok)
expect(response).to match_response_schema('internal/pages/virtual_domain') expect(response).to match_response_schema('internal/pages/virtual_domain')
deployment = project.pages_metadatum.pages_deployment
expect(json_response['lookup_paths']).to eq( expect(json_response['lookup_paths']).to eq(
[ [
{ {
...@@ -277,7 +288,11 @@ RSpec.describe API::Internal::Pages do ...@@ -277,7 +288,11 @@ RSpec.describe API::Internal::Pages do
'prefix' => '/', 'prefix' => '/',
'source' => { 'source' => {
'type' => 'zip', 'type' => 'zip',
'path' => project.pages_metadatum.pages_deployment.file.url(expire_at: 1.day.from_now) 'path' => deployment.file.url(expire_at: 1.day.from_now),
'global_id' => "gid://gitlab/PagesDeployment/#{deployment.id}",
'sha256' => deployment.file_sha256,
'file_size' => deployment.size,
'file_count' => deployment.file_count
} }
} }
] ]
......
...@@ -141,5 +141,23 @@ RSpec.describe GitlabUploader do ...@@ -141,5 +141,23 @@ RSpec.describe GitlabUploader do
end end
end end
end end
describe '#url_or_file_path' do
let(:options) { { expire_at: 1.day.from_now } }
it 'returns url when in remote storage' do
expect(subject).to receive(:file_storage?).and_return(false)
expect(subject).to receive(:url).with(options).and_return("http://example.com")
expect(subject.url_or_file_path(options)).to eq("http://example.com")
end
it 'returns url when in remote storage' do
expect(subject).to receive(:file_storage?).and_return(true)
expect(subject).to receive(:path).and_return("/tmp/file")
expect(subject.url_or_file_path(options)).to eq("file:///tmp/file")
end
end
end end
end end
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