Skip to content

Commit 98582c7

Browse files
committed
Test always-new
1 parent da4cb6c commit 98582c7

File tree

7 files changed

+103
-29
lines changed

7 files changed

+103
-29
lines changed

.github/workflows/test-apply.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,3 +1154,22 @@ jobs:
11541154
echo "::error:: run_id should not be set"
11551155
exit 1
11561156
fi
1157+
1158+
long_outputs:
1159+
runs-on: ubuntu-latest
1160+
name: Apply a plan with long outputs
1161+
env:
1162+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1163+
steps:
1164+
- name: Checkout
1165+
uses: actions/checkout@v3
1166+
1167+
- name: Plan
1168+
uses: ./terraform-plan
1169+
with:
1170+
path: tests/workflows/test-apply/long_outputs
1171+
1172+
- name: Apply
1173+
uses: ./terraform-apply
1174+
with:
1175+
path: tests/workflows/test-apply/long_outputs

.github/workflows/test-plan.yaml

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,3 +872,36 @@ jobs:
872872
with:
873873
path: tests/workflows/test-plan/plan
874874
label: arm64
875+
876+
always_new:
877+
runs-on: ubuntu-latest
878+
name: always-new
879+
env:
880+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
881+
steps:
882+
- name: Checkout
883+
uses: actions/checkout@v3
884+
885+
- name: Plan with label
886+
uses: ./terraform-plan
887+
with:
888+
path: tests/workflows/test-plan/always-new
889+
label: always-new SHOULD BE OUTDATED
890+
891+
- name: Plan 2 with label
892+
uses: ./terraform-plan
893+
with:
894+
path: tests/workflows/test-plan/always-new
895+
label: always-new SHOULD BE OUTDATED
896+
add_github_comment: always-new
897+
898+
- name: Plan
899+
uses: ./terraform-plan
900+
with:
901+
path: tests/workflows/test-plan/always-new
902+
903+
- name: Plan 2
904+
uses: ./terraform-plan
905+
with:
906+
path: tests/workflows/test-plan/always-new
907+
add_github_comment: always-new

image/entrypoints/plan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ if [[ -z "$PLAN_OUT" ]]; then
3838
fi
3939

4040
if [[ "$GITHUB_EVENT_NAME" == "pull_request" || "$GITHUB_EVENT_NAME" == "issue_comment" || "$GITHUB_EVENT_NAME" == "pull_request_review_comment" || "$GITHUB_EVENT_NAME" == "pull_request_target" || "$GITHUB_EVENT_NAME" == "pull_request_review" || "$GITHUB_EVENT_NAME" == "repository_dispatch" ]]; then
41-
if [[ "$INPUT_ADD_GITHUB_COMMENT" == "true" || "$INPUT_ADD_GITHUB_COMMENT" == "changes-only" ]]; then
41+
if [[ "$INPUT_ADD_GITHUB_COMMENT" == "true" || "$INPUT_ADD_GITHUB_COMMENT" == "changes-only" || "$INPUT_ADD_GITHUB_COMMENT" == "always-new" ]]; then
4242

4343
if [[ ! -v TERRAFORM_ACTIONS_GITHUB_TOKEN ]]; then
4444
echo "GITHUB_TOKEN environment variable must be set to add GitHub PR comments"

image/src/github_pr_comment/__main__.py

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -381,48 +381,51 @@ def is_approved(proposed_plan: str, comment: TerraformComment) -> bool:
381381
debug('Approving plan based on plan text')
382382
return plan_cmp(proposed_plan, comment.body)
383383

384+
def truncate(text: str, max_size: int, too_big_message: str) -> str:
385+
lines = []
386+
total_size = 0
387+
388+
for line in text.splitlines():
389+
line_size = len(line.encode()) + 1 # + newline
390+
if total_size + line_size > max_size:
391+
lines.append(too_big_message)
392+
break
393+
394+
lines.append(line)
395+
total_size += line_size
396+
397+
return '\n'.join(lines)
398+
384399
def format_plan_text(plan_text: str) -> Tuple[str, str]:
385400
"""
386401
Format the given plan for insertion into a PR comment
387402
"""
388403

389404
max_body_size = 50000 # bytes
390405

391-
def truncate(t):
392-
lines = []
393-
total_size = 0
394-
395-
for line in t.splitlines():
396-
line_size = len(line.encode()) + 1 # + newline
397-
if total_size + line_size > max_body_size:
398-
lines.append('Plan is too large to fit in a PR comment. See the full plan in the workflow log.')
399-
break
400-
401-
lines.append(line)
402-
total_size += line_size
403-
404-
return '\n'.join(lines)
405-
406406
if len(plan_text.encode()) > max_body_size:
407407
# needs truncation
408-
return 'trunc', truncate(plan_text)
408+
return 'trunc', truncate(plan_text, max_body_size, 'Plan is too large to fit in a PR comment. See the full plan in the workflow log.')
409409
else:
410410
return 'text', plan_text
411411

412-
def format_output_status(outputs: Optional[dict]) -> str:
412+
def format_output_status(outputs: Optional[dict], remaining_size: int) -> str:
413413
status = f':white_check_mark: Plan applied in {job_markdown_ref()}'
414414
if outputs is not None:
415415
stripped_output = render_outputs(outputs).strip()
416-
if '\n' in stripped_output:
417-
status += f'''\n<details open><summary>Outputs</summary>
418-
419-
```hcl
420-
{stripped_output}
421-
```
422-
</details>
423-
'''
424-
else:
425-
status += f'\nOutputs: `{stripped_output}`'
416+
417+
if len(stripped_output) > remaining_size:
418+
stripped_output = truncate(stripped_output, remaining_size, 'Outputs are too large to fit in a PR comment. See the full outputs in the workflow log.')
419+
420+
open_att = ' open' if len(stripped_output.splitlines()) > 6 else ''
421+
422+
status += f'''\n<details{open_att}><summary>Outputs</summary>
423+
424+
```hcl
425+
{stripped_output}
426+
```
427+
</details>
428+
'''
426429

427430
return status
428431

@@ -533,7 +536,10 @@ def main() -> int:
533536
return 1
534537
else:
535538
outputs = read_outputs(sys.argv[2])
536-
comment = update_comment(github, comment, headers=comment.headers | {'closed': True}, status=format_output_status(outputs))
539+
540+
remaining_size = 55000 - len(comment.body)
541+
542+
comment = update_comment(github, comment, headers=comment.headers | {'closed': True}, status=format_output_status(outputs, remaining_size))
537543

538544
elif sys.argv[1] == 'get':
539545
if comment.comment_url is None:

image/src/github_pr_comment/comment.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class TerraformComment:
3838
'plan_hash', # A deterministic hash of the plan (without warnings or unchanged attributes, eventually with unmasked variables)
3939
'variables_hash', # A hash of input variables and values
4040
'truncated' # If the plan text has been truncated (should not be used to approve plans, and will not show a complete diff)
41+
'closed' # If the comment has been closed for modifications
4142
]
4243
4344
"""
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
variable "input" {
2+
type = string
3+
default = "This is my long string\n"
4+
}
5+
6+
output "output" {
7+
value = repeat(var.input, 5000)
8+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
resource "random_string" "my_string" {
2+
length = 11
3+
}
4+
5+
output "s" {
6+
value = "string"
7+
}

0 commit comments

Comments
 (0)