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

Run show internally; fix yaml diffs #30

Open
wants to merge 81 commits into
base: master
Choose a base branch
from

Conversation

jcogilvie
Copy link

@jcogilvie jcogilvie commented Feb 24, 2022

Fixes #27 and #8

It's a little rough around the edges but this fork does three big things:

  1. for the case of a plan, it runs terraform show internally to build its comments; this saves us from the 64k character limit
  2. for the case of a plan whose output exceeds 64k, it splits the plan comment into as many as are necessary to capture all the output (and, correspondingly, it deletes all matching plan comments now, instead of just the first)
  3. any lines in the plan diff starting with a red - are known to be diff-removal lines; lines with a non-red - are known to be text contents; therefore when we find a red one, we temporarily swap it out for an intermediate representation (a literal 😅 in high unicode) before swapping it back at the end in the final diff.

It now accepts a terraform version argument (for plan compatibility purposes) and it expects auth to be available on the environment (to an s3 backend; other cloud providers not tested). Further, it assumes that if you have a plan, it is named tfplan. I didn't want to mess with the externally-facing API, so I leave it to @robburger to decide how to accept a plan file name and what to do with the COMMENTER_INPUT env var that I have used to make the command line invocation more legible.

I apologize for the commit noise; I forgot to set the merge method to "squash" when I forked/merged a couple PRs on my end.

jcogilvie and others added 3 commits February 24, 2022 13:33
* Fix yaml diffs

* Remove branch callout

* Cleanup and doc
@jcogilvie jcogilvie changed the title Run show internally Run show internally; fix yaml diffs Feb 25, 2022
* Add debug flags

* Point at branch

* Fix echo check'

* Work with failed plans; attempt to cd for doing tf show

* Perform init on plan (sigh)

* Perform init on plan (sigh)

* Fix error handling a bit

* Fix case of short plan

* Remove branch ref
* Don't regex replace in the context of '+/-'

* Point to branch

* Try perl?

* Not perl.  sed -r maybe?

* Use perl but install it this time

* Remove branch
@jcogilvie
Copy link
Author

@robburger can you take a look at this?

@dkooll
Copy link

dkooll commented Apr 13, 2022

@robburger would be great if this one will be merged

* Omit drift detection

* Cleanup for PR
@AldinDuraki
Copy link

@robburger please take a look at this

* Commit testing files to track changes

* Refactor plan

* Uncomment delete of old comments

* Add debug info

* Remove testing statement w/curl

* Add more debug

* Fix loop bug

* Add index to comments

* Add index to comments

* Add outputs comment(s)

* Update for outputs

* Fix boundary mixup

* Code reuse

* Refactor everything to functions

* Try fixing failed plan

* Try plan fail now

* Refactor html tags

* Remove comments for outputs

* Correct commenter input

* Fix up commenter plan failure

* Remove branch ref (back to master)

* Remove temp file
* Fix  style syntax

* Point at branch

* Remove branch ref
@jcogilvie
Copy link
Author

jcogilvie commented May 11, 2022

Refactored the bash into functions, cleaned up the comment count, and added a "Changes to Outputs" comment with a flag to enable it.

Problem: usage gets a little messier if you do have really long diffs. The step that generates the plan is responsible for trimming the output string to avoid passing a too-long variable into bash on the next step.

We must continue to propagate the text output from the plan step for the "plan failed" case, but handling that case brings back the "argument too long" problems on the success case. So we bite the bullet and handle it.

    - name: TF Plan - Run
      id: plan
      # have to use /bin/bash because GHA runs by default with `set -e` to end execution on any error.
      # we want to capture the error instead.
      shell: '/bin/bash {0}'
      run: |
        # a lot going on here.  copy the stdout file handle to fd5.
        exec 5>&1

        # merge stderr into stdout and print it to fd5 (parent shell's stdout); exit with the code from terraform plan
        OUTPUT=$(terraform plan -lock=false -input=false -out=tfplan 2>&1 | tee /dev/fd/5; exit ${PIPESTATUS[0]})

        # store the exit code here
        EXITCODE=$?

        # github actions doesn't allow us to set a multiline output so we cat it to the environment
        echo "PLAN_OUTPUT<<EOF" >> $GITHUB_ENV
        echo "${OUTPUT::65000}" >> $GITHUB_ENV
        echo "EOF" >> $GITHUB_ENV

        # set exit code for pickup later, and make sure we exit with same code
        echo "::set-output name=exit::${EXITCODE}"
        exit $EXITCODE
    - name: Post TF Plan Comment
      if: ${{ always() && github.ref != format('refs/heads/{0}', github.event.repository.default_branch) && inputs.enable_plan_commenter == 'true' }}
      uses: GetTerminus/terraform-pr-commenter@master
      env:
        GITHUB_TOKEN: ${{ inputs.github_token }}
        EXPAND_SUMMARY_DETAILS: "false"
        TF_WORKSPACE: ${{ inputs.tf_workspace }}
      with:
        commenter_type: plan
        commenter_input: ${{ env.PLAN_OUTPUT }}
        commenter_plan_path: tfplan
        commenter_exitcode: ${{ steps.plan.outputs.exit }}
        terraform_version: ${{ inputs.terraform_version }}

