Count on-demand scans with PipelineScopeCounts

This replaces the custom scans counting logic with the now available
PipelineScopeCounts module and its GraphQL resolver.
parent 6510a18c
......@@ -7,13 +7,7 @@ import {
toggleQueryPollingByVisibility,
} from '~/pipelines/components/graph/utils';
import onDemandScanCounts from '../graphql/on_demand_scan_counts.query.graphql';
import {
HELP_PAGE_PATH,
PIPELINE_TABS_KEYS,
PIPELINES_COUNT_POLL_INTERVAL,
PIPELINES_SCOPE_RUNNING,
PIPELINES_SCOPE_FINISHED,
} from '../constants';
import { HELP_PAGE_PATH, PIPELINE_TABS_KEYS, PIPELINES_COUNT_POLL_INTERVAL } from '../constants';
import AllTab from './tabs/all.vue';
import RunningTab from './tabs/running.vue';
import FinishedTab from './tabs/finished.vue';
......@@ -42,8 +36,6 @@ export default {
variables() {
return {
fullPath: this.projectPath,
runningScope: PIPELINES_SCOPE_RUNNING,
finishedScope: PIPELINES_SCOPE_FINISHED,
};
},
context() {
......@@ -52,7 +44,7 @@ export default {
update(data) {
return Object.fromEntries(
PIPELINE_TABS_KEYS.map((key) => {
const { count } = data[key].pipelines;
const count = data?.project?.pipelineCounts?.[key] ?? data[key]?.pipelines?.count ?? 0;
return [key, count];
}),
);
......
query onDemandScanCounts(
$fullPath: ID!
$runningScope: PipelineScopeEnum
$finishedScope: PipelineScopeEnum
) {
all: project(fullPath: $fullPath) {
query onDemandScanCounts($fullPath: ID!) {
project(fullPath: $fullPath) {
id
pipelines(source: "ondemand_dast_scan") {
count
}
}
running: project(fullPath: $fullPath) {
id
pipelines(source: "ondemand_dast_scan", scope: $runningScope) {
count
}
}
finished: project(fullPath: $fullPath) {
id
pipelines(source: "ondemand_dast_scan", scope: $finishedScope) {
count
pipelineCounts(source: "ondemand_dast_scan") {
all
running
finished
}
}
scheduled: project(fullPath: $fullPath) {
......
......@@ -2,18 +2,19 @@
module Projects::OnDemandScansHelper
# rubocop: disable CodeReuse/ActiveRecord
def on_demand_scans_data(project)
on_demand_scans = project.all_pipelines.where(source: Enums::Ci::Pipeline.sources[:ondemand_dast_scan])
running_scans_count, finished_scans_count = count_running_and_finished_scans(on_demand_scans)
def on_demand_scans_data(current_user, project)
pipelines_counter = Gitlab::PipelineScopeCounts.new(current_user, project, {
source: "ondemand_dast_scan"
})
saved_scans = ::Dast::ProfilesFinder.new({ project_id: project.id }).execute
scheduled_scans_count = saved_scans.count { |scan| scan.dast_profile_schedule }
common_data(project).merge({
'project-on-demand-scan-counts-etag' => graphql_etag_project_on_demand_scan_counts_path(project),
'on-demand-scan-counts' => {
all: on_demand_scans.length,
running: running_scans_count,
finished: finished_scans_count,
all: pipelines_counter.all,
running: pipelines_counter.running,
finished: pipelines_counter.finished,
scheduled: scheduled_scans_count,
saved: saved_scans.count
}.to_json,
......@@ -43,19 +44,4 @@ module Projects::OnDemandScansHelper
'project-path' => project.path_with_namespace
}
end
def count_running_and_finished_scans(on_demand_scans)
running_scans_count = 0
finished_scans_count = 0
on_demand_scans.each do |pipeline|
if %w[success failed canceled].include?(pipeline.status)
finished_scans_count += 1
elsif pipeline.status == "running"
running_scans_count += 1
end
end
[running_scans_count, finished_scans_count]
end
end
......@@ -2,4 +2,4 @@
- page_title s_('OnDemandScans|On-demand Scans')
- add_page_specific_style 'page_bundles/ci_status'
#js-on-demand-scans{ data: on_demand_scans_data(@project) }
#js-on-demand-scans{ data: on_demand_scans_data(@current_user, @project) }
......@@ -39,9 +39,9 @@ RSpec.describe 'On-demand DAST scans (GraphQL fixtures)' do
})
expect_graphql_errors_to_be_empty
expect(graphql_data_at(:all, :pipelines, :count)).to be(4)
expect(graphql_data_at(:running, :pipelines, :count)).to be(2)
expect(graphql_data_at(:finished, :pipelines, :count)).to be(2)
expect(graphql_data_at(:project, :pipelineCounts, :all)).to be(4)
expect(graphql_data_at(:project, :pipelineCounts, :running)).to be(2)
expect(graphql_data_at(:project, :pipelineCounts, :finished)).to be(2)
end
end
......
......@@ -13,6 +13,7 @@ RSpec.describe Projects::OnDemandScansHelper do
end
describe '#on_demand_scans_data' do
let_it_be(:current_user) { create(:user) }
let_it_be(:dast_profile) { create(:dast_profile, project: project) }
let_it_be(:dast_profile_with_schedule) { create(:dast_profile, project: project) }
let_it_be(:dast_profile_schedule) { create(:dast_profile_schedule, project: project, dast_profile: dast_profile_with_schedule)}
......@@ -22,10 +23,11 @@ RSpec.describe Projects::OnDemandScansHelper do
create_list(:ci_pipeline, 8, :success, project: project, ref: 'master', source: :ondemand_dast_scan)
create_list(:ci_pipeline, 4, :running, project: project, ref: 'master', source: :ondemand_dast_scan)
allow(helper).to receive(:graphql_etag_project_on_demand_scan_counts_path).and_return(graphql_etag_project_on_demand_scan_counts_path)
project.add_developer(current_user)
end
it 'returns proper data' do
expect(helper.on_demand_scans_data(project)).to match(
expect(helper.on_demand_scans_data(current_user, project)).to match(
'project-path' => "foo/bar",
'new-dast-scan-path' => "/#{project.full_path}/-/on_demand_scans/new",
'empty-state-svg-path' => match_asset_path('/assets/illustrations/empty-state/ondemand-scan-empty.svg'),
......
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