Skip to content

Commit

Permalink
Add update-vcs-config command
Browse files Browse the repository at this point in the history
  • Loading branch information
jmfontaine committed May 14, 2024
1 parent e09c36d commit 2026bfc
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
__pycache__
.coverage
.terraform/
*.csv
*.egg-info
/.cache
/.env
Expand Down
60 changes: 60 additions & 0 deletions custom/commands/update_vcs_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import csv
import logging
from pathlib import Path
from urllib.parse import urlparse

import click
from click_help_colors import HelpColorsCommand

from spacemk import get_tmp_folder, load_normalized_data, save_normalized_data


def find_stack_vcs_config(stack_name: str, vcs_config: dict) -> dict | None:
for item in vcs_config:
if item.get("WorkspaceName") == stack_name:
return item

return None


def load_vcs_config(path: str) -> dict:
with Path(path).open("r", encoding="utf-8") as fp:
return list(csv.DictReader(fp))


@click.command(
cls=HelpColorsCommand,
help="Update stacks VCS configuration.",
help_headers_color="yellow",
help_options_color="green",
)
@click.option(
"--path",
"vcs_config_file_path",
default="vcs_config.csv",
help="Path to the CSV file containing the VCS configuration.",
show_default=True,
type=click.Path(exists=True),
)
def update_vcs_config(vcs_config_file_path: str):
data = load_normalized_data()
save_normalized_data(data, Path(get_tmp_folder(), "data.bak.json"))
vcs_config = load_vcs_config(vcs_config_file_path)

for stack in data.get("stacks"):
stack_vcs_config = find_stack_vcs_config(stack.get("name"), vcs_config)
if not stack_vcs_config:
logging.warning(f"No VCS configuration found for the '{stack.get('name')}' stack. Skipping.")
continue

stack.vcs.update(
{
"branch": stack_vcs_config.get("Branch"),
"namespace": urlparse(stack_vcs_config.get("RepoURL")).path.split("/")[1],
"project_root": Path(stack_vcs_config.get("BackendPath")).parent.as_posix() + "/",
"provider": "azure_devops",
"repository": stack_vcs_config.get("RepoName"),
}
)

save_normalized_data(data)
11 changes: 9 additions & 2 deletions spacemk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from importlib.metadata import version
from pathlib import Path
from shutil import which
from typing import Optional

import gitinfo
from benedict import benedict
Expand Down Expand Up @@ -75,7 +76,13 @@ def load_normalized_data() -> dict:
return benedict(json.load(fp))


def save_normalized_data(data: dict) -> None:
path = Path(get_tmp_folder(), "data.json")
def save_normalized_data(data: dict, path: Optional[str | Path] = None) -> None:
if not path:
path = Path(get_tmp_folder(), "data.json")
elif isinstance(path, str):
path = Path(path)
elif not isinstance(path, Path):
raise ValueError("The path argument must be a string or a Path object.")

with path.open("w", encoding="utf-8") as fp:
json.dump(data, fp, indent=2, sort_keys=True)

0 comments on commit 2026bfc

Please sign in to comment.