Skip to content
Merged
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
33 changes: 20 additions & 13 deletions terraformworkspaceid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
import json
import logging
import pprint
import shlex
import re
import thepower


def terraform_workspace_id(text):
"""Extracts Terraform Workspace ID and write it to JSON.

Example input:
Example input (older gheboot notification format):

@kyanny
, :wave:
You've requested GHES 3.10.3 single-node resources in australiaeast on azure with setup.
You can follow your instance creation @ https://terraform.githubapp.com/app/ghes/workspaces/gheboot-kyanny-1716187058841/runs/run-PNAKUxRcwBjyNF1T. (If this is your first time launching a GHEBoot instance via Terraform you will need to log into Terraform Enterprise from the Okta tile before this link will work)
We've set the name of this request to gheboot-kyanny-1716187058841 and your Terraform workspace is: gheboot-kyanny-1716187058841 (ws-v8whx1M2tBoYcNeC) and we've used the key in position 0 in your profile in GitHub.com for SSH access to this instance.
Just FYI, the reqested instance(s) will expire on 2024-05-22.
"""

lexer = shlex.shlex(text)
lexer.whitespace_split = True
lexer.whitespace = ' \t\n\r\f\v'
tokens = list(lexer)
Example input (newer gheboot notification format):

@kyanny, Your gheboot workspace gheboot-kyanny-3-21-3-standalone-0fd7d1e8 has been created with ID ws-K7Km2WgPVYk3GWyG in Terraform Enterprise and is being configured for your GHES instance(s).
"""

# Terraform Workspace ID is the token three after "workspace"
index = tokens.index("workspace")
# Remove the parentheses
workspace_id = tokens[index + 3].replace("(", "").replace(")", "")
# Match the ID directly rather than relying on its position relative to
# surrounding wording, since the notification wording varies between formats.
match = re.search(r"\bws-[A-Za-z0-9]+\b", text)
if not match:
raise ValueError("Could not find a Terraform workspace ID (ws-XXXX) in the input")

return {"terraform_workspace_id": workspace_id}
return {"terraform_workspace_id": match.group(0)}


def main(args):
Expand All @@ -42,12 +42,19 @@ def main(args):
thepower.clear_screen()
print(f"\033[93m\n\n{message}\033[0m\n")
lines = []
empty_line_count = 0
while True:
line = input()
if line:
# Reset empty line counter and add any skipped blank lines back
for _ in range(empty_line_count):
lines.append('')
empty_line_count = 0
lines.append(line)
else:
break
empty_line_count += 1
if empty_line_count >= 2:
break
text = '\n'.join(lines)
else:
# assume the file exists
Expand Down
Loading