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

SD-2436 Backport upstream changes to support TF-1.4 #3

Merged
merged 1 commit into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,8 @@ COPY tools/convert_output.py /usr/local/bin/convert_output
COPY tools/plan_cmp.py /usr/local/bin/plan_cmp
COPY tools/convert_version.py /usr/local/bin/convert_version
COPY tools/workspace_exists.py /usr/local/bin/workspace_exists
COPY tools/compact_plan.py /usr/local/bin/compact_plan


ENTRYPOINT ["/usr/local/bin/terraform"]
LABEL org.opencontainers.image.title="GitHub actions for terraform"
2 changes: 1 addition & 1 deletion image/entrypoints/apply.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function plan() {
2>"$PLAN_DIR/error.txt" \
| $TFMASK \
| tee /dev/fd/3 \
| sed '1,/---/d' \
| compact_plan \
>"$PLAN_DIR/plan.txt"

PLAN_EXIT=${PIPESTATUS[0]}
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/deploy-plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set +e
2>"$PLAN_DIR/error.txt" \
| $TFMASK \
| tee /dev/fd/3 \
| sed '1,/---/d' \
| compact_plan \
>"$PLAN_DIR/plan.txt"

readonly TF_EXIT=${PIPESTATUS[0]}
Expand Down
2 changes: 1 addition & 1 deletion image/entrypoints/plan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set +e
2>"$PLAN_DIR/error.txt" \
| $TFMASK \
| tee /dev/fd/3 \
| sed '1,/---/d' \
| compact_plan \
>"$PLAN_DIR/plan.txt"

readonly TF_EXIT=${PIPESTATUS[0]}
Expand Down
30 changes: 30 additions & 0 deletions image/tools/compact_plan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/python3

import sys


def compact_plan(input):
plan = False
buffer = []

for line in input:

if not plan and (
line.startswith('An execution plan has been generated and is shown below') or
line.startswith('No changes') or
line.startswith('Error')
):
plan = True

if plan:
yield line
else:
buffer.append(line)

if not plan and buffer:
yield from buffer


if __name__ == '__main__':
for line in compact_plan(sys.stdin.readlines()):
sys.stdout.write(line)
18 changes: 15 additions & 3 deletions image/tools/github_pr_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ def find_pr() -> str:
else:
raise Exception(f"The {event_type} event doesn\'t relate to a Pull Request.")

def current_user() -> str:
response = github.get('https://api.github.com/user')
if response.status_code != 403:
user = response.json()
debug('GITHUB_TOKEN user:')
debug(json.dumps(user))

return user['login']

# Assume this is the github actions app token
return 'github-actions[bot]'

class TerraformComment:
"""
The GitHub comment for this specific terraform plan
Expand All @@ -90,8 +102,8 @@ def __init__(self, pr_url: str):
debug('Looking for an existing comment:')
for comment in response.json():
debug(json.dumps(comment))
if comment['user']['login'] == 'github-actions[bot]':
match = re.match(rf'{re.escape(self._comment_identifier)}\n```(.*?)```(.*)', comment['body'], re.DOTALL)
if comment['user']['login'] == current_user():
match = re.match(rf'{re.escape(self._comment_identifier)}\n```(?:hcl)?(.*?)```(.*)', comment['body'], re.DOTALL)

if not match:
match = re.match(rf'{re.escape(self._old_comment_identifier)}\n```(.*?)```(.*)', comment['body'], re.DOTALL)
Expand Down Expand Up @@ -232,7 +244,7 @@ def status(self, status: str) -> None:
self._status = status.strip()

def update_comment(self):
body = f'{self._comment_identifier}\n```\n{self.plan}\n```'
body = f'{self._comment_identifier}\n```hcl\n{self.plan}\n```'

if self.status:
body += '\n' + self.status
Expand Down