Skip to content

Commit 7c50f5b

Browse files
authored
Merge pull request #346 from rvermeulen/rvermeulen/incomplete-codeql-dep-upgrade
Address incomplete CodeQL dependency upgrade workflow
2 parents 9a9927c + 1c676b3 commit 7c50f5b

File tree

3 files changed

+101
-12
lines changed

3 files changed

+101
-12
lines changed

.github/workflows/upgrade_codeql_dependencies.yml

+13-12
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ on:
77
description: |
88
The version of the CodeQL CLI to be set as the default.
99
required: true
10-
codeql_standard_library_commit:
11-
description: |
12-
The tag or commit to use from the CodeQL Standard Library
13-
required: true
1410

1511
env:
1612
XARGS_MAX_PROCS: 4
@@ -19,20 +15,25 @@ jobs:
1915
say_hello:
2016
env:
2117
CODEQL_CLI_VERSION: ${{ github.event.inputs.codeql_cli_version }}
22-
CODEQL_LIB_COMMIT: ${{ github.event.inputs.codeql_standard_library_commit }}
2318
runs-on: ubuntu-22.04
2419
steps:
2520
- name: Checkout
2621
uses: actions/checkout@v2
2722

23+
- name: Install Python
24+
uses: actions/setup-python@v4
25+
with:
26+
python-version: "3.9"
27+
28+
- name: Install upgrade-codeql-dependencies.py dependencies
29+
run: pip install -r scripts/upgrade-codeql-dependencies/requirements.txt
30+
2831
- name: Update the supported environment
32+
env:
33+
GITHUB_TOKEN: ${{ github.token }}
34+
CODEQL_CLI_VERSION: ${{ github.event.inputs.codeql_cli_version }}
2935
run: |
30-
jq \
31-
--arg cli_version "$CODEQL_CLI_VERSION" \
32-
--arg standard_library_commit "$CODEQL_LIB_COMMIT" \
33-
--raw-output \
34-
'.supported_environment | .[0] | .codeql_cli = $cli_version | .codeql_standard_library = $standard_library_commit' \
35-
supported_codeql_configs.json
36+
scripts/upgrade-codeql-dependencies/upgrade_codeql_dependencies.py --cli-version "$CODEQL_CLI_VERSION"
3637
3738
- name: Fetch CodeQL
3839
env:
@@ -54,4 +55,4 @@ jobs:
5455
commit-message: "Upgrading `github/codeql` dependency to ${{ github.event.inputs.codeql_standard_library_commit }}"
5556
team-reviewers: github/codeql-coding-standards
5657
delete-branch: true
57-
branch: "codeql/upgrade-to-${{ github.event.inputs.codeql_standard_library_commit }}-${{ github.event.inputs.codeql_cli_version }}"
58+
branch: "codeql/upgrade-to-${{ github.event.inputs.codeql_cli_version }}"
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
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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

Comments
 (0)