Skip to content

fix(RELEASE-2685): treat empty cpe label as validation failure - #1001

Open
davidmogar wants to merge 1 commit into
mainfrom
release2685
Open

fix(RELEASE-2685): treat empty cpe label as validation failure#1001
davidmogar wants to merge 1 commit into
mainfrom
release2685

Conversation

@davidmogar

Copy link
Copy Markdown
Contributor

check-labels treated a completely unset cpe label and a present-but-empty cpe label identically, silently skipping enforcement for both. this hid cases where a Containerfile sets LABEL cpe=${CPE} but forgets to declare ARG CPE, leaving the label present but empty.

get_label_value now returns None only when the label key is missing, and "" when it is present but empty, so callers can tell the two cases apart. an empty cpe label now fails validation under --enforce (or warns otherwise), while a truly unset label is still skipped as before.

Assisted-by: Claude

check-labels treated a completely unset cpe label and a
present-but-empty cpe label identically, silently skipping
enforcement for both. this hid cases where a Containerfile sets
LABEL cpe=${CPE} but forgets to declare ARG CPE, leaving the
label present but empty.

get_label_value now returns None only when the label key is
missing, and "" when it is present but empty, so callers can
tell the two cases apart. an empty cpe label now fails
validation under --enforce (or warns otherwise), while a truly
unset label is still skipped as before.

Assisted-by: Claude
Signed-off-by: David Moreno García <damoreno@redhat.com>
@qodo-app-for-konflux-ci

Copy link
Copy Markdown

PR Summary by Qodo

Treat empty CPE labels as validation failures

🐞 Bug fix 🧪 Tests 🕐 10-20 Minutes

Grey Divider

AI Description

• Distinguishes missing CPE labels from labels containing empty or whitespace-only values.
• Fails enforced validation for empty CPE labels while warning in non-enforced mode.
• Adds coverage for label extraction and both empty-label validation modes.
Diagram

graph TD
  A["Component labels"] --> B["Extract CPE"] --> C{"Label state"} -->|Value| H["Compare expected"]
  C -->|Missing| D["Skip enforcement"]
  C -->|Empty| E{"Enforce mode"} -->|Yes| F["Raise failure"]
  E -->|No| G["Log warning"]
Loading
High-Level Assessment

The current approach is appropriate: preserving None as the missing-key sentinel and using an empty string for present-but-empty values fixes the ambiguity with minimal API disruption. Introducing a new result type or separate label-presence lookup would add complexity without improving this localized validation flow.

Files changed (2) +81 / -4

Bug fix (1) +16 / -4
check_labels.pyDifferentiate missing and empty CPE labels +16/-4

Differentiate missing and empty CPE labels

• Changes label extraction to return None only when the key is absent and a normalized empty string for empty or whitespace-only values. Empty CPE labels now raise a validation error under enforcement or emit a warning otherwise, while missing labels remain skipped.

src/tasks/managed/check_labels/check_labels.py

Tests (1) +65 / -0
test_check_labels.pyCover empty-label extraction and CPE validation +65/-0

Cover empty-label extraction and CPE validation

• Adds extraction tests for empty and whitespace-only label values. Adds validation tests confirming empty CPE labels fail in enforced mode and warn in non-enforced mode.

src/tasks/managed/check_labels/tests/test_check_labels.py

@qodo-app-for-konflux-ci

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (1) 📘 Rule violations (1) 📜 Skill insights (0)

Grey Divider


Action required

1. Whitespace bypasses label validation 🐞 Bug ≡ Correctness
Description
get_label_value strips surrounding whitespace from every non-empty label before validation, so a
stored name or cpe value such as " expected " incorrectly matches "expected". This allows
malformed image metadata to pass enforce-mode validation.
Code

src/tasks/managed/check_labels/check_labels.py[58]

+            return str(val).strip() if val is not None else ""
Relevance

●●● Strong

Exact validation comparisons should preserve whitespace; trimming allows malformed metadata to pass
enforce-mode checks.

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
The changed helper strips the value before returning it, while both validators compare that
normalized result directly against their expected values. The snapshot producer serializes inspected
label values without trimming, proving whitespace is part of the source metadata and would otherwise
reach these checks unchanged.

src/tasks/managed/check_labels/check_labels.py[57-58]
src/tasks/managed/check_labels/check_labels.py[112-118]
src/tasks/managed/check_labels/check_labels.py[144-152]
src/tasks/managed/apply_mapping/apply_mapping.py[449-452]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
`get_label_value` must distinguish missing and empty labels without altering non-empty values. Its new unconditional `strip()` causes labels containing surrounding whitespace to pass exact-equality validation.

## Issue Context
Return `""` when a value is empty or whitespace-only, but return the original string representation when it contains non-whitespace characters. Add tests proving whitespace-padded `name` and `cpe` labels fail or warn rather than matching normalized expected values.

