Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add VCS_AUTHORS substituted value #66

Merged
merged 2 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ The supported values are given below:
* `@VCS_COMMIT@` → `git rev-parse HEAD` e.g. `3090e61ee3452c0478860747de057c0269bfb7b6`
* `@VCS_SBOM_AUTHORS@` → `git shortlog HEAD -n -s -- bom.json` e.g. `Example User, Another User`
* `@VCS_SBOM_AUTHOR@` → `@VCS_SBOM_AUTHORS@[0]` e.g. `Example User`
* `@VCS_AUTHORS@` → `git shortlog HEAD -n -s` e.g. `Example User, Another User`
* `@VCS_AUTHOR@` → `@VCS_AUTHORS@[0]` e.g. `Example User`

The only supported source control system is `git`, but patches very welcome for `svn`, `hg` etc.

Expand Down
31 changes: 25 additions & 6 deletions uswid/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,12 +307,21 @@ def _get_vcs_toplevel(filepath: str) -> Optional[str]:
return None


def _get_vcs_file_authors(filepath: str, theshold: int = 10) -> List[str]:
def _get_vcs_authors(
filepath: str, threshold: int = 10, single_file=False
) -> List[str]:

authors: List[str] = []
try:
p = subprocess.run(
["git", "shortlog", "HEAD", "-n", "-s", os.path.basename(filepath)],
[
"git",
"shortlog",
"HEAD",
"-n",
"-s",
os.path.basename(filepath) if single_file else ".",
],
capture_output=True,
cwd=os.path.dirname(filepath),
check=True,
Expand All @@ -328,7 +337,7 @@ def _get_vcs_file_authors(filepath: str, theshold: int = 10) -> List[str]:
for cnt, author in authors_tmp:
total += cnt
for cnt, author in authors_tmp:
if (100 / total) * cnt > theshold:
if (100 / total) * cnt > threshold:
authors.append(author)
except subprocess.CalledProcessError:
pass
Expand Down Expand Up @@ -358,6 +367,8 @@ def _container_merge_from_filepath(
"@VCS_COMMIT@",
"@VCS_SBOM_AUTHORS@",
"@VCS_SBOM_AUTHOR@",
"@VCS_AUTHORS@",
"@VCS_AUTHOR@",
]:
if text.find(key) != -1:
replacements[key] = "NOASSERTION"
Expand All @@ -371,10 +382,18 @@ def _container_merge_from_filepath(
replacements["@VCS_COMMIT@"] = _get_vcs_commit(filepath)
if "@VCS_SBOM_AUTHORS@" in replacements:
replacements["@VCS_SBOM_AUTHORS@"] = ", ".join(
_get_vcs_file_authors(filepath)
_get_vcs_authors(filepath, single_file=True)
)
if "@VCS_SBOM_AUTHOR@" in replacements:
replacements["@VCS_SBOM_AUTHOR@"] = _get_vcs_file_authors(filepath)[0]
replacements["@VCS_SBOM_AUTHOR@"] = _get_vcs_authors(
filepath, single_file=True
)[0]
if "@VCS_AUTHORS@" in replacements:
replacements["@VCS_AUTHORS@"] = ", ".join(
_get_vcs_authors(filepath, threshold=5)
)
if "@VCS_AUTHOR@" in replacements:
replacements["@VCS_AUTHOR@"] = _get_vcs_authors(filepath, threshold=5)[0]

# do substitutions
if replacements:
Expand Down Expand Up @@ -405,7 +424,7 @@ def _container_merge_from_filepath(
)
if not component.get_entity_by_role(uSwidEntityRole.TAG_CREATOR):
entity: uSwidEntity = uSwidEntity(
name=", ".join(_get_vcs_file_authors(filepath)),
name=", ".join(_get_vcs_authors(filepath)),
roles=[uSwidEntityRole.TAG_CREATOR],
)
component.add_entity(entity)
Expand Down
Loading