Commit 0d36fc41 authored by Jose Ivan Vargas's avatar Jose Ivan Vargas

Merge branch 'fix-default-dependency-list-sort-order' into 'master'

Fix default dependency list sort order icon

See merge request gitlab-org/gitlab!65238
parents 8dbca902 c575ebda
...@@ -16,5 +16,5 @@ export default () => ({ ...@@ -16,5 +16,5 @@ export default () => ({
}, },
filter: FILTER.all, filter: FILTER.all,
sortField: 'severity', sortField: 'severity',
sortOrder: SORT_ORDER.ascending, sortOrder: SORT_ORDER.descending,
}); });
...@@ -51,25 +51,33 @@ module Security ...@@ -51,25 +51,33 @@ module Security
end end
def sort(collection) def sort(collection)
default_sort_order = 'asc'
case params[:sort_by] case params[:sort_by]
when 'packager' when 'packager'
collection.sort_by! { |a| a[:packager] } collection.sort_by! { |a| a[:packager] }
when 'severity' when 'severity'
default_sort_order = 'desc'
sort_dependency_vulnerabilities_by_severity!(collection) sort_dependency_vulnerabilities_by_severity!(collection)
sort_dependencies_by_severity!(collection) sort_dependencies_by_severity!(collection)
else else
collection.sort_by! { |a| a[:name] } collection.sort_by! { |a| a[:name] }
end end
collection.reverse! if params[:sort] == 'desc' if params[:sort] && params[:sort] != default_sort_order
collection.reverse!
end
collection collection
end end
def compare_severity_levels(level1, level2) def compare_severity_levels(level1, level2)
# level2 appears before level1 because we want the default sort order to be in descending
# order of severity level, for example "critical, high, medium, low"
::Enums::Vulnerability.severity_levels[level2] <=> ::Enums::Vulnerability.severity_levels[level1] ::Enums::Vulnerability.severity_levels[level2] <=> ::Enums::Vulnerability.severity_levels[level1]
end end
# sort dependency vulnerabilities in descending order by severity level
def sort_dependency_vulnerabilities_by_severity!(collection) def sort_dependency_vulnerabilities_by_severity!(collection)
collection.each do |dependency| collection.each do |dependency|
dependency[:vulnerabilities].sort! do |vulnerability1, vulnerability2| dependency[:vulnerabilities].sort! do |vulnerability1, vulnerability2|
...@@ -78,8 +86,8 @@ module Security ...@@ -78,8 +86,8 @@ module Security
end end
end end
# vulnerabilities are already sorted by severity level so we can assume that first vulnerability in # vulnerabilities are already sorted in descending order by severity level so we can assume that
# vulnerabilities array will have highest severity # first vulnerability in the vulnerabilities array will have the highest severity
def sort_dependencies_by_severity!(collection) def sort_dependencies_by_severity!(collection)
collection.sort! do |dep_i, dep_j| collection.sort! do |dep_i, dep_j|
level_i = dep_i.dig(:vulnerabilities, 0, :severity) || :info level_i = dep_i.dig(:vulnerabilities, 0, :severity) || :info
......
...@@ -93,7 +93,7 @@ exports[`DependenciesActions component matches the snapshot 1`] = ` ...@@ -93,7 +93,7 @@ exports[`DependenciesActions component matches the snapshot 1`] = `
variant="default" variant="default"
> >
<gl-icon-stub <gl-icon-stub
name="sort-lowest" name="sort-highest"
size="16" size="16"
/> />
</gl-button-stub> </gl-button-stub>
......
...@@ -97,38 +97,61 @@ RSpec.describe Security::DependencyListService do ...@@ -97,38 +97,61 @@ RSpec.describe Security::DependencyListService do
end end
end end
# this test ensures the dependency list severity sort order is `info, unknown, low, medium, high, critical` context 'sorted by severity' do
# which is asending severity order, however, the UI label for this sort order is currently `desc`.
# TODO: change the UI label to use `asc` for this sort order and use `desc` for the default sort order
# of `critical, high, medium, low, unknown, info`
# See https://gitlab.com/gitlab-org/gitlab/-/issues/332653
context 'sorted by asc severity' do
let(:params) do let(:params) do
{ {
sort: 'desc',
sort_by: 'severity' sort_by: 'severity'
} }
end end
it 'returns array of data sorted by package severity level in ascending order' do context 'in descending order' do
dependencies = subject.last(2).map do |dependency| before do
{ params[:sort] = 'desc'
name: dependency[:name], end
vulnerabilities: dependency[:vulnerabilities].map do |vulnerability|
vulnerability[:severity] it 'returns array of data sorted by package severity level in descending order' do
end dependencies = subject.first(2).map do |dependency|
} {
name: dependency[:name],
vulnerabilities: dependency[:vulnerabilities].pluck(:severity)
}
end
expect(dependencies).to eq([{ name: "saml2-js", vulnerabilities: %w(critical medium unknown) },
{ name: "nokogiri", vulnerabilities: ["high"] }])
end end
expect(dependencies).to eq([{ name: "nokogiri", vulnerabilities: ["high"] }, it 'returns array of data with package vulnerabilities sorted in descending order' do
{ name: "saml2-js", vulnerabilities: %w(critical medium unknown) }]) saml2js_dependency = subject.find { |dep| dep[:name] == 'saml2-js' }
saml2js_severities = saml2js_dependency[:vulnerabilities].map {|v| v[:severity] }
expect(saml2js_severities).to eq(%w(critical medium unknown))
end
end end
it 'returns array of data with package vulnerabilities sorted in descending order' do context 'in ascending order' do
saml2js_dependency = subject.find { |dep| dep[:name] == 'saml2-js' } before do
saml2js_severities = saml2js_dependency[:vulnerabilities].map {|v| v[:severity] } params[:sort] = 'asc'
end
it 'returns array of data sorted by package severity level in ascending order' do
dependencies = subject.last(2).map do |dependency|
{
name: dependency[:name],
vulnerabilities: dependency[:vulnerabilities].pluck(:severity)
}
end
expect(dependencies).to eq([{ name: "nokogiri", vulnerabilities: ["high"] },
{ name: "saml2-js", vulnerabilities: %w(critical medium unknown) }])
end
it 'returns array of data with package vulnerabilities sorted in descending order' do
saml2js_dependency = subject.find { |dep| dep[:name] == 'saml2-js' }
saml2js_severities = saml2js_dependency[:vulnerabilities].map {|v| v[:severity] }
expect(saml2js_severities).to eq(%w(critical medium unknown)) expect(saml2js_severities).to eq(%w(critical medium unknown))
end
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