## Fix Focus Areas
- src/tasks/managed/check_labels/check_labels.py[46-59]
- src/tasks/managed/check_labels/tests/test_check_labels.py[90-120]
- src/tasks/managed/check_labels/tests/test_check_labels.py[144-260]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

2. Unit tests not co-located 📘 Rule violation ▣ Testability
Description
The new check_labels unit tests are placed in a nested tests/ directory rather than alongside
check_labels.py. This violates the required test placement for source files outside utils/.
Code

src/tasks/managed/check_labels/tests/test_check_labels.py[R210-211]

+def test_empty_cpe_label_enforce(tmp_path: Path) -> None:
+    """Fail when the CPE label is present but set to an empty string."""
Relevance

●●● Strong

Recent managed-task PRs consistently place tests beside source files, supporting this explicit
repository rule.

PR-#939
PR-#928
PR-#935

ⓘ Recommendations generated based on similar findings in past PRs

Evidence
Rule 909 requires tests for non-utils source files to reside in the source file's directory. The
added tests are in src/tasks/managed/check_labels/tests/test_check_labels.py, while the tested
implementation is src/tasks/managed/check_labels/check_labels.py.

Rule 909: Co-locate unit tests with source files, except shared utils tests in utils/tests
src/tasks/managed/check_labels/tests/test_check_labels.py[210-260]
src/tasks/managed/check_labels/check_labels.py[46-58]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The newly added unit tests are under a nested `tests/` directory instead of being co-located with `check_labels.py`.

## Issue Context
PR Compliance ID 909 requires unit tests for source files outside `utils/` to reside in the same directory as their source file. Move or consolidate the tests into a co-located test module while preserving pytest discovery and imports.

## Fix Focus Areas
- src/tasks/managed/check_labels/tests/test_check_labels.py[111-260]
- src/tasks/managed/check_labels/check_labels.py[46-58]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Context sources
✅ Compliance rules (platform): 23 rules

Grey Divider

Tip of the day
💡 Did you know, you can turn on the rule miner and Qodo learns your standards from review history

More tips ↗ | Customize Qodo ↗ | Qodo docs ↗

Grey Divider

Qodo Logo

Comment on lines +210 to +211
def test_empty_cpe_label_enforce(tmp_path: Path) -> None:
"""Fail when the CPE label is present but set to an empty string."""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remediation recommended

1. Unit tests not co-located 📘 Rule violation ▣ Testability

The new check_labels unit tests are placed in a nested tests/ directory rather than alongside
check_labels.py. This violates the required test placement for source files outside utils/.
Agent Prompt
## Issue description
The newly added unit tests are under a nested `tests/` directory instead of being co-located with `check_labels.py`.

## Issue Context
PR Compliance ID 909 requires unit tests for source files outside `utils/` to reside in the same directory as their source file. Move or consolidate the tests into a co-located test module while preserving pytest discovery and imports.

## Fix Focus Areas
- src/tasks/managed/check_labels/tests/test_check_labels.py[111-260]
- src/tasks/managed/check_labels/check_labels.py[46-58]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

val = label.get("value")
if val is not None and str(val).strip():
return str(val)
return str(val).strip() if val is not None else ""

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Action required

2. Whitespace bypasses label validation 🐞 Bug ≡ Correctness

get_label_value strips surrounding whitespace from every non-empty label before validation, so a
stored name or cpe value such as " expected " incorrectly matches "expected". This allows
malformed image metadata to pass enforce-mode validation.
Agent Prompt
## Issue description
`get_label_value` must distinguish missing and empty labels without altering non-empty values. Its new unconditional `strip()` causes labels containing surrounding whitespace to pass exact-equality validation.

## Issue Context
Return `""` when a value is empty or whitespace-only, but return the original string representation when it contains non-whitespace characters. Add tests proving whitespace-padded `name` and `cpe` labels fail or warn rather than matching normalized expected values.

## Fix Focus Areas
- src/tasks/managed/check_labels/check_labels.py[46-59]
- src/tasks/managed/check_labels/tests/test_check_labels.py[90-120]
- src/tasks/managed/check_labels/tests/test_check_labels.py[144-260]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.24%. Comparing base (56ff579) to head (9b2801f).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main    #1001   +/-   ##
=======================================
  Coverage   97.24%   97.24%           
=======================================
  Files         205      205           
  Lines       12808    12813    +5     
=======================================
+ Hits        12455    12460    +5     
  Misses        353      353           
Flag Coverage Δ
unit-tests 97.24% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/tasks/managed/check_labels/check_labels.py 100.00% <100.00%> (ø)

Continue to review full report in Codecov by Harness.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 56ff579...9b2801f. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants