|
| 1 | +import json |
| 2 | +import os |
| 3 | + |
| 4 | +from argparse import Namespace |
| 5 | +from git import Repo, GitCommandError |
| 6 | + |
| 7 | +from ..utils.cmd import BaseCommand, CMDInterface |
| 8 | + |
| 9 | + |
| 10 | +class RMKConfigInitCommand(BaseCommand, CMDInterface): |
| 11 | + def __init__(self, environment: str, args: Namespace): |
| 12 | + super().__init__(environment) |
| 13 | + self.cluster_provider = args.rmk_cluster_provider |
| 14 | + self.github_token = args.github_token |
| 15 | + self.slack_notification = "" |
| 16 | + self.slack_channel = "" |
| 17 | + self.slack_message_details = "" |
| 18 | + self.slack_webhook = "" |
| 19 | + |
| 20 | + def execute(self): |
| 21 | + self.run() |
| 22 | + |
| 23 | + def run(self): |
| 24 | + """Configure Slack notifications if enabled.""" |
| 25 | + os.environ["RMK_GITHUB_TOKEN"] = self.github_token |
| 26 | + if self.slack_notification == "true": |
| 27 | + os.environ["RMK_SLACK_WEBHOOK"] = self.slack_webhook |
| 28 | + os.environ["RMK_SLACK_CHANNEL"] = self.slack_channel |
| 29 | + |
| 30 | + flags_slack_message_details = "" |
| 31 | + if self.slack_message_details.splitlines(): |
| 32 | + flags_slack_message_details = " ".join( |
| 33 | + [f'--slack-message-details="{detail}"' for detail in self.slack_message_details.splitlines()] |
| 34 | + ) |
| 35 | + |
| 36 | + self.run_command(f"rmk config init --cluster-provider={self.cluster_provider}" |
| 37 | + f" --progress-bar=false --slack-notifications {flags_slack_message_details}") |
| 38 | + else: |
| 39 | + self.run_command(f"rmk config init --cluster-provider={self.cluster_provider} --progress-bar=false") |
| 40 | + |
| 41 | + |
| 42 | +class GETTenant(BaseCommand, CMDInterface): |
| 43 | + def __init__(self, environment: str): |
| 44 | + super().__init__(environment) |
| 45 | + |
| 46 | + def execute(self) -> str: |
| 47 | + return self.run() |
| 48 | + |
| 49 | + def run(self) -> str: |
| 50 | + output = self.run_command(f"rmk --log-format=json config view", True) |
| 51 | + rmk_config = json.loads(output) |
| 52 | + return rmk_config["config"]["Tenant"] |
| 53 | + |
| 54 | + |
| 55 | +class ProjectInitializer: |
| 56 | + GIT_CONFIG = { |
| 57 | + "name": "github-actions", |
| 58 | + "email": "github-actions@github.com", |
| 59 | + } |
| 60 | + |
| 61 | + def __init__(self, environment: str): |
| 62 | + print("Initialize project repository.") |
| 63 | + self.environment = environment |
| 64 | + self.configure_git() |
| 65 | + |
| 66 | + def configure_git(self): |
| 67 | + """Configure Git user settings.""" |
| 68 | + try: |
| 69 | + repo = Repo(".") |
| 70 | + repo.config_writer().set_value("user", "name", self.GIT_CONFIG["name"]).release() |
| 71 | + repo.config_writer().set_value("user", "email", self.GIT_CONFIG["email"]).release() |
| 72 | + except GitCommandError as err: |
| 73 | + raise ValueError(f"failed to configure Git: {err}") |
| 74 | + |
| 75 | + def configure_rmk_init(self, args: Namespace): |
| 76 | + """Configure Slack notifications using SlackConfigCommand.""" |
| 77 | + rmk_init = RMKConfigInitCommand(self.environment, args) |
| 78 | + rmk_init.execute() |
0 commit comments