From 659fd77894ee06efb004d77806bbddf95c09cfcf Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Fri, 17 May 2024 15:19:38 +0900 Subject: [PATCH 01/16] Remove plan if report is too long --- tfcheck.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/tfcheck.py b/tfcheck.py index a9d5036..670d982 100644 --- a/tfcheck.py +++ b/tfcheck.py @@ -135,6 +135,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 == "
Show Plan": + add_line = False + if add_line: + report_lines.append(line) + if not add_line and line == "
": + add_line = True + return "\n".join(report_lines) + + if __name__ == "__main__": args = parser.parse_args() @@ -157,17 +170,19 @@ 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(), - ) + 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 len(report) > 65536: + report = remove_plan(report) + + args.report.write(report) sys.exit(result.exitcode()) From afaa75bbc5cde531724c16eccc338640592fc93d Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Fri, 17 May 2024 16:56:36 +0900 Subject: [PATCH 02/16] Keep full report for job summary --- action.yml | 6 +++--- tfcheck.py | 19 ++++++++++++++----- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/action.yml b/action.yml index 438f217..972425c 100644 --- a/action.yml +++ b/action.yml @@ -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' }} @@ -89,7 +89,7 @@ runs: venv/bin/python "${{ github.action_path }}/tfcheck.py" "${directories[$i]}" -report 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]} @@ -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 diff --git a/tfcheck.py b/tfcheck.py index 670d982..80e1ce9 100644 --- a/tfcheck.py +++ b/tfcheck.py @@ -19,7 +19,7 @@ ) 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( @@ -27,6 +27,11 @@ 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") @@ -169,8 +174,8 @@ def remove_plan(report: str) -> str: ) ) - if args.report: - report = template.render( + if args.report or args.full_report: + full_report = template.render( path=result.path, init_result=result.init_result(), check_result=result.check_result_msg(), @@ -180,8 +185,12 @@ def remove_plan(report: str) -> str: plan_output=result.plan_output, plan_msg=result.plan_msg(), ) - if len(report) > 65536: - report = remove_plan(report) + + if args.full_report: + args.full_report.write(full_report) + + if len(full_report) > 65536: + report = remove_plan(full_report) args.report.write(report) From 2c365d737422bbb422d942056672beb8f8f26282 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Fri, 17 May 2024 16:58:39 +0900 Subject: [PATCH 03/16] Set report as full report by default --- tfcheck.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tfcheck.py b/tfcheck.py index 80e1ce9..aa0b40f 100644 --- a/tfcheck.py +++ b/tfcheck.py @@ -189,6 +189,7 @@ def remove_plan(report: str) -> str: if args.full_report: args.full_report.write(full_report) + report = full_report if len(full_report) > 65536: report = remove_plan(full_report) From f60111546eb66de343dc0ce77aa4a8b6a9ec369c Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Fri, 17 May 2024 16:59:31 +0900 Subject: [PATCH 04/16] Naming --- tfcheck.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tfcheck.py b/tfcheck.py index aa0b40f..742dd59 100644 --- a/tfcheck.py +++ b/tfcheck.py @@ -175,7 +175,7 @@ def remove_plan(report: str) -> str: ) if args.report or args.full_report: - full_report = template.render( + report = template.render( path=result.path, init_result=result.init_result(), check_result=result.check_result_msg(), @@ -187,11 +187,10 @@ def remove_plan(report: str) -> str: ) if args.full_report: - args.full_report.write(full_report) + args.full_report.write(report) - report = full_report - if len(full_report) > 65536: - report = remove_plan(full_report) + if len(report) > 65536: + report = remove_plan(report) args.report.write(report) From d4dcdcbe337dfd2e77a7e707479e351e0496a00b Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Fri, 17 May 2024 17:02:17 +0900 Subject: [PATCH 05/16] Fix logic --- tfcheck.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tfcheck.py b/tfcheck.py index 742dd59..286574d 100644 --- a/tfcheck.py +++ b/tfcheck.py @@ -189,9 +189,10 @@ def remove_plan(report: str) -> str: if args.full_report: args.full_report.write(report) - if len(report) > 65536: - report = remove_plan(report) + if args.report: + if len(report) > 65536: + report = remove_plan(report) - args.report.write(report) + args.report.write(report) sys.exit(result.exitcode()) From a4a51e84f770f55705b362340677b99348399642 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Fri, 17 May 2024 17:17:50 +0900 Subject: [PATCH 06/16] Missed -full-report arg --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 972425c..e4feafc 100644 --- a/action.yml +++ b/action.yml @@ -86,7 +86,7 @@ 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 -full-report full_report.md -plan-args "${plan_args[$i]}" $hide | tee >(tail -1 >> result.txt) From db9a62c0bb5fe7e1681ca86f2cfbb254e6aa9cdd Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Mon, 20 May 2024 17:57:28 +0900 Subject: [PATCH 07/16] Remove plans from the overall report file --- action.yml | 10 +++++---- report_postprocess.py | 33 ++++++++++++++++++++++++++++ tfcheck.py | 51 +++++++++++-------------------------------- 3 files changed, 52 insertions(+), 42 deletions(-) create mode 100644 report_postprocess.py diff --git a/action.yml b/action.yml index e4feafc..8c7fb30 100644 --- a/action.yml +++ b/action.yml @@ -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 -full-report full_report.md $hide | tee >(tail -1 >> result.txt) + venv/bin/python "${{ github.action_path }}/tfcheck.py" "${directories[$i]}" -report 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 -full-report full_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 -plan-args "${plan_args[$i]}" $hide | tee >(tail -1 >> result.txt) fi ret=${PIPESTATUS[0]} @@ -117,6 +117,10 @@ runs: - id: result run: | + echo "$(cat report.md)" >> $GITHUB_STEP_SUMMARY + + venv/bin/python "${{ github.action_path }}/report_postprocess.py" -report report.md + delimiter="$(openssl rand -hex 8)" echo "result<<$delimiter" >> $GITHUB_OUTPUT @@ -126,8 +130,6 @@ runs: echo "report<<$delimiter" >> $GITHUB_OUTPUT echo "$(cat report.md)" >> $GITHUB_OUTPUT echo "$delimiter" >> $GITHUB_OUTPUT - - echo "$(cat full_report.md)" >> $GITHUB_STEP_SUMMARY shell: bash - uses: actions/github-script@v7 diff --git a/report_postprocess.py b/report_postprocess.py new file mode 100644 index 0000000..4bcc6da --- /dev/null +++ b/report_postprocess.py @@ -0,0 +1,33 @@ +import argparse + +parser = argparse.ArgumentParser() +parser.add_argument( + "report", + help="Path to report file in markdown format", + type=argparse.FileType("r+", encoding="UTF-8"), +) + + +def remove_plan(report: str) -> str: + report_lines = [] + add_line = True + for line in report.splitlines(): + if add_line and line == "
Show Plan": + add_line = False + if add_line: + report_lines.append(line) + if not add_line and line == "
": + add_line = True + return "\n".join(report_lines) + + +if __name__ == "__main__": + args = parser.parse_args() + + report = args.report.read() + if len(report) > 65536: + report = remove_plan(report) + + args.report.seek(0) + args.report.truncate() + args.report.write(report) diff --git a/tfcheck.py b/tfcheck.py index 286574d..a9d5036 100644 --- a/tfcheck.py +++ b/tfcheck.py @@ -19,7 +19,7 @@ ) parser.add_argument( "-report", - help="print report to a file, removing plans if report length > 65336", + help="print detailed report to a file", type=argparse.FileType("a", encoding="UTF-8"), ) parser.add_argument( @@ -27,11 +27,6 @@ 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") @@ -140,19 +135,6 @@ 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 == "
Show Plan": - add_line = False - if add_line: - report_lines.append(line) - if not add_line and line == "
": - add_line = True - return "\n".join(report_lines) - - if __name__ == "__main__": args = parser.parse_args() @@ -174,25 +156,18 @@ def remove_plan(report: str) -> str: ) ) - 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.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.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()) From f1203a5807a3eb3f782573368e603ba79f358b52 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Mon, 20 May 2024 18:05:36 +0900 Subject: [PATCH 08/16] Fix script call --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 8c7fb30..5347d4a 100644 --- a/action.yml +++ b/action.yml @@ -119,7 +119,7 @@ runs: run: | echo "$(cat report.md)" >> $GITHUB_STEP_SUMMARY - venv/bin/python "${{ github.action_path }}/report_postprocess.py" -report report.md + venv/bin/python "${{ github.action_path }}/report_postprocess.py" report.md delimiter="$(openssl rand -hex 8)" From c78a0507aa8160d3750ca67ef98ec504ddec2496 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Mon, 20 May 2024 18:14:07 +0900 Subject: [PATCH 09/16] Add instruction to check the full report --- report_postprocess.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/report_postprocess.py b/report_postprocess.py index 4bcc6da..3109c59 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -14,6 +14,9 @@ def remove_plan(report: str) -> str: for line in report.splitlines(): if add_line and line == "
Show Plan": add_line = False + report_lines.append( + "The plan is too long, check workflow summary to view the full report." + ) if add_line: report_lines.append(line) if not add_line and line == "
": From c13220728de0fc74a313707dbd09dfe295b2c141 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Mon, 20 May 2024 18:20:42 +0900 Subject: [PATCH 10/16] Italicize comment --- report_postprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/report_postprocess.py b/report_postprocess.py index 3109c59..e0c0d13 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -15,7 +15,7 @@ def remove_plan(report: str) -> str: if add_line and line == "
Show Plan": add_line = False report_lines.append( - "The plan is too long, check workflow summary to view the full report." + "_The plan is too long, check workflow summary to view the full report._" ) if add_line: report_lines.append(line) From ddd7c6dcc465a78e72d2bd49395f6590151ef669 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Tue, 21 May 2024 14:33:22 +0900 Subject: [PATCH 11/16] Update report_postprocess.py --- report_postprocess.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/report_postprocess.py b/report_postprocess.py index e0c0d13..e37b908 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -14,13 +14,15 @@ def remove_plan(report: str) -> str: for line in report.splitlines(): if add_line and line == "
Show Plan": add_line = False - report_lines.append( - "_The plan is too long, check workflow summary to view the full report._" - ) if add_line: report_lines.append(line) if not add_line and line == "
": add_line = True + + report_lines.append("") + eport_lines.append("---") + eport_lines.append("") + eport_lines.append("Note: the terraform plans are too long. Please check the workflow summary to view the full report.") return "\n".join(report_lines) From 70d9f21fb214adf60b3ebe11e0c6a09c48e7c8d2 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Tue, 21 May 2024 14:40:56 +0900 Subject: [PATCH 12/16] Update report_postprocess.py --- report_postprocess.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/report_postprocess.py b/report_postprocess.py index e37b908..9414754 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -20,9 +20,9 @@ def remove_plan(report: str) -> str: add_line = True report_lines.append("") - eport_lines.append("---") - eport_lines.append("") - eport_lines.append("Note: the terraform plans are too long. Please check the workflow summary to view the full report.") + report_lines.append("---") + report_lines.append("") + report_lines.append("### _Note: the terraform plans are too long. Check workflow summary to view the full report._") return "\n".join(report_lines) From b40e4341f2c67f409c783586211d859cce28e765 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Tue, 21 May 2024 15:34:11 +0900 Subject: [PATCH 13/16] Update report_postprocess.py --- report_postprocess.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/report_postprocess.py b/report_postprocess.py index 9414754..2b763a2 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -9,7 +9,12 @@ def remove_plan(report: str) -> str: - report_lines = [] + report_lines = [ + f"### ⚠️ _Terraform plan details have been removed from the report to fit within the maximum comment length. Check [workflow summary]({os.environ("GITHUB_SERVER_URL")}/{os.environ("GITHUB_REPOSITORY")}/actions/runs/{os.environ("GITHUB_RUN_ID")}) for the full report._", + "", + "---", + "" + ] add_line = True for line in report.splitlines(): if add_line and line == "
Show Plan": @@ -18,11 +23,6 @@ def remove_plan(report: str) -> str: report_lines.append(line) if not add_line and line == "
": add_line = True - - report_lines.append("") - report_lines.append("---") - report_lines.append("") - report_lines.append("### _Note: the terraform plans are too long. Check workflow summary to view the full report._") return "\n".join(report_lines) From 9280d8df807040fb57818d749215587f0a65222e Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Tue, 21 May 2024 15:45:44 +0900 Subject: [PATCH 14/16] Update report_postprocess.py --- report_postprocess.py | 1 + 1 file changed, 1 insertion(+) diff --git a/report_postprocess.py b/report_postprocess.py index 2b763a2..d5ba666 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -1,4 +1,5 @@ import argparse +import os parser = argparse.ArgumentParser() parser.add_argument( From 81457bdfecc585ec4a96fc0111e991b155345f29 Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Tue, 21 May 2024 15:49:55 +0900 Subject: [PATCH 15/16] Update report_postprocess.py --- report_postprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/report_postprocess.py b/report_postprocess.py index d5ba666..7a3a257 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -11,7 +11,7 @@ def remove_plan(report: str) -> str: report_lines = [ - f"### ⚠️ _Terraform plan details have been removed from the report to fit within the maximum comment length. Check [workflow summary]({os.environ("GITHUB_SERVER_URL")}/{os.environ("GITHUB_REPOSITORY")}/actions/runs/{os.environ("GITHUB_RUN_ID")}) for the full report._", + f"### ⚠️ _Terraform plan details have been removed from the report to fit within the maximum comment length. Check [workflow summary]({os.environ["GITHUB_SERVER_URL"]}/{os.environ(["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}) for the full report._", "", "---", "" From 2ba42f9376e29d9270346e9cb35a718f27f67f0a Mon Sep 17 00:00:00 2001 From: Muhammad Furqan Habibi Date: Tue, 21 May 2024 15:58:46 +0900 Subject: [PATCH 16/16] Update report_postprocess.py --- report_postprocess.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/report_postprocess.py b/report_postprocess.py index 7a3a257..4284911 100644 --- a/report_postprocess.py +++ b/report_postprocess.py @@ -11,7 +11,7 @@ def remove_plan(report: str) -> str: report_lines = [ - f"### ⚠️ _Terraform plan details have been removed from the report to fit within the maximum comment length. Check [workflow summary]({os.environ["GITHUB_SERVER_URL"]}/{os.environ(["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}) for the full report._", + f"### ⚠️ _Terraform plan details have been removed from the report to fit within the maximum comment length. Check [workflow summary]({os.environ["GITHUB_SERVER_URL"]}/{os.environ["GITHUB_REPOSITORY"]}/actions/runs/{os.environ["GITHUB_RUN_ID"]}) for the full report._", "", "---", ""