Commit 0f93d783 authored by Mayra Cabrera's avatar Mayra Cabrera

Merge branch '14732-prerequisite-refactoring-fetch-service' into 'master'

Add service to help fetch Security CI Report

See merge request gitlab-org/gitlab!17755
parents 0cd22231 03e60db8
......@@ -10,24 +10,15 @@ module Projects
format.json do
::Gitlab::UsageCounters::DependencyList.increment(project.id)
render json: serializer.represent(dependencies, build: build)
render json: serializer.represent(dependencies, build: report_service.build)
end
end
end
private
def build
return unless pipeline
return @build if @build
@build = pipeline.builds.latest
.with_reports(::Ci::JobArtifact.dependency_list_reports)
.last
end
def collect_dependencies
found_dependencies = build&.success? ? service.execute : []
found_dependencies = report_service.able_to_fetch? ? service.execute : []
::Gitlab::DependenciesCollection.new(found_dependencies)
end
......@@ -46,7 +37,7 @@ module Projects
end
def pipeline
@pipeline ||= project.all_pipelines.latest_successful_for_ref(project.default_branch)
@pipeline ||= report_service.pipeline
end
def query_params
......@@ -55,6 +46,10 @@ module Projects
end
end
def report_service
@report_service ||= ::Security::ReportFetchService.new(project, ::Ci::JobArtifact.dependency_list_reports)
end
def serializer
serializer = ::DependencyListSerializer.new(project: project, user: current_user)
serializer = serializer.with_pagination(request, response) if params[:page]
......
# frozen_string_literal: true
module Security
class ReportFetchService
def initialize(project, artifact)
@project = project
@artifact = artifact
end
def self.pipeline_for(project)
project.all_pipelines.latest_successful_for_ref(project.default_branch)
end
def pipeline
@pipeline ||= self.class.pipeline_for(project)
end
def build
return unless pipeline
@build ||= pipeline.builds.latest
.with_reports(artifact)
.last
end
def able_to_fetch?
build&.success?
end
private
attr_reader :project, :artifact
end
end
......@@ -4,7 +4,7 @@ module API
class Dependencies < Grape::API
helpers do
def dependencies_by(params)
pipeline = user_project.all_pipelines.latest_successful_for_ref(user_project.default_branch)
pipeline = ::Security::ReportFetchService.pipeline_for(user_project)
return [] unless pipeline
......
# frozen_string_literal: true
require 'spec_helper'
describe Security::ReportFetchService do
set(:project) { create(:project) }
let(:service) { described_class.new(project, artifact) }
let(:artifact) { ::Ci::JobArtifact.dependency_list_reports }
describe '.pipeline_for' do
subject { described_class.pipeline_for(project) }
context 'with found pipeline' do
let!(:pipeline) { create(:ee_ci_pipeline, :with_dependency_list_report, project: project) }
it { is_expected.to eq(pipeline) }
end
context 'without any pipelines' do
it { is_expected.to be_nil }
end
end
describe '#pipeline' do
subject { service.pipeline }
context 'with found pipeline' do
let!(:pipeline1) { create(:ee_ci_pipeline, :with_dependency_list_report, project: project) }
let!(:pipeline2) { create(:ee_ci_pipeline, :with_dependency_list_report, project: project) }
it { is_expected.to eq(pipeline2) }
end
context 'without any pipelines' do
it { is_expected.to be_nil }
end
end
describe '#build' do
subject { service.build }
context 'with right artifacts' do
let!(:pipeline) { create(:ee_ci_pipeline, :with_dependency_list_report, project: project) }
let(:build) { pipeline.builds.last }
it { is_expected.to eq(build) }
end
context 'without right kind of artifacts' do
let!(:pipeline) { create(:ee_ci_pipeline, :with_sast_report, project: project) }
it { is_expected.to be_nil }
end
context 'without found pipeline' do
it { is_expected.to be_nil }
end
end
describe '#able_to_fetch?' do
subject { service.able_to_fetch? }
before do
allow(service).to receive(:build).and_return(build)
end
context 'with successful build' do
let(:build) { create(:ci_build, :success) }
it { is_expected.to be_truthy }
end
context 'with failed build' do
let(:build) { create(:ci_build, :failed) }
it { is_expected.to be_falsey }
end
context 'without build' do
let(:build) { nil }
it { is_expected.to be_falsey }
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