Skip to content

Commit e05df88

Browse files
bin: fix sbom diff
1 parent 0fd21e2 commit e05df88

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

scripts/sbom/sbom.bash

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -620,9 +620,9 @@ sbom_get_charts() {
620620

621621
sbom_get_containers() {
622622
local query
623-
query='.components[] | [.components[] | { "name": .name, "version": .version}] | .[]'
623+
query='.components[] | select(has("components")) | [.components[] | { "name": .name, "version": .version}] | unique_by([.name, .version]) | sort_by([.name, .version]) | .[]'
624624

625-
jq -e --color-output -c "${query}" "${SBOM_FILE}" | sort -u
625+
jq -e -c "${query}" "${SBOM_FILE}"
626626
}
627627

628628
sbom_edit() {
@@ -787,39 +787,29 @@ sbom_generate() {
787787
}
788788

789789
sbom_diff() {
790-
local chart_file chart_name chart_version found_diff location
791-
mapfile -t diff_files < <(git diff --staged --name-only | grep "helmfile.d/")
792-
mapfile -t all_charts < <(sbom_get_charts "${SBOM_FILE}")
790+
local chart_file chart_name chart_version location
791+
mapfile -t all_chart_components < <(sbom_get_charts "${SBOM_FILE}")
793792

794793
should_fail=false
795-
for chart in "${all_charts[@]}"; do
796-
found_diff=false
797-
sbom_component_name=$(jq '.name' <<<"${chart}")
798-
sbom_component_version=$(jq '.version' <<<"${chart}")
799-
location=$(jq '.location' <<<"${chart}")
794+
for chart in "${all_chart_components[@]}"; do
795+
sbom_component_name=$(jq -r '.name' <<<"${chart}")
796+
sbom_component_version=$(jq -r '.version' <<<"${chart}")
797+
location=$(jq -r '.location' <<<"${chart}")
800798
chart_file="${ROOT}/${location}/Chart.yaml"
801799

802-
for diff_file in "${diff_files[@]}"; do
803-
if [[ "${diff_file}" == *${location}* ]]; then
804-
chart_name="$(yq '.name' "${chart_file}")"
805-
chart_version="$(yq '.version' "${chart_file}")"
806-
if [[ "${chart_version}" != "${sbom_component_version}" ]]; then
807-
found_diff=true
808-
log_warning "Chart version \"${chart_version}\" does not match SBOM \"${sbom_component_version}\""
809-
break
810-
elif [[ "${chart_name}" != "${sbom_component_name}" ]]; then
811-
found_diff=true
812-
log_warning "Chart name \"${chart_name}\" does not match SBOM \"${sbom_component_name}\""
813-
break
814-
fi
815-
fi
816-
done
817-
818-
if [[ "${found_diff}" == true ]]; then
819-
should_fail=true
820-
log_warning "Run the following to update the SBOM:"
821-
log_warning "./scripts/sbom/sbom.bash update ${location}"
800+
chart_name="$(yq '.name' "${chart_file}")"
801+
chart_version="$(yq '.version' "${chart_file}")"
802+
if [[ "${chart_version}" != "${sbom_component_version}" ]]; then
803+
log_warning "Chart version \"${chart_version}\" does not match SBOM \"${sbom_component_version}\""
804+
elif [[ "${chart_name}" != "${sbom_component_name}" ]]; then
805+
log_warning "Chart name \"${chart_name}\" does not match SBOM \"${sbom_component_name}\""
806+
else
807+
continue
822808
fi
809+
810+
should_fail=true
811+
log_warning "Run the following to update the SBOM:"
812+
log_warning "./scripts/sbom/sbom.bash update ${location}"
823813
done
824814

825815
if [[ "${should_fail}" == false ]]; then

0 commit comments

Comments
 (0)