dreinhardt89 and others added 7 commits May 23, 2022 13:07
BREAKING CHANGE: Switch to version v2
* Try paginating comments

* Fix range

* Update action to run from branch (undo before merge)

* Update beta release to track v2; add flag to run beta commenter

* Fix pagination url

* Better logging

* Really fix query parameter

* Detect rate limiting(ish)

* Fix deletes during pagination
This was referenced Apr 11, 2023
@sunnyoswalcro
Copy link

@jcogilvie if this is not the right place to report this , pls redirect to the best way for this communication.

I am using your forked repo action:

- name: PR COMMENT -Plan - ${{ matrix['env'] }}
  id: planprcomment
  if: github.event_name == 'pull_request'
  uses: GetTerminus/terraform-pr-commenter@master
  continue-on-error: true
  with:
    commenter_type: plan
    commenter_input: ${{ format('{0}{1}', steps.plan.outputs.stdout, steps.plan.outputs.stderr) }}
    commenter_exitcode: ${{ steps.plan.outputs.exitcode }}
    terraform_version: "1.4.5" # this version is just used for this action and not in terraform execution.
  env:
    TF_WORKSPACE: ${{ matrix['env'] }}

for 1 repo i have 3 envs running in parallel . but for only 1 env i never get plan PR comment .

logs for the PR comments that worked:

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
╷
│ Warning: Experimental feature "module_variable_optional_attrs" is active
│ 
│   on .terraform/modules/node_groups_2/node-group-2/variables.tf line 42, in terraform:
│   42:   experiments = [module_variable_optional_attrs]
│ 
│ Experimental features are subject to breaking changes in future minor or
│ patch releases, based on feedback.
│ 
│ If you have feedback on the design of this feature, please open a GitHub
│ issue to discuss it.
│ 
│ (and 2 more similar warnings elsewhere)
╵
Releasing state lock. This may take a few moments...
INFO: Found no tfplan.  Proceeding with input argument.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing plan PR comment.
INFO: No existing plan PR comment found.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing outputs PR comment.
INFO: No existing outputs PR comment found.
INFO: Writing 1 plan comment(s)
INFO: Adding plan comment to PR.
INFO: Writing 0 outputs comment(s)

logs for the PR comments that didn't work :

INFO: Found no tfplan.  Proceeding with input argument.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing plan PR comment.
INFO: No existing plan PR comment found.
INFO: Found 1 page(s) of comments at https://api.github.com/repos/monacohq/oex-terraform-exchange/issues/313/comments?per_page=100.
INFO: Looking for an existing outputs PR comment.
INFO: No existing outputs PR comment found.
INFO: Writing 0 plan comment(s)
INFO: Writing 0 outputs comment(s)

plan output:

No changes. Your infrastructure matches the configuration.

Terraform has compared your real infrastructure against your configuration
and found no differences, so no changes are needed.
╷
│ Warning: Experimental feature "module_variable_optional_attrs" is active
│ 
│   on .terraform/modules/node_groups_2/node-group-2/variables.tf line 42, in terraform:
│   42:   experiments = [module_variable_optional_attrs]
│ 
│ Experimental features are subject to breaking changes in future minor or
│ patch releases, based on feedback.
│ 
│ If you have feedback on the design of this feature, please open a GitHub
│ issue to discuss it.
│ 
│ (and 2 more similar warnings elsewhere)
╵
Releasing state lock. This may take a few moments...

@jcogilvie
Copy link
Author

Hey there @sunnyoswalcro -- I just enabled issues on the forked repo, if you want to put this over there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Read plan from a file?
5 participants