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

fix: remove plan from report if comment is too long #35

Merged
merged 16 commits into from
May 21, 2024
Merged
8 changes: 4 additions & 4 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ runs:
steps:
- uses: actions/setup-python@v5
with:
python-version: '3.12'
python-version: "3.12"

- name: Setup tfenv
if: ${{ inputs.terraform_version != 'system' }}
Expand Down Expand Up @@ -86,10 +86,10 @@ runs:
fi

if [ -z "${plan_args[$i]}" ] ; then
venv/bin/python "${{ github.action_path }}/tfcheck.py" "${directories[$i]}" -report report.md $hide | tee >(tail -1 >> result.txt)
venv/bin/python "${{ github.action_path }}/tfcheck.py" "${directories[$i]}" -report report.md -full-report full_report.md $hide | tee >(tail -1 >> result.txt)
else
printf "Passing these args to plan: %s\n" "${plan_args[$i]}"
venv/bin/python "${{ github.action_path }}/tfcheck.py" "${directories[$i]}" -report report.md -plan-args "${plan_args[$i]}" $hide | tee >(tail -1 >> result.txt)
venv/bin/python "${{ github.action_path }}/tfcheck.py" "${directories[$i]}" -report report.md -full-report full_report.md -plan-args "${plan_args[$i]}" $hide | tee >(tail -1 >> result.txt)
fi
ret=${PIPESTATUS[0]}

Expand Down Expand Up @@ -127,7 +127,7 @@ runs:
echo "$(cat report.md)" >> $GITHUB_OUTPUT
echo "$delimiter" >> $GITHUB_OUTPUT

echo "$(cat report.md)" >> $GITHUB_STEP_SUMMARY
echo "$(cat full_report.md)" >> $GITHUB_STEP_SUMMARY
shell: bash

- uses: actions/github-script@v7
Expand Down
51 changes: 38 additions & 13 deletions tfcheck.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,19 @@
)
parser.add_argument(
"-report",
help="print detailed report to a file",
help="print report to a file, removing plans if report length > 65336",
type=argparse.FileType("a", encoding="UTF-8"),
)
parser.add_argument(
"-hide-refresh",
help="hide terraform state refresh output from report",
action=argparse.BooleanOptionalAction,
)
parser.add_argument(
"-full-report",
help="print full report to a file",
type=argparse.FileType("a", encoding="UTF-8"),
)

env = Environment(loader=FileSystemLoader(f"{os.path.dirname(__file__)}/templates"))
template = env.get_template("tfcheck.md")
Expand Down Expand Up @@ -135,6 +140,19 @@ def check(path: str, plan_args: Optional[str] = None) -> CheckResult:
)


def remove_plan(report: str) -> str:
report_lines = []
add_line = True
for line in report.splitlines():
if add_line and line == "<details><summary>Show Plan</summary>":
add_line = False
if add_line:
report_lines.append(line)
if not add_line and line == "</details>":
add_line = True
return "\n".join(report_lines)


if __name__ == "__main__":
args = parser.parse_args()

Expand All @@ -156,18 +174,25 @@ def check(path: str, plan_args: Optional[str] = None) -> CheckResult:
)
)

if args.report:
args.report.write(
template.render(
path=result.path,
init_result=result.init_result(),
check_result=result.check_result_msg(),
fmt_result=result.fmt_result(),
validate_result=result.validate_result(),
plan_result=result.plan_result(),
plan_output=result.plan_output,
plan_msg=result.plan_msg(),
)
if args.report or args.full_report:
report = template.render(
path=result.path,
init_result=result.init_result(),
check_result=result.check_result_msg(),
fmt_result=result.fmt_result(),
validate_result=result.validate_result(),
plan_result=result.plan_result(),
plan_output=result.plan_output,
plan_msg=result.plan_msg(),
)

if args.full_report:
args.full_report.write(report)

if args.report:
if len(report) > 65536:
report = remove_plan(report)

args.report.write(report)

sys.exit(result.exitcode())