|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# ******************************************************************************* |
| 3 | +# Copyright (c) 2026 Qorix |
| 4 | +# |
| 5 | +# See the NOTICE file(s) distributed with this work for additional |
| 6 | +# information regarding copyright ownership. |
| 7 | +# |
| 8 | +# This program and the accompanying materials are made available under the |
| 9 | +# terms of the Apache License Version 2.0 which is available at |
| 10 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# SPDX-License-Identifier: Apache-2.0 |
| 13 | +# ******************************************************************************* |
| 14 | +""" |
| 15 | +Checkout all pinned repositories for CodeQL analysis. |
| 16 | +
|
| 17 | +Uses scripts.tooling.lib.git_operations for repository checkout operations |
| 18 | +with GitHub API authentication to avoid rate limits. |
| 19 | +""" |
| 20 | + |
| 21 | +import json |
| 22 | +import logging |
| 23 | +import os |
| 24 | +import sys |
| 25 | +from pathlib import Path |
| 26 | + |
| 27 | +# Add repository root to path for module imports |
| 28 | +_REPO_ROOT = Path(__file__).resolve().parent.parent.parent.parent.parent |
| 29 | +if str(_REPO_ROOT) not in sys.path: |
| 30 | + sys.path.insert(0, str(_REPO_ROOT)) |
| 31 | +from scripts.tooling.lib.git_operations import shallow_clone_repository |
| 32 | + |
| 33 | +_LOG = logging.getLogger(__name__) |
| 34 | +logging.basicConfig(level=logging.INFO, format="%(message)s") |
| 35 | + |
| 36 | + |
| 37 | +def load_repos_config(config_file: str = "./repos.json") -> list[dict]: |
| 38 | + """ |
| 39 | + Load repository configuration from repos.json. |
| 40 | +
|
| 41 | + Args: |
| 42 | + config_file: Path to repos.json file |
| 43 | +
|
| 44 | + Returns: |
| 45 | + List of repository configurations |
| 46 | +
|
| 47 | + Raises: |
| 48 | + SystemExit: If file not found or invalid JSON |
| 49 | + """ |
| 50 | + config_path = Path(config_file) |
| 51 | + |
| 52 | + if not config_path.exists(): |
| 53 | + _LOG.error(f"Error: file not found '{config_file}'") |
| 54 | + sys.exit(1) |
| 55 | + |
| 56 | + try: |
| 57 | + with open(config_path, "r") as f: |
| 58 | + repos = json.load(f) |
| 59 | + return repos |
| 60 | + except (json.JSONDecodeError, IOError) as e: |
| 61 | + _LOG.error(f"Error: Failed to load repos.json: {e}") |
| 62 | + sys.exit(1) |
| 63 | + |
| 64 | + |
| 65 | +def checkout_repo(name: str, url: str, ref: str, path: str) -> bool: |
| 66 | + """ |
| 67 | + Checkout a single repository using git_operations library. |
| 68 | +
|
| 69 | + Args: |
| 70 | + name: Repository name |
| 71 | + url: Repository URL |
| 72 | + ref: Git reference (branch, tag, or commit hash) |
| 73 | + path: Local path to checkout into |
| 74 | +
|
| 75 | + Returns: |
| 76 | + True if successful, False otherwise |
| 77 | + """ |
| 78 | + path_obj = Path(path) |
| 79 | + |
| 80 | + _LOG.info(f"Checking out {name} ({ref}) to {path}") |
| 81 | + |
| 82 | + return shallow_clone_repository(url=url, path=path_obj, ref=ref) |
| 83 | + |
| 84 | + |
| 85 | +def main() -> int: |
| 86 | + """Main entry point for standalone execution.""" |
| 87 | + # Load repository configurations |
| 88 | + repos = load_repos_config("./repos.json") |
| 89 | + repo_count = len(repos) |
| 90 | + |
| 91 | + _LOG.info(f"Checking out {repo_count} repositories...") |
| 92 | + |
| 93 | + # Track successfully checked out repositories |
| 94 | + repo_paths = [] |
| 95 | + |
| 96 | + # Checkout each repository |
| 97 | + for i, repo in enumerate(repos): |
| 98 | + name = repo.get("name", f"repo-{i}") |
| 99 | + url = repo.get("url", "") |
| 100 | + ref = repo.get("version", "") |
| 101 | + path = repo.get("path", "") |
| 102 | + |
| 103 | + if not all([url, ref, path]): |
| 104 | + _LOG.warning(f"Skipping {name} - missing required fields") |
| 105 | + continue |
| 106 | + |
| 107 | + if checkout_repo(name, url, ref, path): |
| 108 | + repo_paths.append(path) |
| 109 | + else: |
| 110 | + _LOG.error(f"Failed to checkout {name}") |
| 111 | + |
| 112 | + # Output all paths (comma-separated for GitHub Actions compatibility) |
| 113 | + repo_paths_output = ",".join(repo_paths) |
| 114 | + |
| 115 | + # Write to GITHUB_OUTPUT if available |
| 116 | + github_output = os.environ.get("GITHUB_OUTPUT") |
| 117 | + if github_output: |
| 118 | + try: |
| 119 | + with open(github_output, "a") as f: |
| 120 | + f.write(f"repo_paths={repo_paths_output}\n") |
| 121 | + except IOError as e: |
| 122 | + _LOG.warning(f"Failed to write GITHUB_OUTPUT: {e}") |
| 123 | + |
| 124 | + # Print summary |
| 125 | + _LOG.info(f"\nSuccessfully checked out {len(repo_paths)} of {repo_count} repositories") |
| 126 | + _LOG.info(f"repo_paths={repo_paths_output}") |
| 127 | + |
| 128 | + return 0 if len(repo_paths) == repo_count else 1 |
| 129 | + |
| 130 | + |
| 131 | +if __name__ == "__main__": |
| 132 | + sys.exit(main()) |
0 commit comments