Skip to content

Commit

Permalink
fix: remove plan from report if comment is too long (#35)
Browse files Browse the repository at this point in the history
* Remove plan if report is too long

* Keep full report for job summary

* Set report as full report by default

* Naming

* Fix logic

* Missed -full-report arg

* Remove plans from the overall report file

* Fix script call

* Add instruction to check the full report

* Italicize comment

* Update report_postprocess.py

* Update report_postprocess.py

* Update report_postprocess.py

* Update report_postprocess.py

* Update report_postprocess.py

* Update report_postprocess.py
  • Loading branch information
FurqanHabibi authored May 21, 2024
1 parent e4c9ae5 commit 1b6d220
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
8 changes: 5 additions & 3 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 @@ -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.md
delimiter="$(openssl rand -hex 8)"
echo "result<<$delimiter" >> $GITHUB_OUTPUT
Expand All @@ -126,8 +130,6 @@ runs:
echo "report<<$delimiter" >> $GITHUB_OUTPUT
echo "$(cat report.md)" >> $GITHUB_OUTPUT
echo "$delimiter" >> $GITHUB_OUTPUT
echo "$(cat report.md)" >> $GITHUB_STEP_SUMMARY
shell: bash

- uses: actions/github-script@v7
Expand Down
39 changes: 39 additions & 0 deletions report_postprocess.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import argparse
import os

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 = [
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 == "<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()

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

args.report.seek(0)
args.report.truncate()
args.report.write(report)

0 comments on commit 1b6d220

Please sign in to comment.