Skip to content
Open
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
16 changes: 9 additions & 7 deletions src/update_tf_modules/clients/github_api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import os
import logging

import requests

from ..config import GITHUB_API


logger = logging.getLogger(__name__)


def build_github_session() -> requests.Session:
"""Create an HTTP session configured for GitHub API requests.

Expand Down Expand Up @@ -67,10 +71,10 @@ def get_latest_github_tag(

raise ValueError(f"Unsupported GitHub lookup strategy: {lookup}")
except requests.HTTPError as error:
print(f"[ERROR] Failed to fetch latest GitHub version for '{repo}': {error}")
logger.error(f"Failed to fetch latest GitHub version for '{repo}': {error}")
return None
except Exception as error:
print(f"[ERROR] Unexpected error fetching GitHub version for '{repo}': {error}")
logger.exception(f"Unexpected error fetching GitHub version for '{repo}': {error}")
return None

def get_commit_hash_for_tag(
Expand Down Expand Up @@ -99,12 +103,10 @@ def get_commit_hash_for_tag(
return tag_response.json().get("object", {}).get("sha")
return obj.get("sha")
except requests.HTTPError as error:
print(
f"[ERROR] Failed to fetch commit hash for tag '{tag}' in repo '{repo}': {error}"
)
logger.error(f"Failed to fetch commit hash for tag '{tag}' in repo '{repo}': {error}")
return None
except Exception as error:
print(
f"[ERROR] Unexpected error fetching commit hash for tag '{tag}' in repo '{repo}': {error}"
logger.exception(
f"Unexpected error fetching commit hash for tag '{tag}' in repo '{repo}': {error}"
)
return None
12 changes: 8 additions & 4 deletions src/update_tf_modules/clients/registry_api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import re
import logging

import requests

from ..config import TERRAFORM_REGISTRY_API


logger = logging.getLogger(__name__)

def build_registry_session() -> requests.Session:
"""Create an HTTP session configured for Terraform Registry requests.

Expand Down Expand Up @@ -43,13 +47,13 @@ def get_latest_registry_version(
return None
return max(version_numbers, key=semver_key)
except requests.HTTPError as error:
print(
f"[ERROR] Failed to fetch latest version for registry module '{source}': {error}"
logger.error(
f"Failed to fetch latest version for registry module '{source}': {error}"
)
return None
except Exception as error:
print(
f"[ERROR] Unexpected error fetching version for registry module '{source}': {error}"
logger.exception(
f"Unexpected error fetching version for registry module '{source}': {error}"
)
return None

Expand Down
10 changes: 8 additions & 2 deletions src/update_tf_modules/discovery.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import re
import logging

from .config import TERRAFORM_ROOT
from .models import GitHubModule, Module


logger = logging.getLogger(__name__)

def normalize_discovered_source(source: str) -> str:
"""Normalize discovered module sources for manifest key comparison.

Expand Down Expand Up @@ -81,6 +85,8 @@ def warn_on_unmanaged_modules(modules: list[Module]) -> None:
unmanaged = sorted(discovered - managed)

if unmanaged:
print("[WARN] Terraform modules were found in the repo but are not represented in the manifest:")
logger.warning(
"Terraform modules were found in the repo but are not represented in the manifest:"
)
Comment on lines +88 to +90
for source in unmanaged:
print(f" - {source}")
logger.warning(f" - {source}")
6 changes: 5 additions & 1 deletion src/update_tf_modules/updaters/github_source.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from pathlib import Path
import re
import logging

from ..config import ROOT


logger = logging.getLogger(__name__)

def update_github_module(
file_path: Path,
source_prefix: str,
Expand Down Expand Up @@ -38,6 +42,6 @@ def replace(match: re.Match[str]) -> str:

if replacements > 0:
file_path.write_text(new_content, encoding="utf-8")
print(f"Updated GitHub module in {file_path.relative_to(ROOT)} to {new_ref}")
logger.info(f"Updated GitHub module in {file_path.relative_to(ROOT)} to {new_ref}")

return replacements
6 changes: 5 additions & 1 deletion src/update_tf_modules/updaters/registry_source.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
from pathlib import Path
import re
import logging

from ..config import ROOT


logger = logging.getLogger(__name__)

def update_registry_module(
file_path: Path,
source: str,
Expand Down Expand Up @@ -90,7 +94,7 @@ def update_registry_module(
old_content = file_path.read_text(encoding="utf-8")
if new_content != old_content:
file_path.write_text(new_content, encoding="utf-8")
print(
logger.info(
f"Updated registry module '{source}' in {file_path.relative_to(ROOT)} to {new_version}"
)

Expand Down
10 changes: 5 additions & 5 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ def test_managed_source_keys():
assert "git::https://github.com/org/repo.git?ref=" in result
assert "registry.terraform.io/org/module/aws" in result

def test_warn_on_unmanaged_modules(monkeypatch: MonkeyPatch, capsys: pytest.CaptureFixture[str]):
def test_warn_on_unmanaged_modules(monkeypatch: MonkeyPatch, caplog: pytest.LogCaptureFixture):
monkeypatch.setattr(discovery, "discover_module_sources", lambda: {"source1", "source2"})
monkeypatch.setattr(discovery, "managed_source_keys", lambda _: {"source1"})
warn_on_unmanaged_modules([])
captured = capsys.readouterr()
assert "[WARN] Terraform modules were found in the repo but are not represented in the manifest:" in captured.out
assert " - source2" in captured.out
with caplog.at_level("WARNING"):
warn_on_unmanaged_modules([])
assert "Terraform modules were found in the repo but are not represented in the manifest:" in caplog.text
assert " - source2" in caplog.text