From ca6fee127cec4ef5f99bd458b214dbd29ba2ef89 Mon Sep 17 00:00:00 2001 From: Michael Chen Date: Tue, 13 Dec 2022 14:50:02 +0100 Subject: [PATCH] Added argument can now be a list of arguments for list formatting Artifact lines can now be omitted to show entire file --- createreadmes.py | 24 +++++++++++++++++------ security_rules_schema.json | 39 +++++++++++++++----------------------- 2 files changed, 33 insertions(+), 30 deletions(-) diff --git a/createreadmes.py b/createreadmes.py index 63c7528..39964b2 100644 --- a/createreadmes.py +++ b/createreadmes.py @@ -217,11 +217,11 @@ from typing import TypedDict class Artifact(TypedDict): file: str - lines: list[int] + lines: NotRequired[list[int]] class SecurityRule(TypedDict): status: str - argument: str + argument: str | list[str] artifacts: NotRequired[list[Artifact]] rule_schema = yaml.safe_load("""type: object @@ -238,10 +238,17 @@ properties: - not applicable - unknown argument: - type: string + anyOf: + - type: string + - type: array + items: + type: string artifacts: type: array items: + additionalProperties: no + required: + - file type: object properties: file: @@ -300,16 +307,21 @@ def artifact_to_string(info: ModelInformation, artifact: Artifact): file = Path(artifact['file']) filename = file.name file_url = f"https://github.com/{info['slug']}/blob/{info.get('branch', 'master')}/{artifact['file']}" - return f"- {filename}: Line{'s'[:len(artifact['lines'])^1]}: {', '.join(f'[{line}]({file_url}#L{line})' for line in artifact['lines'])}" + lines = artifact.get("lines") + if lines is None: + return f"- {filename}: [File]({file_url})" + return f"- {filename}: Line{'s'[:len(lines)^1]}: {', '.join(f'[{line}]({file_url}#L{line})' for line in lines)}" def rule_to_string(info: ModelInformation, id: int, rule: SecurityRule | None): if rule is None: - # warning(f"Rule {id} is missing!") # TODO Enable warning + warning(f"Rule {id} is missing!") return "" + argument = rule['argument'] + argument = argument if isinstance(argument, str) else "".join(f"\n1. {arg}" for arg in argument) text = f"""#### Rule {id}: {rule_names[id]} -This rule is {rule['status']}: {rule['argument']}""" +This rule is {rule['status']}: {argument}""" artifacts = rule.get("artifacts", []) if len(artifacts) > 0: text = text + f""" diff --git a/security_rules_schema.json b/security_rules_schema.json index f87b95b..e59c538 100644 --- a/security_rules_schema.json +++ b/security_rules_schema.json @@ -16,33 +16,24 @@ ] }, "argument": { - "type": "string" + "anyOf": [ + { "type": "string" }, + { "type": "array", "items": { "type": "string" } } + ] }, "artifacts": { "type": "array", - "items": [ - { - "type": "object", - "additionalProperties": false, - "properties": { - "file": { - "type": "string" - }, - "lines": { - "type": "array", - "items": [ - { - "type": "integer" - } - ] - } - }, - "required": [ - "file", - "lines" - ] - } - ] + "items": { + "type": "object", + "additionalProperties": false, + "properties": { + "file": { "type": "string" }, + "lines": { "type": "array", "items": { "type": "integer" } } + }, + "required": [ + "file" + ] + } } }, "required": [