Added support for project-name
Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
parent
0e1d535ba0
commit
de67130fd5
|
@ -1,4 +1,5 @@
|
|||
from .checksum import gen_repo_hashed_name
|
||||
from .cred_helper import prepare_git_auth
|
||||
from .cloner_config import ClonerConfig, ClonerConfigParser
|
||||
from .config_file_not_found_error import ConfigFileNotFoundError
|
||||
from .default_cloner_config import DefaultClonerConfig
|
||||
|
|
|
@ -66,6 +66,7 @@ class Cloner:
|
|||
self._repo = RepoTool(repo_path)
|
||||
return self.__opened
|
||||
|
||||
|
||||
@property
|
||||
def __opened(self) -> bool:
|
||||
if not self._repo:
|
||||
|
@ -78,7 +79,7 @@ class Cloner:
|
|||
return os.path.join(self._dirs.repos_dir, hashed_name)
|
||||
|
||||
@property
|
||||
def __main_repo_path(self) -> str:
|
||||
def main_repo_path(self) -> str:
|
||||
return self._repo_path_by_url(self._config.cloner_repo_url)
|
||||
|
||||
@classmethod
|
||||
|
@ -112,7 +113,7 @@ class Cloner:
|
|||
def sync(self) -> bool:
|
||||
|
||||
if not self.__opened:
|
||||
self._repo = RepoTool(self.__main_repo_path)
|
||||
self._repo = RepoTool(self.main_repo_path)
|
||||
if not self._repo.initialized:
|
||||
return False
|
||||
# determine recursive behavior
|
||||
|
@ -158,7 +159,7 @@ class Cloner:
|
|||
if url not in fetched_repos:
|
||||
everything_checked = False
|
||||
# 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)))
|
||||
# clone or checkout?
|
||||
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))
|
||||
|
||||
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():
|
||||
log.debug(f"Starting detector discovery")
|
||||
detector.run(callback)
|
||||
|
|
43
repo_cloner/lib/cred_helper.py
Normal file
43
repo_cloner/lib/cred_helper.py
Normal 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"
|
||||
)
|
|
@ -20,10 +20,11 @@ class DetectedCommit:
|
|||
_log: str = ""
|
||||
_dict = {}
|
||||
|
||||
def __init__(self, env: dict):
|
||||
def __init__(self, env: dict, project_name: str):
|
||||
for key in env.keys():
|
||||
self.__setattr__(f"_{key}", env[key])
|
||||
self._dict = env
|
||||
self.project = project_name
|
||||
|
||||
@property
|
||||
def commit(self) -> str:
|
||||
|
@ -232,7 +233,7 @@ class Detector:
|
|||
'branches': special_branch,
|
||||
'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}")
|
||||
if env.is_tag or env.is_branch:
|
||||
log.info(f"Additional info: branch: {env.branches}; tag(s) {env.tags}")
|
||||
|
|
|
@ -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()
|
||||
|
||||
|
||||
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.config.cloner_repo_url = path_repo_base.as_uri()
|
||||
hashed = gen_repo_hashed_name(path_repo_base.as_uri())
|
||||
c = Cloner(ds)
|
||||
x = c._Cloner__main_repo_path
|
||||
x = c.main_repo_path
|
||||
assert x == ds.repos_dir.joinpath(hashed).as_posix()
|
||||
|
||||
|
||||
|
|
|
@ -21,11 +21,11 @@ def det_dict():
|
|||
|
||||
@pytest.fixture
|
||||
def det(det_dict):
|
||||
return DetectedCommit(det_dict)
|
||||
return DetectedCommit(det_dict, "wut")
|
||||
|
||||
|
||||
def test_detected_commit_dict(det_dict):
|
||||
det = DetectedCommit(det_dict)
|
||||
det = DetectedCommit(det_dict, "wut")
|
||||
assert det.dict == det_dict
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user