Skip to content

Commit

Permalink
Fix/action_file_references (#23)
Browse files Browse the repository at this point in the history
* feat(action): Implement correct handling of repo root and action files path

* fix(tests): Change inputs in config tests.

* refac(rich): Change text in progress bar.

* fix(action): Change working directory
  • Loading branch information
Noahnc authored Nov 29, 2023
1 parent 46a50ba commit cb9d633
Show file tree
Hide file tree
Showing 7 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ runs:
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
working-directory: ${{ github.action_path }}
shell: bash

- name: Configure git
Expand All @@ -81,7 +82,9 @@ runs:
git config --global user.email "${{ inputs.git_email }}"
- name: Run InfraPatch Action

shell: bash
working-directory: ${{ github.action_path }}
run: |
module="infrapatch.action"
arguments=()
Expand All @@ -99,6 +102,8 @@ runs:
WORKING_DIRECTORY_RELATIVE: ${{ inputs.working_directory_relative }}
ENABLED_PROVIDERS: ${{ inputs.enabled_providers }}

REPOSITORY_ROOT: ${{ github.workspace }}

# Calculated config from other steps
HEAD_BRANCH: ${{ steps.branch.outputs.head }}
TARGET_BRANCH: ${{ steps.branch.outputs.target }}
Expand Down
2 changes: 1 addition & 1 deletion infrapatch/action/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def main(debug: bool):
raise Exception("No providers enabled. Please enable at least one provider.")

builder = ProviderHandlerBuilder(config.working_directory)
builder.with_git_integration()
builder.with_git_integration(config.repository_root)
if "terraform_modules" in config.enabled_providers or "terraform_providers" in config.enabled_providers:
builder.add_terraform_registry_configuration(config.default_registry_domain, config.terraform_registry_secrets)
if "terraform_modules" in config.enabled_providers:
Expand Down
2 changes: 1 addition & 1 deletion infrapatch/action/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self) -> None:
self.head_branch = _get_value_from_env("HEAD_BRANCH")
self.target_branch = _get_value_from_env("TARGET_BRANCH")
self.repository_name = _get_value_from_env("REPOSITORY_NAME")
self.repository_root = Path(os.getcwd())
self.repository_root = Path(_get_value_from_env("REPOSITORY_ROOT"))
self.enabled_providers = _get_value_from_env("ENABLED_PROVIDERS", default="").split(",")
self.working_directory = self.repository_root.joinpath(_get_value_from_env("WORKING_DIRECTORY_RELATIVE", default=""))
self.default_registry_domain = _get_value_from_env("DEFAULT_REGISTRY_DOMAIN")
Expand Down
7 changes: 4 additions & 3 deletions infrapatch/action/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ def test_action_config_init(working_directory_relative_path):
os.environ["HEAD_BRANCH"] = "main"
os.environ["TARGET_BRANCH"] = "develop"
os.environ["REPOSITORY_NAME"] = "my-repo"
os.environ["REPOSITORY_ROOT"] = "/repository/root"
os.environ["WORKING_DIRECTORY_RELATIVE"] = working_directory_relative_path
os.environ["DEFAULT_REGISTRY_DOMAIN"] = "registry.example.com"
os.environ["REGISTRY_SECRET_STRING"] = "test_registry.ch=abc123"
os.environ["TERRAFORM_REGISTRY_SECRET_STRING"] = "test_registry.ch=abc123"
os.environ["REPORT_ONLY"] = "False"

config = ActionConfigProvider()
Expand All @@ -64,8 +65,8 @@ def test_action_config_init(working_directory_relative_path):
assert config.head_branch == "main"
assert config.target_branch == "develop"
assert config.repository_name == "my-repo"
assert config.working_directory == Path(os.getcwd()).joinpath(working_directory_relative_path)
assert config.repository_root == Path(os.getcwd())
assert config.working_directory == config.repository_root.joinpath(working_directory_relative_path)
assert config.repository_root == Path("/repository/root")
assert config.default_registry_domain == "registry.example.com"
assert config.terraform_registry_secrets == {"test_registry.ch": "abc123"}
assert config.report_only is False
Expand Down
3 changes: 2 additions & 1 deletion infrapatch/core/provider_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging as log
from pathlib import Path
from typing import Sequence, Union
from rich import progress
from git import Repo
from pytablewriter import MarkdownTableWriter
from rich.console import Console
Expand Down Expand Up @@ -50,7 +51,7 @@ def upgrade_resources(self) -> bool:
return False
upgradable_resources = self.get_upgradable_resources()
for provider_name, resources in upgradable_resources.items():
for resource in resources:
for resource in progress.track(resources, description=f"Upgrading resources for Provider {self.providers[provider_name].get_provider_display_name()}..."):
try:
resource = self.providers[provider_name].patch_resource(resource)
except Exception as e:
Expand Down
10 changes: 4 additions & 6 deletions infrapatch/core/provider_handler_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, working_directory: Path) -> None:
self.providers = []
self.working_directory = working_directory
self.registry_handler = None
self.git_integration = False
self.git_repo = None
pass

def add_terraform_registry_configuration(self, default_registry_domain: str, credentials: dict[str, str]) -> Self:
Expand All @@ -45,16 +45,14 @@ def with_terraform_provider_provider(self) -> Self:
self.providers.append(tf_module_provider)
return self

def with_git_integration(self) -> Self:
def with_git_integration(self, git_working_directory: Path) -> Self:
log.debug("Enabling Git integration.")
self.git_integration = True
self.git_repo = Repo(git_working_directory)
return self

def build(self) -> ProviderHandler:
if len(self.providers) == 0:
raise Exception("No providers added to ProviderHandlerBuilder.")
statistics_file = self.working_directory.joinpath(f"{cs.APP_NAME}_Statistics.json")
git_repo = None
if self.git_integration:
git_repo = Repo(self.working_directory)
return ProviderHandler(providers=self.providers, console=Console(width=const.CLI_WIDTH), statistics_file=statistics_file, repo=git_repo)
return ProviderHandler(providers=self.providers, console=Console(width=const.CLI_WIDTH), statistics_file=statistics_file, repo=self.git_repo)
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def get_resources(self) -> Sequence[VersionedResource]:
return []

resources = []
for terraform_file in progress.track(terraform_files, description=f"Parsing .tf files for {self.get_provider_name()}..."):
for terraform_file in progress.track(terraform_files, description=f"Parsing .tf files for {self.get_provider_display_name()}..."):
if self.get_provider_name() == "terraform_modules":
resources.extend(self.hcl_handler.get_terraform_resources_from_file(terraform_file, get_modules=True, get_providers=False))

Expand All @@ -47,7 +47,7 @@ def get_resources(self) -> Sequence[VersionedResource]:
else:
raise Exception(f"Provider name '{self.get_provider_name()}' is not implemented.")

for resource in progress.track(resources, description="Getting newest resource versions..."):
for resource in progress.track(resources, description=f"Getting newest resource versions for Provider {self.get_provider_display_name()}..."):
resource.newest_version = self.registry_handler.get_newest_version(resource)
return resources

Expand Down

0 comments on commit cb9d633

Please sign in to comment.