Skip to content
Closed
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
14 changes: 8 additions & 6 deletions src/update_tf_modules/clients/github_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import requests

from ..config import GITHUB_API
import logging
logger = logging.getLogger(__name__)


def build_github_session() -> requests.Session:
Expand Down Expand Up @@ -67,10 +69,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.error(f"Unexpected error fetching GitHub version for '{repo}': {error}")
return None

def get_commit_hash_for_tag(
Expand Down Expand Up @@ -99,12 +101,12 @@ 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.error(
f"Unexpected error fetching commit hash for tag '{tag}' in repo '{repo}': {error}"
)
return None
10 changes: 6 additions & 4 deletions src/update_tf_modules/clients/registry_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import requests

from ..config import TERRAFORM_REGISTRY_API
import logging
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 +45,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.error(
f"Unexpected error fetching version for registry module '{source}': {error}"
)
return None

Expand Down
6 changes: 4 additions & 2 deletions src/update_tf_modules/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from .config import TERRAFORM_ROOT
from .models import GitHubModule, Module
import logging
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 +83,6 @@ 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("[WARN] Terraform modules were found in the repo but are not represented in the manifest:")
for source in unmanaged:
print(f" - {source}")
logger.warning(f" - {source}")
4 changes: 3 additions & 1 deletion src/update_tf_modules/updaters/github_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re

from ..config import ROOT
import logging
logger = logging.getLogger(__name__)

def update_github_module(
file_path: Path,
Expand Down Expand Up @@ -32,6 +34,6 @@ def update_github_module(

if count > 0 and new_content != content:
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 count
4 changes: 3 additions & 1 deletion src/update_tf_modules/updaters/registry_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import re

from ..config import ROOT
import logging
logger = logging.getLogger(__name__)

def update_registry_module(
file_path: Path,
Expand Down Expand Up @@ -89,7 +91,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
7 changes: 3 additions & 4 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ 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, caplog):
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
assert "Terraform modules were found in the repo but are not represented in the manifest:" in caplog.text
assert " - source2" in caplog.text