Skip to content

Commit 5b8847b

Browse files
authored
Merge pull request #194 from dflook/move-summary
Add number of resource moves to plan summary
2 parents a51ca44 + 7c64325 commit 5b8847b

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

image/src/github_pr_comment/__main__.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import hashlib
22
import json
33
import os
4+
import re
45
import sys
56
from pathlib import Path
67
from typing import (NewType, Optional, cast)
@@ -108,18 +109,26 @@ def format_classic_description(action_inputs: PlanPrInputs) -> str:
108109
def create_summary(plan: Plan) -> Optional[str]:
109110
summary = None
110111

112+
to_move = 0
113+
111114
for line in plan.splitlines():
112115
if line.startswith('No changes') or line.startswith('Error'):
113116
return line
114117

118+
if re.match(r' # \S+ has moved to \S+$', line):
119+
to_move += 1
120+
115121
if line.startswith('Plan:'):
116122
summary = line
117123

124+
if to_move and 'move' not in summary:
125+
summary = summary.rstrip('.') + f', {to_move} to move.'
126+
118127
if line.startswith('Changes to Outputs'):
119128
if summary:
120129
return summary + ' Changes to Outputs.'
121130
else:
122-
return 'Changes to Outputs'
131+
return 'Changes to Outputs.'
123132

124133
return summary
125134

tests/github_pr_comment/test_summary.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,27 @@ def test_summary_unknown():
158158
This is not anything like terraform output we know. We don't want to generate a summary for this.
159159
"""
160160
assert create_summary(plan) is None
161+
162+
def test_summary_move_only():
163+
plan = """Terraform will perform the following actions:
164+
165+
# random_string.your_string has moved to random_string.my_string
166+
resource "random_string" "my_string" {
167+
id = "Iyh3jLKc"
168+
length = 8
169+
# (8 unchanged attributes hidden)
170+
}
171+
172+
# random_string.blah_string has moved to random_string.my_string2
173+
resource "random_string" "my_string2" {
174+
id = "Iyh3jLKc"
175+
length = 8
176+
# (8 unchanged attributes hidden)
177+
}
178+
179+
Plan: 0 to add, 0 to change, 0 to destroy.
180+
"""
181+
182+
expected = "Plan: 0 to add, 0 to change, 0 to destroy, 2 to move."
183+
184+
assert create_summary(plan) == expected

tests/test_compact_plan.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ def test_plan_15():
226226
output = '\n'.join(compact_plan(input.splitlines()))
227227
assert output == expected_output
228228

229-
def test_plan_refresh_on_changes_11():
229+
def test_plan_refresh_no_changes_11():
230230
input = """
231231
Refreshing Terraform state in-memory prior to plan...
232232
The refreshed state will be used to calculate this plan, but will not be
@@ -510,7 +510,7 @@ def test_plan_refresh_changes_15():
510510
output = '\n'.join(compact_plan(input.splitlines()))
511511
assert output == expected_output
512512

513-
def test_plan_refresh_changes_16():
513+
def test_plan_move_only():
514514
input = """
515515
random_string.my_string: Refreshing state... [id=Iyh3jLKc]
516516

0 commit comments

Comments
 (0)