Skip to content

Commit 5c01ac2

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

File tree

4 files changed

+50
-0
lines changed

4 files changed

+50
-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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,28 @@
88
def test_load_config() -> None:
99
config = Config.from_file(HERE / "data" / "config.toml")
1010
assert not config.pulse.ssl
11+
assert config.pulse.host == "pulse"
1112
assert config.branch_mappings[0].destination_branch == "default"
13+
14+
15+
def test_load_config_env_override(monkeypatch) -> None:
16+
pulse_env = {
17+
"exchange": "overridden exchange",
18+
"host": "overridden host",
19+
"password": "overridden password",
20+
"port": "overridden port",
21+
"queue": "overridden queue",
22+
"routing_key": "overridden routing_key",
23+
"ssl": "false",
24+
"userid": "overridden userid",
25+
}
26+
27+
for key in pulse_env: # noqa: PLC0206 We want the key only here...
28+
monkeypatch.setenv(f"PULSE_{key}".upper(), pulse_env[key])
29+
30+
config = Config.from_file(HERE / "data" / "config.toml")
31+
32+
for key in pulse_env: # noqa: PLC0206
33+
assert (
34+
getattr(config.pulse, key) == pulse_env[key]
35+
), f"Pulse configuration not overridden for {key}"

0 commit comments

Comments
 (0)