diff --git a/README.md b/README.md index a6436da..816f9f9 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,23 @@ $ echo '{"payload": {"type": "push", "repo_url": "bla", "branches": [ "main" ], The RabbitMQ management interface is available at http://localhost:15672/, and the login and password are `guest` (don't do this at home, or in prod). +## Configuration + +An example configuration file can be found in `config.toml.example`. A different +configuration file can be specified with the `--config` (`-c`) option. + +In addition, Pulse parameters can be overridden via the following environment +variables: + +- PULSE_EXCHANGE +- PULSE_HOST +- PULSE_PASSWORD +- PULSE_PORT (needs to be an integer) +- PULSE_QUEUE +- PULSE_ROUTING_KEY +- PULSE_SSL (needs to be an empty string to be False, otherwise True) +- PULSE_USERID + ## Build and test Format and test/lint code: diff --git a/git_hg_sync/__main__.py b/git_hg_sync/__main__.py index 5e7bea0..e470eee 100644 --- a/git_hg_sync/__main__.py +++ b/git_hg_sync/__main__.py @@ -53,6 +53,8 @@ def start_app( queue = get_queue(pulse_config) + logger.info(f"Reading messages from {connection}/{queue.name} ...") + synchronizers = { tracked_repo.url: RepoSynchronizer( config.clones.directory / tracked_repo.name, tracked_repo.url diff --git a/git_hg_sync/config.py b/git_hg_sync/config.py index 04d1f91..5ddb6de 100644 --- a/git_hg_sync/config.py +++ b/git_hg_sync/config.py @@ -1,3 +1,4 @@ +import os import pathlib from typing import Self @@ -43,6 +44,12 @@ class Config(pydantic.BaseModel): def verify_all_mappings_reference_tracked_repositories( self, ) -> Self: + # Allow to override Pulse parameters via environment. + for config in self.pulse.model_fields: + env_var = f"PULSE_{config}".upper() + if value := os.getenv(env_var): + setattr(self.pulse, config, value) + tracked_urls = [tracked_repo.url for tracked_repo in self.tracked_repositories] for mapping in self.branch_mappings: if mapping.source_url not in tracked_urls: diff --git a/tests/test_config.py b/tests/test_config.py index e351d16..f620300 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -1,5 +1,7 @@ from pathlib import Path +from pytest import MonkeyPatch + from git_hg_sync.config import Config HERE = Path(__file__).parent @@ -8,4 +10,28 @@ def test_load_config() -> None: config = Config.from_file(HERE / "data" / "config.toml") assert not config.pulse.ssl + assert config.pulse.host == "pulse" assert config.branch_mappings[0].destination_branch == "default" + + +def test_load_config_env_override(monkeypatch: MonkeyPatch) -> None: + pulse_env = { + "exchange": "overridden exchange", + "host": "overridden host", + "password": "overridden password", + "port": "overridden port", + "queue": "overridden queue", + "routing_key": "overridden routing_key", + "ssl": "false", + "userid": "overridden userid", + } + + for key in pulse_env: # noqa: PLC0206 We want the key only here... + monkeypatch.setenv(f"PULSE_{key}".upper(), pulse_env[key]) + + config = Config.from_file(HERE / "data" / "config.toml") + + for key in pulse_env: # noqa: PLC0206 + assert ( + getattr(config.pulse, key) == pulse_env[key] + ), f"Pulse configuration not overridden for {key}"