|
| 1 | +import json |
| 2 | +import requests |
| 3 | +from typing import Optional, Dict, List |
| 4 | +from semantic_version import Version |
| 5 | +from pathlib import Path |
| 6 | + |
| 7 | +SCRIPT_PATH = Path(__file__) |
| 8 | +SUPPORTED_VERSIONS_PATH = SCRIPT_PATH.parent.parent.parent / "supported_codeql_configs.json" |
| 9 | + |
| 10 | +def get_compatible_stdlib(version: Version) -> Optional[str]: |
| 11 | + tag = f"codeql-cli/v{version}" |
| 12 | + response = requests.get(f"https://raw.githubusercontent.com/github/codeql/{tag}/cpp/ql/lib/qlpack.yml") |
| 13 | + |
| 14 | + if response.status_code == 200: |
| 15 | + return tag |
| 16 | + return None |
| 17 | + |
| 18 | +def get_compatible_bundle(version: Version, token: str) -> Optional[str]: |
| 19 | + tag = f"codeql-bundle-v{version}" |
| 20 | + response = requests.get(f"https://api.github.com/repos/github/codeql-action/releases/tags/{tag}", headers={ |
| 21 | + "Accept": "application/vnd.github+json", |
| 22 | + "Authorization": f"Bearer {token}", |
| 23 | + "X-GitHub-Api-Version": "2022-11-28" |
| 24 | + }) |
| 25 | + |
| 26 | + if response.status_code == 200: |
| 27 | + return tag |
| 28 | + return None |
| 29 | + |
| 30 | +def main(cli_version : str, github_token: str) -> None: |
| 31 | + try: |
| 32 | + parsed_cli_version = Version(cli_version) |
| 33 | + compatible_stdlib = get_compatible_stdlib(parsed_cli_version) |
| 34 | + if compatible_stdlib is None: |
| 35 | + print(f"Unable to find compatible standard library for: {parsed_cli_version}") |
| 36 | + exit(1) |
| 37 | + compatible_bundle = get_compatible_bundle(parsed_cli_version, github_token) |
| 38 | + if compatible_bundle is None: |
| 39 | + print(f"Unable to find compatible bundle for: {parsed_cli_version}") |
| 40 | + exit(1) |
| 41 | + |
| 42 | + with SUPPORTED_VERSIONS_PATH.open("r") as f: |
| 43 | + supported_versions = json.load(f) |
| 44 | + |
| 45 | + supported_envs: List[Dict[str, str]] = supported_versions["supported_environment"] |
| 46 | + if len(supported_envs) != 1: |
| 47 | + print("Expected exactly one supported environment, cannot upgrade!") |
| 48 | + exit(1) |
| 49 | + supported_env = supported_envs[0] |
| 50 | + supported_env["codeql_cli"] = str(parsed_cli_version) |
| 51 | + supported_env["codeql_cli_bundle"] = compatible_bundle |
| 52 | + supported_env["codeql_standard_library"] = compatible_stdlib |
| 53 | + |
| 54 | + with SUPPORTED_VERSIONS_PATH.open("w") as f: |
| 55 | + json.dump(supported_versions, f, indent=2) |
| 56 | + except ValueError as e: |
| 57 | + print(e) |
| 58 | + exit(1) |
| 59 | + |
| 60 | +if __name__ == '__main__': |
| 61 | + import sys |
| 62 | + import argparse |
| 63 | + import os |
| 64 | + |
| 65 | + parser = argparse.ArgumentParser(description='Upgrade CodeQL dependencies') |
| 66 | + |
| 67 | + parser.add_argument('--cli-version', type=str, required=True, help='CodeQL CLI version') |
| 68 | + parser.add_argument('--github-auth-stdin', action='store_true', help='Authenticate to the GitHub API by providing a GitHub token via standard input.') |
| 69 | + |
| 70 | + args = parser.parse_args() |
| 71 | + if args.github_auth_stdin: |
| 72 | + token = sys.stdin.read() |
| 73 | + else: |
| 74 | + if "GITHUB_TOKEN" not in os.environ: |
| 75 | + print("GITHUB_TOKEN environment variable not set") |
| 76 | + exit(1) |
| 77 | + token = os.environ["GITHUB_TOKEN"] |
| 78 | + |
| 79 | + main(args.cli_version, token) |
| 80 | + |
| 81 | + |
| 82 | + |
0 commit comments