Commit 56a56dd5 authored by Stan Hu's avatar Stan Hu

Merge branch 'rf-sort-vulnerability-history' into 'master'

Group Security Dashboard: Sort all vulnerability history keys

See merge request gitlab-org/gitlab-ee!15301
parents d5779d5d b6e64050
...@@ -32,14 +32,19 @@ module Gitlab ...@@ -32,14 +32,19 @@ module Gitlab
project_ids_to_fetch.each do |project_id| project_ids_to_fetch.each do |project_id|
project_history = Gitlab::Vulnerabilities::HistoryCache.new(group, project_id).fetch(HISTORY_RANGE) project_history = Gitlab::Vulnerabilities::HistoryCache.new(group, project_id).fetch(HISTORY_RANGE)
history_keys = ::Vulnerabilities::Occurrence::SEVERITY_LEVELS.keys.map(&:to_sym) history.each do |key, value|
history_keys << :total value.merge!(project_history[key]) { |k, aggregate, project_count| aggregate + project_count }
history_keys.each do |key|
history[key].merge!(project_history[key]) { |k, aggregate, project_count| aggregate + project_count }
end end
end end
history[:total] = history[:total].sort_by { |date, count| date }.to_h sort_by_date_for_each_key(history)
end
def sort_by_date_for_each_key(history)
history.each do |key, value|
history[key] = value.sort_by { |date, count| date }.to_h
end
history history
end end
......
...@@ -28,8 +28,10 @@ describe Gitlab::Vulnerabilities::History do ...@@ -28,8 +28,10 @@ describe Gitlab::Vulnerabilities::History do
end end
it 'returns the proper format for the history' do it 'returns the proper format for the history' do
expect(counter[:total]).to eq({ Date.today => 3 }) Timecop.freeze do
expect(counter[:high]).to eq({ Date.today => 2 }) expect(counter[:total]).to eq({ Date.today => 3 })
expect(counter[:high]).to eq({ Date.today => 2 })
end
end end
end end
...@@ -55,8 +57,27 @@ describe Gitlab::Vulnerabilities::History do ...@@ -55,8 +57,27 @@ describe Gitlab::Vulnerabilities::History do
end end
it 'returns the proper format for the history' do it 'returns the proper format for the history' do
expect(counter[:total]).to eq({ Date.today => 3 }) Timecop.freeze do
expect(counter[:high]).to eq({ Date.today => 2 }) expect(counter[:total]).to eq({ Date.today => 3 })
expect(counter[:high]).to eq({ Date.today => 2 })
end
end
context 'multiple projects with vulnerabilities' do
before do
Timecop.freeze(Date.today - 1) do
create_vulnerabilities(1, project1, { severity: :high })
end
Timecop.freeze(Date.today - 4) do
create_vulnerabilities(1, project2, { severity: :high })
end
end
it 'sorts by date for each key' do
Timecop.freeze do
expect(counter[:high].keys).to eq([(Date.today - 4), (Date.today - 1), Date.today])
end
end
end end
end end
...@@ -64,7 +85,8 @@ describe Gitlab::Vulnerabilities::History do ...@@ -64,7 +85,8 @@ describe Gitlab::Vulnerabilities::History do
report_type = options[:report_type] || :sast report_type = options[:report_type] || :sast
severity = options[:severity] || :high severity = options[:severity] || :high
pipeline = create(:ci_pipeline, :success, project: project) pipeline = create(:ci_pipeline, :success, project: project)
create_list(:vulnerabilities_occurrence, count, report_type: report_type, severity: severity, pipelines: [pipeline], project: project) created_at = options[:created_at] || Date.today
create_list(:vulnerabilities_occurrence, count, report_type: report_type, severity: severity, pipelines: [pipeline], project: project, created_at: created_at)
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