|
| 1 | +# ******************************************************************************* |
| 2 | +# Copyright (c) 2026 Contributors to the Eclipse Foundation |
| 3 | +# |
| 4 | +# See the NOTICE file(s) distributed with this work for additional |
| 5 | +# information regarding copyright ownership. |
| 6 | +# |
| 7 | +# This program and the accompanying materials are made available under the |
| 8 | +# terms of the Apache License Version 2.0 which is available at |
| 9 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# SPDX-License-Identifier: Apache-2.0 |
| 12 | +# ******************************************************************************* |
| 13 | + |
| 14 | +""" |
| 15 | +Checkout all pinned repositories for CodeQL analysis. |
| 16 | +
|
| 17 | +Clones repositories directly from known_good.json using GitPython library |
| 18 | +with GitHub token authentication to avoid rate limits. |
| 19 | +""" |
| 20 | + |
| 21 | +import logging |
| 22 | +import os |
| 23 | +import sys |
| 24 | +from pathlib import Path |
| 25 | + |
| 26 | +from scripts.tooling.lib.git_operations import shallow_clone_repository |
| 27 | +from scripts.tooling.lib.known_good import load_known_good |
| 28 | + |
| 29 | +_LOG = logging.getLogger(__name__) |
| 30 | +logging.basicConfig(level=logging.INFO, format="%(message)s") |
| 31 | + |
| 32 | + |
| 33 | +def checkout_repo(name: str, url: str, ref: str, path: Path) -> None: |
| 34 | + """ |
| 35 | + Checkout a single repository using git_operations library. |
| 36 | +
|
| 37 | + Args: |
| 38 | + name: Repository name |
| 39 | + url: Repository URL |
| 40 | + ref: Git reference (branch, tag, or commit hash) |
| 41 | + path: Local path to checkout into |
| 42 | +
|
| 43 | + Raises: |
| 44 | + Exception: If checkout fails |
| 45 | + """ |
| 46 | + _LOG.info(f"Checking out {name} ({ref}) to {path}") |
| 47 | + |
| 48 | + shallow_clone_repository(url=url, path=path, ref=ref) |
| 49 | + |
| 50 | + |
| 51 | +def main() -> int: |
| 52 | + """Main entry point for standalone execution.""" |
| 53 | + # When running with bazel, use BUILD_WORKING_DIRECTORY to find workspace root |
| 54 | + workspace_root = Path(os.environ.get("BUILD_WORKING_DIRECTORY", ".")) |
| 55 | + known_good_path = workspace_root / "known_good.json" |
| 56 | + |
| 57 | + if not known_good_path.exists(): |
| 58 | + _LOG.error(f"known_good.json not found at {known_good_path}") |
| 59 | + return 1 |
| 60 | + |
| 61 | + try: |
| 62 | + known_good = load_known_good(known_good_path) |
| 63 | + except Exception as e: |
| 64 | + _LOG.error(f"Failed to parse known_good.json: {e}") |
| 65 | + return 1 |
| 66 | + |
| 67 | + # Extract target_sw modules |
| 68 | + modules = known_good.modules.get("target_sw", {}) |
| 69 | + repo_count = len(modules) |
| 70 | + |
| 71 | + _LOG.info(f"Checking out {repo_count} repositories from known_good.json...") |
| 72 | + |
| 73 | + # Track successfully checked out repositories |
| 74 | + repo_paths = [] |
| 75 | + |
| 76 | + # Checkout each repository |
| 77 | + for name, module in modules.items(): |
| 78 | + url = module.repo |
| 79 | + ref = module.version or module.branch or module.hash |
| 80 | + # Use workspace-relative path |
| 81 | + path = workspace_root / "repos" / name |
| 82 | + |
| 83 | + try: |
| 84 | + checkout_repo(name, url, ref, path) |
| 85 | + repo_paths.append(str(path)) |
| 86 | + except Exception as e: |
| 87 | + _LOG.error(f"Failed to checkout {name}: {e}") |
| 88 | + return 1 |
| 89 | + |
| 90 | + # Output all paths (comma-separated for GitHub Actions compatibility) |
| 91 | + repo_paths_output = ",".join(repo_paths) |
| 92 | + |
| 93 | + # Write to GITHUB_OUTPUT if available |
| 94 | + github_output = os.environ.get("GITHUB_OUTPUT") |
| 95 | + if github_output: |
| 96 | + try: |
| 97 | + with open(github_output, "a") as f: |
| 98 | + f.write(f"repo_paths={repo_paths_output}\n") |
| 99 | + except IOError as e: |
| 100 | + _LOG.warning(f"Failed to write GITHUB_OUTPUT: {e}") |
| 101 | + |
| 102 | + # Log summary |
| 103 | + _LOG.info(f"Successfully checked out {len(repo_paths)} of {repo_count} repositories") |
| 104 | + |
| 105 | + return 0 |
| 106 | + |
| 107 | + |
| 108 | +if __name__ == "__main__": |
| 109 | + sys.exit(main()) |
0 commit comments