Skip to content

Commit 1d72c6e

Browse files
committed
config: allow to override pulse parameter via env
1 parent 3dcf382 commit 1d72c6e

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@ $ echo '{"payload": {"type": "push", "repo_url": "bla", "branches": [ "main" ],
4242
The RabbitMQ management interface is available at http://localhost:15672/, and
4343
the login and password are `guest` (don't do this at home, or in prod).
4444

45+
## Configuration
46+
47+
An example configuration file can be found in `config.toml.example`. A different
48+
configuration file can be specified with the `--config` (`-c`) option.
49+
50+
In addition, Pulse parameters can be overridden via the following environment
51+
variables:
52+
53+
- PULSE_EXCHANGE
54+
- PULSE_HOST
55+
- PULSE_PASSWORD
56+
- PULSE_PORT (needs to be an integer)
57+
- PULSE_QUEUE
58+
- PULSE_ROUTING_KEY
59+
- PULSE_SSL (needs to be an empty string to be False, otherwise True)
60+
- PULSE_USERID
61+
4562
## Build and test
4663

4764
Format and test/lint code:

git_hg_sync/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ def start_app(
5353

5454
queue = get_queue(pulse_config)
5555

56+
logger.info(f"Reading messages from {connection}/{queue.name} ...")
57+
5658
synchronizers = {
5759
tracked_repo.url: RepoSynchronizer(
5860
config.clones.directory / tracked_repo.name, tracked_repo.url

git_hg_sync/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import pathlib
23
from typing import Self
34

@@ -43,6 +44,12 @@ class Config(pydantic.BaseModel):
4344
def verify_all_mappings_reference_tracked_repositories(
4445
self,
4546
) -> Self:
47+
# Allow to override Pulse parameters via environment.
48+
for config in self.pulse.model_fields:
49+
env_var = f"PULSE_{config}".upper()
50+
if value := os.getenv(env_var):
51+
setattr(self.pulse, config, value)
52+
4653
tracked_urls = [tracked_repo.url for tracked_repo in self.tracked_repositories]
4754
for mapping in self.branch_mappings:
4855
if mapping.source_url not in tracked_urls:

tests/test_config.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from pathlib import Path
22

3+
from pytest import MonkeyPatch
4+
35
from git_hg_sync.config import Config
46

57
HERE = Path(__file__).parent
@@ -8,4 +10,28 @@
810
def test_load_config() -> None:
911
config = Config.from_file(HERE / "data" / "config.toml")
1012
assert not config.pulse.ssl
13+
assert config.pulse.host == "pulse"
1114
assert config.branch_mappings[0].destination_branch == "default"
15+
16+
17+
def test_load_config_env_override(monkeypatch: MonkeyPatch) -> None:
18+
pulse_env = {
19+
"exchange": "overridden exchange",
20+
"host": "overridden host",
21+
"password": "overridden password",
22+
"port": "overridden port",
23+
"queue": "overridden queue",
24+
"routing_key": "overridden routing_key",
25+
"ssl": "false",
26+
"userid": "overridden userid",
27+
}
28+
29+
for key in pulse_env: # noqa: PLC0206 We want the key only here...
30+
monkeypatch.setenv(f"PULSE_{key}".upper(), pulse_env[key])
31+
32+
config = Config.from_file(HERE / "data" / "config.toml")
33+
34+
for key in pulse_env: # noqa: PLC0206
35+
assert getattr(config.pulse, key) == pulse_env[key], (
36+
f"Pulse configuration not overridden for {key}"
37+
)

0 commit comments

Comments
 (0)