Skip to content

Commit 8cfa9cf

Browse files
committed
Upgrade CodeQL dependencies now updates qlpack.yml files
The appropriate version of the `codeql/cpp-all` pack is identified by querying the qlpack.yml of the tag for the CodeQL version on github/codeql. This is then applied to all relevant qlpack.yml files in the repo, then codeql pack upgrade is used to update the lock files.
1 parent c4dafe7 commit 8cfa9cf

File tree

2 files changed

+41
-7
lines changed

2 files changed

+41
-7
lines changed

scripts/upgrade-codeql-dependencies/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ idna==3.4
44
requests==2.31.0
55
semantic-version==2.10.0
66
urllib3==1.26.18
7+
pyyaml==6.0.1

scripts/upgrade-codeql-dependencies/upgrade-codeql-dependencies.py

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
11
import json
22
import requests
3-
from typing import Optional, Dict, List
3+
from typing import Optional, Dict, List, Tuple
44
from semantic_version import Version
55
from pathlib import Path
6+
import yaml
67

78
SCRIPT_PATH = Path(__file__)
8-
SUPPORTED_VERSIONS_PATH = SCRIPT_PATH.parent.parent.parent / "supported_codeql_configs.json"
9+
CODING_STANDARDS_ROOT = SCRIPT_PATH.parent.parent.parent
10+
SUPPORTED_VERSIONS_PATH = CODING_STANDARDS_ROOT / "supported_codeql_configs.json"
911

10-
def get_compatible_stdlib(version: Version) -> Optional[str]:
12+
def get_compatible_stdlib(version: Version) -> Optional[Tuple[str, str]]:
1113
tag = f"codeql-cli/v{version}"
1214
response = requests.get(f"https://raw.githubusercontent.com/github/codeql/{tag}/cpp/ql/lib/qlpack.yml")
1315

1416
if response.status_code == 200:
15-
return tag
17+
# Parse the qlpack.yml returned in the response as a yaml file to read the version property
18+
qlpack = yaml.safe_load(response.text)
19+
if qlpack is not None and "version" in qlpack:
20+
return (tag, qlpack["version"])
1621
return None
1722

1823
def get_compatible_bundle(version: Version, token: str) -> Optional[str]:
@@ -30,15 +35,17 @@ def get_compatible_bundle(version: Version, token: str) -> Optional[str]:
3035
def main(cli_version : str, github_token: str) -> None:
3136
try:
3237
parsed_cli_version = Version(cli_version)
33-
compatible_stdlib = get_compatible_stdlib(parsed_cli_version)
34-
if compatible_stdlib is None:
38+
compatible_stdlib_return = get_compatible_stdlib(parsed_cli_version)
39+
if compatible_stdlib_return is None:
3540
print(f"Unable to find compatible standard library for: {parsed_cli_version}")
3641
exit(1)
3742
compatible_bundle = get_compatible_bundle(parsed_cli_version, github_token)
3843
if compatible_bundle is None:
3944
print(f"Unable to find compatible bundle for: {parsed_cli_version}")
4045
exit(1)
4146

47+
compatible_stdlib_tag, compatible_stdlib_version = compatible_stdlib_return
48+
4249
with SUPPORTED_VERSIONS_PATH.open("r") as f:
4350
supported_versions = json.load(f)
4451

@@ -49,10 +56,36 @@ def main(cli_version : str, github_token: str) -> None:
4956
supported_env = supported_envs[0]
5057
supported_env["codeql_cli"] = str(parsed_cli_version)
5158
supported_env["codeql_cli_bundle"] = compatible_bundle
52-
supported_env["codeql_standard_library"] = compatible_stdlib
59+
supported_env["codeql_standard_library"] = compatible_stdlib_tag
5360

5461
with SUPPORTED_VERSIONS_PATH.open("w") as f:
5562
json.dump(supported_versions, f, indent=2)
63+
64+
# Find every qlpack.yml file in the repository
65+
qlpack_files = list(CODING_STANDARDS_ROOT.rglob("qlpack.yml"))
66+
# Filter out any files that are in a hidden directory
67+
qlpack_files = [f for f in qlpack_files if not any(part for part in f.parts if part.startswith("."))]
68+
69+
# Update the "codeql/cpp-all" entries in the "dependencies" property in every qlpack.yml file
70+
updated_qlpacks = []
71+
for qlpack_file in qlpack_files:
72+
with qlpack_file.open("r") as f:
73+
qlpack = yaml.safe_load(f)
74+
print("Updating dependencies in " + str(qlpack_file))
75+
if "codeql/cpp-all" in qlpack["dependencies"]:
76+
qlpack["dependencies"]["codeql/cpp-all"] = compatible_stdlib_version
77+
with qlpack_file.open("w") as f:
78+
yaml.safe_dump(qlpack, f)
79+
updated_qlpacks.append(qlpack_file.parent)
80+
81+
# Call CodeQL to update the lock files by running codeql pack upgrade
82+
# Note: we need to do this after updating all the qlpack files,
83+
# otherwise we may get dependency resolution errors
84+
for qlpack in updated_qlpacks:
85+
print("Updating lock files for " + str(qlpack))
86+
os.system(f"codeql pack upgrade {qlpack}")
87+
88+
5689
except ValueError as e:
5790
print(e)
5891
exit(1)

0 commit comments

Comments
 (0)