diff --git a/image/Dockerfile b/image/Dockerfile index 3cc16110..7a6ef562 100644 --- a/image/Dockerfile +++ b/image/Dockerfile @@ -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" diff --git a/image/entrypoints/apply.sh b/image/entrypoints/apply.sh index a8c0a386..5a309719 100755 --- a/image/entrypoints/apply.sh +++ b/image/entrypoints/apply.sh @@ -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]} diff --git a/image/entrypoints/deploy-plan.sh b/image/entrypoints/deploy-plan.sh index 16e7c223..6910d327 100755 --- a/image/entrypoints/deploy-plan.sh +++ b/image/entrypoints/deploy-plan.sh @@ -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]} diff --git a/image/entrypoints/plan.sh b/image/entrypoints/plan.sh index 2f85b6b4..a9f42e3d 100755 --- a/image/entrypoints/plan.sh +++ b/image/entrypoints/plan.sh @@ -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]} diff --git a/image/tools/compact_plan.py b/image/tools/compact_plan.py new file mode 100755 index 00000000..b7439f02 --- /dev/null +++ b/image/tools/compact_plan.py @@ -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) diff --git a/image/tools/github_pr_comment.py b/image/tools/github_pr_comment.py index dbc8f3b5..a304866d 100755 --- a/image/tools/github_pr_comment.py +++ b/image/tools/github_pr_comment.py @@ -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 @@ -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) @@ -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