Skip to content

Commit d99a94c

Browse files
committed
Add python script to upgrade CodeQL supported environment
1 parent c5bf50b commit d99a94c

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
certifi==2023.7.22
2+
charset-normalizer==3.2.0
3+
idna==3.4
4+
requests==2.31.0
5+
semantic-version==2.10.0
6+
urllib3==2.0.4
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
with SUPPORTED_VERSIONS_PATH.open("w") as f:
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+
json.dump(supported_versions, f, indent=2)
55+
except ValueError as e:
56+
print(e)
57+
exit(1)
58+
59+
if __name__ == '__main__':
60+
import sys
61+
import argparse
62+
import os
63+
64+
parser = argparse.ArgumentParser(description='Upgrade CodeQL dependencies')
65+
66+
parser.add_argument('--cli-version', type=str, required=True, help='CodeQL CLI version')
67+
parser.add_argument('--github-auth-stdin', action='store_true', help='CodeQL bundle version')
68+
69+
args = parser.parse_args()
70+
if args.github_auth_stdin:
71+
token = sys.stdin.read()
72+
else:
73+
if "GITHUB_TOKEN" not in os.environ:
74+
print("GITHUB_TOKEN environment variable not set")
75+
exit(1)
76+
token = os.environ["GITHUB_TOKEN"]
77+
78+
main(args.cli_version, token)
79+
80+
81+

0 commit comments

Comments
 (0)