Skip to content

Conversation

@rn23thakur
Copy link

Hey, I gave it another shot, tried to read and follow the existing patterns. I'm still not sure how to write tests for these, or how to run the pre-existing ones and interpret their results.

Signed-off-by: rn23thakur <[email protected]>
@rn23thakur rn23thakur requested a review from a team as a code owner November 6, 2025 22:32
@jkowalleck
Copy link
Member

@rn23thakur , thank you for all your efforts and your time.

just a note: you dont need to close a pull-request, and restart your changes with another pull request. you could have left it open and improved it.
a pull-request can be a very lively conversation with lots of continuous changes in the scope.
So, instead of closing your PR, you could just have addressed the notes, push another set of changes, and have this reviewed/discussed. (See this as an example CycloneDX/cyclonedx-python-lib#908)

Anyway, I am happy to review your changes.

@jkowalleck jkowalleck requested a review from Copilot November 7, 2025 07:35
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for extracting and populating tags from package metadata keywords across different Python package managers (Poetry, Pipenv, and standard environment).

Key changes:

  • Introduces a new metadata2tags function in packaging.py to parse keywords from package metadata
  • Updates Poetry, Pipenv, and environment builders to populate component tags from package keywords
  • Adds logic to conditionally update component tags when the tags attribute exists

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
cyclonedx_py/_internal/utils/packaging.py Adds metadata2tags function to parse keywords from package metadata into tags
cyclonedx_py/_internal/poetry.py Imports and uses tag parsing for Poetry lock file packages
cyclonedx_py/_internal/pipenv.py Imports and uses tag parsing for Pipenv lock file packages
cyclonedx_py/_internal/environment.py Imports and uses metadata2tags for environment-based components

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

from .cli_common import add_argument_mc_type
from .utils.cdx import make_bom
from .utils.packaging import normalize_packagename
from .utils.packaging import normalize_packagename, to_tags
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

The imported function to_tags does not exist in utils.packaging. The actual function defined in that module is metadata2tags. This will cause an ImportError at runtime.

Suggested change
from .utils.packaging import normalize_packagename, to_tags
from .utils.packaging import normalize_packagename, metadata2tags

Copilot uses AI. Check for mistakes.
)

if hasattr(component, 'tags'):
component.tags.update(to_tags(package.get('keywords')))
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

The function to_tags does not exist. Based on the implementation in utils/packaging.py, this should call metadata2tags. However, metadata2tags expects a PackageMetadata object, not a raw keywords value. A separate function needs to be created that accepts keywords as a string or list, or this code needs to be adjusted to match the actual function signature.

Copilot uses AI. Check for mistakes.
from .utils.args import arparse_split
from .utils.cdx import make_bom
from .utils.packaging import normalize_packagename
from .utils.packaging import normalize_packagename, to_tags
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

The imported function to_tags does not exist in utils.packaging. The actual function defined in that module is metadata2tags. This will cause an ImportError at runtime.

Suggested change
from .utils.packaging import normalize_packagename, to_tags
from .utils.packaging import normalize_packagename, metadata2tags

Copilot uses AI. Check for mistakes.
external_references=self.__make_extrefs(package_name, package_data, source_urls),
)
if hasattr(component, 'tags'):
component.tags.update(to_tags(package_data.get('keywords')))
Copy link

Copilot AI Nov 7, 2025

Choose a reason for hiding this comment

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

The function to_tags does not exist. Based on the implementation in utils/packaging.py, this should call metadata2tags. However, metadata2tags expects a PackageMetadata object, not a raw keywords value. A separate function needs to be created that accepts keywords as a string or list, or this code needs to be adjusted to match the actual function signature.

Copilot uses AI. Check for mistakes.
_KEYWORDS_SPLIT_MATCHER = re_compile(r'[;, ]+')


def metadata2tags(metadata: 'PackageMetadata') -> Generator[str, None, None]:
Copy link
Member

Choose a reason for hiding this comment

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

from ..py_interop.packagemetadata import PackageMetadata


_KEYWORDS_SPLIT_MATCHER = re_compile(r'[;, ]+')
Copy link
Member

Choose a reason for hiding this comment

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

let's discuss: why did you use a regular expression, instead of just splitting on comma(,)?
(I am not saying you are wrong,I just dont understand the solution, as I might not know all the details.)

"""
keywords_string = metadata.get('Keywords', '')
if keywords_string:
yield from (
Copy link
Member

Choose a reason for hiding this comment

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

can this me simplified?
pseudo code:

filter(None, map(str.strip, 
  _KEYWORDS_SPLIT_MATCHER.split(keywords_string)
))

@jkowalleck jkowalleck changed the title retry feat: add unified tags mapping across environment builders Nov 7, 2025
@jkowalleck jkowalleck linked an issue Nov 7, 2025 that may be closed by this pull request
@jkowalleck
Copy link
Member

@rn23thakur

I'm still not sure how to write tests for these, or how to run the pre-existing ones and interpret their results.

this tool works on specific setups, we all them test beds.
you will find the test beds here: https://github.com/CycloneDX/cyclonedx-python/tree/main/tests/_data/infiles
these test beds are used to run the tool on, and the results are stored here: https://github.com/CycloneDX/cyclonedx-python/tree/main/tests/_data/snapshots
You may run the tests as shown here: https://github.com/CycloneDX/cyclonedx-python/tree/main/tests/_data/snapshots#re-creation
step by step instructions:

  1. open your terminal
  2. navigate to the root folder of this project
  3. setup the project as shown here:
    poetry install
  4. run the tests as shown here:
    CDX_TEST_RECREATE_SNAPSHOTS=1 poetry run tox -e py
  5. see if tests are passing, and review the updated SBOM snapshots.

feel free to ask further question.
If you find the existing docs insufficient, I would be happy to see a separate pull request improving the docs.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat: populate component's tags

2 participants