Skip to content

Commit

Permalink
config: allow to override pulse parameter via env
Browse files Browse the repository at this point in the history
  • Loading branch information
shtrom committed Feb 5, 2025
1 parent 3dcf382 commit 3babf19
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions git_hg_sync/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions git_hg_sync/config.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import pathlib
from typing import Self

Expand Down Expand Up @@ -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:
Expand Down
26 changes: 26 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pathlib import Path

from pytest import MonkeyPatch

from git_hg_sync.config import Config

HERE = Path(__file__).parent
Expand All @@ -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}"

0 comments on commit 3babf19

Please sign in to comment.