Skip to content

Commit

Permalink
Test always-new
Browse files Browse the repository at this point in the history
  • Loading branch information
dflook committed Dec 7, 2023
1 parent da4cb6c commit 98582c7
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 29 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/test-apply.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1154,3 +1154,22 @@ jobs:
echo "::error:: run_id should not be set"
exit 1
fi
long_outputs:
runs-on: ubuntu-latest
name: Apply a plan with long outputs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Plan
uses: ./terraform-plan
with:
path: tests/workflows/test-apply/long_outputs

- name: Apply
uses: ./terraform-apply
with:
path: tests/workflows/test-apply/long_outputs
33 changes: 33 additions & 0 deletions .github/workflows/test-plan.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -872,3 +872,36 @@ jobs:
with:
path: tests/workflows/test-plan/plan
label: arm64

always_new:
runs-on: ubuntu-latest
name: always-new
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Plan with label
uses: ./terraform-plan
with:
path: tests/workflows/test-plan/always-new
label: always-new SHOULD BE OUTDATED

- name: Plan 2 with label
uses: ./terraform-plan
with:
path: tests/workflows/test-plan/always-new
label: always-new SHOULD BE OUTDATED
add_github_comment: always-new

- name: Plan
uses: ./terraform-plan
with:
path: tests/workflows/test-plan/always-new

- name: Plan 2
uses: ./terraform-plan
with:
path: tests/workflows/test-plan/always-new
add_github_comment: always-new
2 changes: 1 addition & 1 deletion image/entrypoints/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ if [[ -z "$PLAN_OUT" ]]; then
fi

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
if [[ "$INPUT_ADD_GITHUB_COMMENT" == "true" || "$INPUT_ADD_GITHUB_COMMENT" == "changes-only" ]]; then
if [[ "$INPUT_ADD_GITHUB_COMMENT" == "true" || "$INPUT_ADD_GITHUB_COMMENT" == "changes-only" || "$INPUT_ADD_GITHUB_COMMENT" == "always-new" ]]; then

if [[ ! -v TERRAFORM_ACTIONS_GITHUB_TOKEN ]]; then
echo "GITHUB_TOKEN environment variable must be set to add GitHub PR comments"
Expand Down
62 changes: 34 additions & 28 deletions image/src/github_pr_comment/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -381,48 +381,51 @@ def is_approved(proposed_plan: str, comment: TerraformComment) -> bool:
debug('Approving plan based on plan text')
return plan_cmp(proposed_plan, comment.body)

def truncate(text: str, max_size: int, too_big_message: str) -> str:
lines = []
total_size = 0

for line in text.splitlines():
line_size = len(line.encode()) + 1 # + newline
if total_size + line_size > max_size:
lines.append(too_big_message)
break

lines.append(line)
total_size += line_size

return '\n'.join(lines)

def format_plan_text(plan_text: str) -> Tuple[str, str]:
"""
Format the given plan for insertion into a PR comment
"""

max_body_size = 50000 # bytes

def truncate(t):
lines = []
total_size = 0

for line in t.splitlines():
line_size = len(line.encode()) + 1 # + newline
if total_size + line_size > max_body_size:
lines.append('Plan is too large to fit in a PR comment. See the full plan in the workflow log.')
break

lines.append(line)
total_size += line_size

return '\n'.join(lines)

if len(plan_text.encode()) > max_body_size:
# needs truncation
return 'trunc', truncate(plan_text)
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.')
else:
return 'text', plan_text

def format_output_status(outputs: Optional[dict]) -> str:
def format_output_status(outputs: Optional[dict], remaining_size: int) -> str:
status = f':white_check_mark: Plan applied in {job_markdown_ref()}'
if outputs is not None:
stripped_output = render_outputs(outputs).strip()
if '\n' in stripped_output:
status += f'''\n<details open><summary>Outputs</summary>
```hcl
{stripped_output}
```
</details>
'''
else:
status += f'\nOutputs: `{stripped_output}`'

if len(stripped_output) > remaining_size:
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.')

open_att = ' open' if len(stripped_output.splitlines()) > 6 else ''

status += f'''\n<details{open_att}><summary>Outputs</summary>
```hcl
{stripped_output}
```
</details>
'''

return status

Expand Down Expand Up @@ -533,7 +536,10 @@ def main() -> int:
return 1
else:
outputs = read_outputs(sys.argv[2])
comment = update_comment(github, comment, headers=comment.headers | {'closed': True}, status=format_output_status(outputs))

remaining_size = 55000 - len(comment.body)

comment = update_comment(github, comment, headers=comment.headers | {'closed': True}, status=format_output_status(outputs, remaining_size))

elif sys.argv[1] == 'get':
if comment.comment_url is None:
Expand Down
1 change: 1 addition & 0 deletions image/src/github_pr_comment/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class TerraformComment:
'plan_hash', # A deterministic hash of the plan (without warnings or unchanged attributes, eventually with unmasked variables)
'variables_hash', # A hash of input variables and values
'truncated' # If the plan text has been truncated (should not be used to approve plans, and will not show a complete diff)
'closed' # If the comment has been closed for modifications
]
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/workflows/test-apply/long_outputs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
variable "input" {
type = string
default = "This is my long string\n"
}

output "output" {
value = repeat(var.input, 5000)
}
7 changes: 7 additions & 0 deletions tests/workflows/test-plan/always_new/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "random_string" "my_string" {
length = 11
}

output "s" {
value = "string"
}

0 comments on commit 98582c7

Please sign in to comment.