Skip to content

Commit

Permalink
Merge pull request #1702 from camptocamp/security-md
Browse files Browse the repository at this point in the history
Add `Version` column found at index 0.
  • Loading branch information
sbrunner authored May 31, 2024
2 parents c3c1f0d + ef3918a commit 8a70e5d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 4 deletions.
56 changes: 55 additions & 1 deletion c2cciutils/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import markdown
from markdown.extensions.tables import TableExtension

HEADER_VERSION = "Version"
HEADER_ALTERNATE_TAG = "Alternate Tag"
HEADER_SUPPORT_UNTIL = "Supported Until"
SUPPORT_TO_BE_DEFINED = "To be defined"
SUPPORT_BEST_EFFORT = "Best effort"
SUPPORT_UNSUPPORTED = "Unsupported"


class Security:
"""
Expand All @@ -18,12 +25,13 @@ class Security:
data: list[list[str]]
_row: Optional[list[str]] = None

def __init__(self, status: str):
def __init__(self, status: str, check: bool = True):
"""
Initialize.
Arguments:
status: the content of the SECURITY.md file.
check: Set to `False` to skip the check.
"""

self.headers = []
Expand All @@ -40,6 +48,52 @@ def __init__(self, status: str):
for row in self.data:
row.append("")

self.version_index = self.headers.index(HEADER_VERSION) if HEADER_VERSION in self.headers else -1
self.alternate_tag_index = (
self.headers.index(HEADER_ALTERNATE_TAG) if HEADER_ALTERNATE_TAG in self.headers else -1
)
self.support_until_index = (
self.headers.index(HEADER_SUPPORT_UNTIL) if HEADER_SUPPORT_UNTIL in self.headers else -1
)

if check:
if not self.check(verbose=0):
raise ValueError("SECURITY.md file is not valid.")

def check(self, verbose: int = -1) -> bool:
"""
Check the content.
Arguments:
verbose: the verbosity level, `-1` for no output, `0` for errors only, `1` for all.
Return:
`True` if the content is valid, `False` otherwise.
"""

success = True
if self.version_index == -1:
if verbose >= 0:
print("`Version` column not found.")
success = False
elif verbose >= 1:
print(f"`Version` column found at index {self.version_index}.")

if self.alternate_tag_index == -1:
if verbose >= 1:
print("Optional `Alternate Tag` column not found.")
elif verbose >= 1:
print(f"`Alternate Tag` column found at index {self.alternate_tag_index}.")

if self.support_until_index == -1:
if verbose >= 0:
print("`Support Until` column not found.")
success = False
elif verbose >= 1:
print(f"`Support Until` column found at index {self.support_until_index}.")

return success

def _pe(self, elem: xml.etree.ElementTree.Element) -> None:
"""
Parse the HTML table.
Expand Down
7 changes: 4 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@ exclude = ["c2cciutils/node_modules/**/test"]
[tool.poetry.scripts]
c2cciutils = "c2cciutils.scripts.main:main"
c2cciutils-env = "c2cciutils.scripts.env:main"
c2cciutils-checks = "c2cciutils.scripts.env:main"
c2cciutils-pull-request-checks = "c2cciutils.scripts.pr_checks:main"
c2cciutils-audit = "c2cciutils.scripts.audit:main"
c2cciutils-publish = "c2cciutils.scripts.publish:main"
c2cciutils-version = "c2cciutils.scripts.version:main"
c2cciutils-clean = "c2cciutils.scripts.clean:main"
c2cciutils-security-md = "c2cciutils.scripts.security_md:main"
c2cciutils-checks = "c2cciutils.scripts.env:main"
c2cciutils-pull-request-checks = "c2cciutils.scripts.pr_checks:main"
c2cciutils-audit = "c2cciutils.scripts.audit:main"
c2cciutils-google-calendar = "c2cciutils.publish:main_calendar"
c2cciutils-k8s-install = "c2cciutils.scripts.k8s.install:main"
c2cciutils-k8s-db = "c2cciutils.scripts.k8s.db:main"
Expand Down

0 comments on commit 8a70e5d

Please sign in to comment.