Added support for project-name

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
Václav Valíček 2022-08-04 16:52:43 +02:00
parent 0e1d535ba0
commit de67130fd5
Signed by: valicek
GPG Key ID: FF05BDCA0C73BB31
6 changed files with 56 additions and 10 deletions

View File

@ -1,4 +1,5 @@
from .checksum import gen_repo_hashed_name from .checksum import gen_repo_hashed_name
from .cred_helper import prepare_git_auth
from .cloner_config import ClonerConfig, ClonerConfigParser from .cloner_config import ClonerConfig, ClonerConfigParser
from .config_file_not_found_error import ConfigFileNotFoundError from .config_file_not_found_error import ConfigFileNotFoundError
from .default_cloner_config import DefaultClonerConfig from .default_cloner_config import DefaultClonerConfig

View File

@ -66,6 +66,7 @@ class Cloner:
self._repo = RepoTool(repo_path) self._repo = RepoTool(repo_path)
return self.__opened return self.__opened
@property @property
def __opened(self) -> bool: def __opened(self) -> bool:
if not self._repo: if not self._repo:
@ -78,7 +79,7 @@ class Cloner:
return os.path.join(self._dirs.repos_dir, hashed_name) return os.path.join(self._dirs.repos_dir, hashed_name)
@property @property
def __main_repo_path(self) -> str: def main_repo_path(self) -> str:
return self._repo_path_by_url(self._config.cloner_repo_url) return self._repo_path_by_url(self._config.cloner_repo_url)
@classmethod @classmethod
@ -112,7 +113,7 @@ class Cloner:
def sync(self) -> bool: def sync(self) -> bool:
if not self.__opened: if not self.__opened:
self._repo = RepoTool(self.__main_repo_path) self._repo = RepoTool(self.main_repo_path)
if not self._repo.initialized: if not self._repo.initialized:
return False return False
# determine recursive behavior # determine recursive behavior
@ -158,7 +159,7 @@ class Cloner:
if url not in fetched_repos: if url not in fetched_repos:
everything_checked = False everything_checked = False
# generate new path # generate new path
directory = os.path.dirname(self.__main_repo_path) directory = os.path.dirname(self.main_repo_path)
submodule_cloner = RepoTool(os.path.join(directory, gen_repo_hashed_name(url))) submodule_cloner = RepoTool(os.path.join(directory, gen_repo_hashed_name(url)))
# clone or checkout? # clone or checkout?
if not submodule_cloner.initialized: if not submodule_cloner.initialized:
@ -216,7 +217,7 @@ class Cloner:
return os.path.exists(os.path.join(self._dirs.conf_dir, self.__detector_cfg)) return os.path.exists(os.path.join(self._dirs.conf_dir, self.__detector_cfg))
def detector_run(self, callback: Callable[[DetectedCommit], None]): def detector_run(self, callback: Callable[[DetectedCommit], None]):
detector = Detector(Path(self.__main_repo_path), Path(self._dirs.cache_dir), self._config.cloner_project_name) detector = Detector(Path(self.main_repo_path), Path(self._dirs.cache_dir), self._config.cloner_project_name)
if detector.check_fingerprint(): if detector.check_fingerprint():
log.debug(f"Starting detector discovery") log.debug(f"Starting detector discovery")
detector.run(callback) detector.run(callback)

View File

@ -0,0 +1,43 @@
from git import Repo, GitConfigParser
import logging
import os
log = logging.getLogger("rc.cfghelper")
def config_try_override(config_writer: GitConfigParser, section: str, option: str, value: str):
if section not in config_writer.sections():
log.debug(f"CFG Creating section: {section}")
config_writer.add_section(section)
if not config_writer.has_option(section, option):
log.debug(f"CFG Creating option: {option}")
config_writer.add_value(section, option, "")
log.debug(f"Setting {section}.{option} = {value}")
config_writer.set(section, option, value)
def prepare_git_auth(repo: str, config_dir):
log.debug(f"CFG: Opening repo {repo}")
repo = Repo(repo)
path: str = repo._get_config_path("user")
log.debug(f"CFG config path: {path}")
path = os.path.dirname(path)
log.debug(f"CFG parent path: {path}")
if not os.path.isdir(path):
log.debug(f"CFG Creating config dir")
os.mkdir(path)
cred_store: str = os.path.join(path, "git-credentials")
ssh_identity: str = os.path.join(config_dir, "ssh", "identity")
with repo.config_writer("user") as cfgw:
log.debug(f"Writing credential store setting")
config_try_override(cfgw, "credential", "helper", f"store --file={cred_store}")
log.debug(f"Writing SSH cert path")
config_try_override(
cfgw,
"core",
"sshcommand",
f"ssh -i {ssh_identity} -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o BatchMode=yes -q"
)

View File

@ -20,10 +20,11 @@ class DetectedCommit:
_log: str = "" _log: str = ""
_dict = {} _dict = {}
def __init__(self, env: dict): def __init__(self, env: dict, project_name: str):
for key in env.keys(): for key in env.keys():
self.__setattr__(f"_{key}", env[key]) self.__setattr__(f"_{key}", env[key])
self._dict = env self._dict = env
self.project = project_name
@property @property
def commit(self) -> str: def commit(self) -> str:
@ -232,7 +233,7 @@ class Detector:
'branches': special_branch, 'branches': special_branch,
'log': commit.message, 'log': commit.message,
} }
env = DetectedCommit(env) env = DetectedCommit(env, self._project)
log.info(f"Executing CI: commit {self._project}: {env.commit} @ {env.date} by {env.author}") log.info(f"Executing CI: commit {self._project}: {env.commit} @ {env.date} by {env.author}")
if env.is_tag or env.is_branch: if env.is_tag or env.is_branch:
log.info(f"Additional info: branch: {env.branches}; tag(s) {env.tags}") log.info(f"Additional info: branch: {env.branches}; tag(s) {env.tags}")

View File

@ -168,12 +168,12 @@ def test_repo_path_by_url(cloner_dir_struct: Path, path_repo_base: Path):
assert x == ds.repos_dir.joinpath("namespace_repo_3375634822.git").as_posix() assert x == ds.repos_dir.joinpath("namespace_repo_3375634822.git").as_posix()
def test__main_repo_path(cloner_dir_struct: Path, path_repo_base: Path): def test_main_repo_path(cloner_dir_struct: Path, path_repo_base: Path):
ds = MockDirStruct(cloner_dir_struct) ds = MockDirStruct(cloner_dir_struct)
ds.config.cloner_repo_url = path_repo_base.as_uri() ds.config.cloner_repo_url = path_repo_base.as_uri()
hashed = gen_repo_hashed_name(path_repo_base.as_uri()) hashed = gen_repo_hashed_name(path_repo_base.as_uri())
c = Cloner(ds) c = Cloner(ds)
x = c._Cloner__main_repo_path x = c.main_repo_path
assert x == ds.repos_dir.joinpath(hashed).as_posix() assert x == ds.repos_dir.joinpath(hashed).as_posix()

View File

@ -21,11 +21,11 @@ def det_dict():
@pytest.fixture @pytest.fixture
def det(det_dict): def det(det_dict):
return DetectedCommit(det_dict) return DetectedCommit(det_dict, "wut")
def test_detected_commit_dict(det_dict): def test_detected_commit_dict(det_dict):
det = DetectedCommit(det_dict) det = DetectedCommit(det_dict, "wut")
assert det.dict == det_dict assert det.dict == det_dict