FAQ | This is a LIVE service | Changelog

Skip to content
Snippets Groups Projects
Commit 677a904d authored by Robin Goodall's avatar Robin Goodall :speech_balloon:
Browse files

Add optional token argument

parent 24a202b3
No related branches found
No related tags found
1 merge request!21Script to pull specs and optionally commit them
......@@ -9,6 +9,7 @@ from dataclasses import dataclass
import git
import os
import shutil
from urllib.parse import quote
LOG = logging.getLogger(Path(__file__).stem)
......@@ -37,17 +38,23 @@ def load_config(source: str) -> dict[str, SpecConfig]:
return spec_configs
def pull_specs(config: dict[str, SpecConfig]) -> set[str]:
def pull_specs(config: dict[str, SpecConfig], token: Optional[str]) -> set[str]:
spec_files = set()
for name, c in config.items():
LOG.info(f'{name}:')
LOG.info(f'...cloning from {c.repo} @ {c.branch}')
repo = c.repo
if token:
# Token given so insert in to url, default to using USER env for username as
# this is needed for personal access tokens and ignored for project access tokens
creds = quote(os.getenv('USER', 'token')) + ':' + quote(token) + '@'
repo = repo.replace('https://', f'https://{creds}')
LOG.info(f"...cloning from {c.repo}{' (using token)' if token else ''} @ {c.branch}")
with tempfile.TemporaryDirectory() as tmp_dirname:
# Clone repo@branch in to temp directory without checkout then checkout
# just the file we want before copying it into this repo and recording
# the SHA of the last commit to affect it
repo = git.Repo.clone_from(
url=c.repo,
url=repo,
to_path=tmp_dirname,
branch=c.branch,
no_checkout=True,
......@@ -93,6 +100,7 @@ def commit_specs(spec_files: set[str]):
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument('--configuration', '-c', type=str, default='sources.yaml')
parser.add_argument('--token', '-t', type=str)
parser.add_argument('--commit', action='store_true')
parser.add_argument('--debug', action='store_true')
return parser.parse_args()
......@@ -102,6 +110,6 @@ if __name__ == "__main__":
args = parse_args()
logging.basicConfig(level=logging.DEBUG if args.debug else logging.INFO)
config = load_config(args.configuration)
spec_files = pull_specs(config)
spec_files = pull_specs(config, args.token)
if args.commit:
commit_specs(spec_files)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment