Commit d01f2541 authored by Matija Čupić's avatar Matija Čupić

Extract artifact existence check into method

parent 16ce6833
...@@ -108,9 +108,7 @@ class Projects::ArtifactsController < Projects::ApplicationController ...@@ -108,9 +108,7 @@ class Projects::ArtifactsController < Projects::ApplicationController
end end
def validate_artifacts! def validate_artifacts!
if build.nil? || build.artifacts_file.nil? || (build.pipeline.unlocked? && build.artifacts_expired?) render_404 unless build&.available_artifacts?
render_404
end
end end
def build def build
......
...@@ -647,6 +647,13 @@ module Ci ...@@ -647,6 +647,13 @@ module Ci
!artifacts_expired? && artifacts_file&.exists? !artifacts_expired? && artifacts_file&.exists?
end end
# This method is similar to #artifacts? but it includes the artifacts
# locking mechanics. A new method was created to prevent breaking existing
# behavior and avoid introducing N+1s.
def available_artifacts?
(!artifacts_expired? || pipeline.artifacts_locked?) && job_artifacts_archive&.exists?
end
def artifacts_metadata? def artifacts_metadata?
artifacts? && artifacts_metadata&.exists? artifacts? && artifacts_metadata&.exists?
end end
......
...@@ -612,6 +612,62 @@ RSpec.describe Ci::Build do ...@@ -612,6 +612,62 @@ RSpec.describe Ci::Build do
end end
end end
describe '#available_artifacts?' do
let(:build) { create(:ci_build) }
subject { build.available_artifacts? }
context 'when artifacts are not expired' do
before do
build.artifacts_expire_at = Date.tomorrow
end
context 'when artifacts exist' do
before do
create(:ci_job_artifact, :archive, job: build)
end
it { is_expected.to be_truthy }
end
context 'when artifacts do not exist' do
it { is_expected.to be_falsey }
end
end
context 'when artifacts are expired' do
before do
build.artifacts_expire_at = Date.yesterday
end
context 'when artifacts are not locked' do
before do
build.pipeline.locked = :unlocked
end
it { is_expected.to be_falsey }
end
context 'when artifacts are locked' do
before do
build.pipeline.locked = :artifacts_locked
end
context 'when artifacts exist' do
before do
create(:ci_job_artifact, :archive, job: build)
end
it { is_expected.to be_truthy }
end
context 'when artifacts do not exist' do
it { is_expected.to be_falsey }
end
end
end
end
describe '#browsable_artifacts?' do describe '#browsable_artifacts?' do
subject { build.browsable_artifacts? } subject { build.browsable_artifacts? }
......
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