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

Feature/output limit #130

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion gatorgrade/input/parse_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pathlib import Path

import typer

from gatorgrade.input.command_line_generator import generate_checks
from gatorgrade.input.in_file_path import parse_yaml_file
from gatorgrade.input.in_file_path import reformat_yaml_data
Expand All @@ -12,6 +14,7 @@ def parse_config(file: Path):

Args:
file: Yaml file containing gatorgrade and shell command checks
specified_checks: List of specific checks to run
Returns:
Returns a dictionary that specifies shell commands and gatorgrade commands
"""
Expand All @@ -24,7 +27,9 @@ def parse_config(file: Path):
# use it to generate all of the checks;
# these will be valid checks that are now
# ready for execution with this tool
parse_con = generate_checks(reformat_yaml_data(parsed_yaml_file))
reformatted_yaml_data = reformat_yaml_data(parsed_yaml_file)
# Filter the reformat_yaml_data to only include specified checks
parse_con = generate_checks(reformatted_yaml_data)
return parse_con
# return an empty list because of the fact that the
# parsing process did not return a list with content;
Expand Down
9 changes: 8 additions & 1 deletion gatorgrade/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import sys
from pathlib import Path
from typing import Optional
from typing import Tuple

import typer
Expand Down Expand Up @@ -45,6 +46,12 @@ def gatorgrade(
3. the name of the file or environment variable\
4. use 'env md GITHUB_STEP_SUMMARY' to create GitHub job summary in GitHub Action",
),
output_limit: int = typer.Option(
None,
"--output-limit",
"-l",
help="The maximum number of characters to store in an environment variable. Example: '--output-limit 1000'",
),
):
"""Run the GatorGrader checks in the specified gatorgrade.yml file."""
# if ctx.subcommand is None then this means
Expand All @@ -55,7 +62,7 @@ def gatorgrade(
# there are valid checks and thus the
# tool should run them with run_checks
if len(checks) > 0:
checks_status = run_checks(checks, report)
checks_status = run_checks(checks, report, output_limit)
# no checks were created and this means
# that, most likely, the file was not
# valid and thus the tool cannot run checks
Expand Down
67 changes: 57 additions & 10 deletions gatorgrade/output/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ def create_markdown_report_file(json: dict) -> str:
# split checks into passing and not passing
for check in json.get("checks"):
# if the check is passing
if check["status"] == True:
if check["status"]:
passing_checks.append(check)
# if the check is failing
else:
Expand Down Expand Up @@ -209,8 +209,37 @@ def create_markdown_report_file(json: dict) -> str:
return markdown_contents


def truncate_report(report_output_data_json: dict, output_limit: int = None) -> str:
"""Truncate the json report to the maximum number of characters allowed.

Args:
report_output_data_json: the json dictionary that will be used or converted to md
output_limit: the maximum number of characters to display in the output
"""
if len(report_output_data_json) < output_limit:
return json.dumps(report_output_data_json)

for check in report_output_data_json["checks"]:
check["description"] = check["description"][:50]
check["path"] = check["path"][:50]
if "diagnostic" in check:
check["diagnostic"] = check["diagnostic"][:100]

# Convert the truncated report back to JSON string
truncated_report = json.dumps(report_output_data_json)

# Ensure the length does not exceed the maximum allowed length
if output_limit is not None:
if len(truncated_report) > output_limit:
truncated_report = truncated_report[: output_limit - 3] + "..."

return truncated_report


def configure_report(
report_params: Tuple[str, str, str], report_output_data_json: dict
report_params: Tuple[str, str, str],
report_output_data_json: dict,
output_limit: int = None,
):
"""Put together the contents of the report depending on the inputs of the user.

Expand All @@ -220,6 +249,7 @@ def configure_report(
report_params[1]: json or md
report_params[2]: name of the file or env
report_output_data: the json dictionary that will be used or converted to md
output_limit: the maximum number of characters to display in the output
"""
report_format = report_params[0]
report_type = report_params[1]
Expand All @@ -243,16 +273,29 @@ def configure_report(
elif report_format == "env":
if report_name == "GITHUB_STEP_SUMMARY":
env_file = os.getenv("GITHUB_STEP_SUMMARY")
if report_type == "md":
write_json_or_md_file(env_file, report_type, report_output_data_md)
if env_file: # Check if env_file is not None
if report_type == "md":
write_json_or_md_file(env_file, report_type, report_output_data_md)
else:
write_json_or_md_file(
env_file, report_type, report_output_data_json
)
else:
write_json_or_md_file(env_file, report_type, report_output_data_json)
print(
"Environment variable 'GITHUB_STEP_SUMMARY' is not set or is empty."
)

# Add json report into the GITHUB_ENV environment variable for data collection purpose
env_file = os.getenv("GITHUB_ENV")
with open(env_file, "a") as myfile:
myfile.write(f"JSON_REPORT={json.dumps(report_output_data_json)}")
# Add env
if env_file: # Check if env_file is not None
truncated_json_report = truncate_report(
report_output_data_json, output_limit
)
with open(env_file, "a", encoding="utf-8") as myfile:
myfile.write(f"JSON_REPORT={truncated_json_report}")
else:
print("Environment variable 'GITHUB_ENV' is not set or is empty.")

else:
raise ValueError(
"\n[red]The first argument of report has to be 'env' or 'file' "
Expand All @@ -278,7 +321,9 @@ def write_json_or_md_file(file_name, content_type, content):


def run_checks(
checks: List[Union[ShellCheck, GatorGraderCheck]], report: Tuple[str, str, str]
checks: List[Union[ShellCheck, GatorGraderCheck]],
report: Tuple[str, str, str],
output_limit: int = None,
) -> bool:
"""Run shell and GatorGrader checks and display whether each has passed or failed.

Expand All @@ -287,6 +332,8 @@ def run_checks(

Args:
checks: The list of shell and GatorGrader checks to run.
output_limit: The maximum number of characters to display in the output.
report: The details of what the user wants the report to look like.
"""
results = []
# run each of the checks
Expand Down Expand Up @@ -325,7 +372,7 @@ def run_checks(
# if the report is wanted, create output in line with their specifications
if all(report):
report_output_data = create_report_json(passed_count, results, percent)
configure_report(report, report_output_data)
configure_report(report, report_output_data, output_limit)

# compute summary results and display them in the console
summary = f"Passed {passed_count}/{len(results)} ({percent}%) of checks for {Path.cwd().name}!"
Expand Down
Loading