Skip to content
20 changes: 20 additions & 0 deletions backend/infrahub/config.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import os
import re
import ssl
import sys
import tomllib
Expand Down Expand Up @@ -444,6 +445,25 @@ class GitSettings(BaseSettings):
default_factory=default_append_git_suffix_domains,
description="Automatically append '.git' to HTTP URLs if for these domains.",
)
import_sync_branch_names: list[str] = Field(
default_factory=list,
description=(
"Names or regex of branches to be created in infrahub during import "
"e.g. 'infrahub/.*', 'release/.*', '^branch-'. "
"Note: other branches created with sync with git will be imported also"
),
)

@model_validator(mode="after")
def validate_sync_branch_names(self) -> Self:
for branch_filter in self.import_sync_branch_names:
try:
re.compile(branch_filter)
except re.error as exc:
raise ValueError(
f"Invalid regex pattern for import_sync_branch_names: '{branch_filter}' — {exc}"
) from exc
return self


class HTTPSettings(BaseSettings):
Expand Down
16 changes: 15 additions & 1 deletion backend/tests/unit/test_config.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from infrahub.config import UserInfoMethod, load
import pytest

from infrahub.config import GitSettings, UserInfoMethod, load
from tests.conftest import TestHelper


Expand All @@ -19,3 +21,15 @@ def test_load_sso_config(helper: TestHelper) -> None:
assert oauth_provider2.userinfo_method == UserInfoMethod.GET
assert oidc_provider1.userinfo_method == UserInfoMethod.POST
assert oidc_provider2.userinfo_method == UserInfoMethod.GET


def test_valid_git_settings__sync_branch_names():
import_sync_branch_names = ["main", "infrahub/.*", "release/.*"]
git_settings = GitSettings(import_sync_branch_names=import_sync_branch_names)
assert git_settings.import_sync_branch_names == import_sync_branch_names


def test_invalid_git_settings__sync_branch_names():
with pytest.raises(ValueError) as exc:
GitSettings(import_sync_branch_names=["main", "infrahub/.*", "release/.*", "a[b"])
assert "Invalid regex pattern for import_sync_branch_names: 'a[b'" in str(exc.value)
1 change: 1 addition & 0 deletions docs/docs/reference/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Here are a few common methods of setting environmental variables:
| `INFRAHUB_GIT_REPOSITORIES_DIRECTORY` | None | string | repositories |
| `INFRAHUB_GIT_SYNC_INTERVAL` | Time (in seconds) between git repositories synchronizations | integer | 10 |
| `INFRAHUB_GIT_APPEND_GIT_SUFFIX` | Automatically append '.git' to HTTP URLs if for these domains. | array[string] | None |
| `INFRAHUB_GIT_IMPORT_SYNC_BRANCH_NAMES` | Names or regex of branches to be created in infrahub during import e.g. 'infrahub/.*', 'release/.*', '^branch-'. Note: other branches created with sync with git will be imported also | array[string] | None |

## Dev

Expand Down