Cred helper: load token, token support. RepoTool: timeout

Signed-off-by: Václav Valíček <valicek1994@gmail.com>
This commit is contained in:
2022-08-05 23:05:33 +02:00
parent de67130fd5
commit b3b9fbf0c8
4 changed files with 147 additions and 2 deletions

View File

@@ -1,9 +1,40 @@
from git import Repo, GitConfigParser
import logging
import os
from typing import Optional, List
from pathlib import Path
log = logging.getLogger("rc.cfghelper")
token: Optional[str] = None
def gen_gh_token_candidates() -> List[Path]:
return [
Path(os.getcwd()).joinpath(".gh-token"),
Path(os.getenv("HOME")).joinpath(".config", "gh-token"),
Path("/etc").joinpath("cloner-gh-token"),
]
def init_gh_token():
for candidate in gen_gh_token_candidates():
log.debug(f"Loading gh-candidate candidate {candidate.as_posix()}")
load_gh_token(candidate)
if token:
log.info(f"Token succesfully loaded")
break
def load_gh_token(path: Path):
global token
log.info(f"Loading secret github token")
if not path.is_file():
log.warning(f"Token load did not pass - file not found")
return
# load token
token = path.read_text().strip()
def config_try_override(config_writer: GitConfigParser, section: str, option: str, value: str):
if section not in config_writer.sections():
@@ -32,8 +63,20 @@ def prepare_git_auth(repo: str, config_dir):
ssh_identity: str = os.path.join(config_dir, "ssh", "identity")
with repo.config_writer("user") as cfgw:
# github personal token
# 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
if token:
config_try_override(
cfgw,
f"url \"https://{token}:x-oauth-basic@github.com/\"",
"insteadOf",
"https://github.com/"
)
# https credential store
log.debug(f"Writing credential store setting")
config_try_override(cfgw, "credential", "helper", f"store --file={cred_store}")
# ssh key
log.debug(f"Writing SSH cert path")
config_try_override(
cfgw,