Skip to content

improve digger commands #7

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

Merged
merged 3 commits into from
Mar 1, 2023
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
20 changes: 8 additions & 12 deletions code/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@

from githubpr import GitHubPR
from simple_lock import acquire_lock, release_lock, get_lock
from tf_utils import get_terraform_plan, get_terraform_apply, cleanup_terraform_plan, cleanup_terraform_apply
from tf_utils import (
get_terraform_plan,
get_terraform_apply,
cleanup_terraform_plan,
cleanup_terraform_apply,
)
from usage import send_usage_record

import github_action_utils as gha_utils

logger = logging.getLogger("python_terraform")
logger.setLevel(logging.CRITICAL)


def main(argv):
dynamodb = boto3.resource("dynamodb")

Expand Down Expand Up @@ -40,17 +47,6 @@ def main(argv):

print(f"event_name: {event_name}")

if (
event_name not in ["issue_comment"]
and ref_name
and not head_ref
and not base_ref
):
print(f"commit merged to {ref_name}")
# lock_released = release_lock(dynamodb, repo_name)
# if lock_released:
# print("Project unlocked")

if "pull_request" in j["event"]:
if "merged" in j["event"]["pull_request"]:
print(f"pull_request merged: {j['event']['pull_request']['merged']}")
Expand Down
32 changes: 20 additions & 12 deletions code/tf_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,27 @@ def cleanup_terraform_plan(return_code: int, stdout: str, stderr: str):
return "```terraform\n" + error + "\n```"

result = None
if return_code == 0 or return_code == 2:

if return_code == 0:
# Succeeded, with empty diff (no changes)
start = "No changes. Your infrastructure matches the configuration."

if return_code == 2:
# Succeeded, with non-empty diff (changes present)
start = "Terraform will perform the following actions:"
end_pos = len(stdout)
end_pos = len(stdout)

try:
start_pos = stdout.index(start)
except ValueError:
start_pos = 0
try:
start_pos = stdout.index(start)
except ValueError:
start_pos = 0

regex = r"(Plan: [0-9]+ to add, [0-9]+ to change, [0-9]+ to destroy.)"
matches = re.search(regex, stdout, re.MULTILINE)
if matches:
end_pos = matches.end()
regex = r"(Plan: [0-9]+ to add, [0-9]+ to change, [0-9]+ to destroy.)"
matches = re.search(regex, stdout, re.MULTILINE)
if matches:
end_pos = matches.end()

result = stdout[start_pos:end_pos]
result = stdout[start_pos:end_pos]

return "```terraform\n" + result + "\n```"

Expand All @@ -49,7 +55,9 @@ def cleanup_terraform_apply(return_code: int, stdout: str, stderr: str):
except ValueError:
start_pos = 0

regex = r"(Apply complete! Resources: [0-9]+ added, [0-9]+ changed, [0-9]+ destroyed.)"
regex = (
r"(Apply complete! Resources: [0-9]+ added, [0-9]+ changed, [0-9]+ destroyed.)"
)
matches = re.search(regex, stdout, re.MULTILINE)
if matches:
end_pos = matches.end()
Expand Down