Skip to content

Commit b78dead

Browse files
Merge pull request #3 from SparkPost/feature/SD-2436
SD-2436 Backport upstream changes to support TF-1.4
2 parents 1ad1e7d + e02876c commit b78dead

File tree

6 files changed

+51
-6
lines changed

6 files changed

+51
-6
lines changed

image/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,5 +50,8 @@ COPY tools/convert_output.py /usr/local/bin/convert_output
5050
COPY tools/plan_cmp.py /usr/local/bin/plan_cmp
5151
COPY tools/convert_version.py /usr/local/bin/convert_version
5252
COPY tools/workspace_exists.py /usr/local/bin/workspace_exists
53+
COPY tools/compact_plan.py /usr/local/bin/compact_plan
54+
5355

5456
ENTRYPOINT ["/usr/local/bin/terraform"]
57+
LABEL org.opencontainers.image.title="GitHub actions for terraform"

image/entrypoints/apply.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function plan() {
3838
2>"$PLAN_DIR/error.txt" \
3939
| $TFMASK \
4040
| tee /dev/fd/3 \
41-
| sed '1,/---/d' \
41+
| compact_plan \
4242
>"$PLAN_DIR/plan.txt"
4343

4444
PLAN_EXIT=${PIPESTATUS[0]}

image/entrypoints/deploy-plan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set +e
1919
2>"$PLAN_DIR/error.txt" \
2020
| $TFMASK \
2121
| tee /dev/fd/3 \
22-
| sed '1,/---/d' \
22+
| compact_plan \
2323
>"$PLAN_DIR/plan.txt"
2424

2525
readonly TF_EXIT=${PIPESTATUS[0]}

image/entrypoints/plan.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ set +e
1919
2>"$PLAN_DIR/error.txt" \
2020
| $TFMASK \
2121
| tee /dev/fd/3 \
22-
| sed '1,/---/d' \
22+
| compact_plan \
2323
>"$PLAN_DIR/plan.txt"
2424

2525
readonly TF_EXIT=${PIPESTATUS[0]}

image/tools/compact_plan.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/python3
2+
3+
import sys
4+
5+
6+
def compact_plan(input):
7+
plan = False
8+
buffer = []
9+
10+
for line in input:
11+
12+
if not plan and (
13+
line.startswith('An execution plan has been generated and is shown below') or
14+
line.startswith('No changes') or
15+
line.startswith('Error')
16+
):
17+
plan = True
18+
19+
if plan:
20+
yield line
21+
else:
22+
buffer.append(line)
23+
24+
if not plan and buffer:
25+
yield from buffer
26+
27+
28+
if __name__ == '__main__':
29+
for line in compact_plan(sys.stdin.readlines()):
30+
sys.stdout.write(line)

image/tools/github_pr_comment.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@ def find_pr() -> str:
7070
else:
7171
raise Exception(f"The {event_type} event doesn\'t relate to a Pull Request.")
7272

73+
def current_user() -> str:
74+
response = github.get('https://api.github.com/user')
75+
if response.status_code != 403:
76+
user = response.json()
77+
debug('GITHUB_TOKEN user:')
78+
debug(json.dumps(user))
79+
80+
return user['login']
81+
82+
# Assume this is the github actions app token
83+
return 'github-actions[bot]'
84+
7385
class TerraformComment:
7486
"""
7587
The GitHub comment for this specific terraform plan
@@ -90,8 +102,8 @@ def __init__(self, pr_url: str):
90102
debug('Looking for an existing comment:')
91103
for comment in response.json():
92104
debug(json.dumps(comment))
93-
if comment['user']['login'] == 'github-actions[bot]':
94-
match = re.match(rf'{re.escape(self._comment_identifier)}\n```(.*?)```(.*)', comment['body'], re.DOTALL)
105+
if comment['user']['login'] == current_user():
106+
match = re.match(rf'{re.escape(self._comment_identifier)}\n```(?:hcl)?(.*?)```(.*)', comment['body'], re.DOTALL)
95107

96108
if not match:
97109
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:
232244
self._status = status.strip()
233245

234246
def update_comment(self):
235-
body = f'{self._comment_identifier}\n```\n{self.plan}\n```'
247+
body = f'{self._comment_identifier}\n```hcl\n{self.plan}\n```'
236248

237249
if self.status:
238250
body += '\n' + self.status

0 commit comments

Comments
 (0)