Skip to content

Commit 6d19adc

Browse files
committed
feat: update code scanning packs support
1 parent 481ffc6 commit 6d19adc

File tree

4 files changed

+120
-22
lines changed

4 files changed

+120
-22
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
lockVersion: 1.0.0
3+
dependencies:
4+
codeql/python-all:
5+
version: 0.9.4
6+
codeql/regex:
7+
version: 0.0.15
8+
codeql/tutorial:
9+
version: 0.0.12
10+
codeql/util:
11+
version: 0.0.12
12+
codeql/yaml:
13+
version: 0.0.4
14+
compiled: false

examples/packs/qlpack.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
library: false
3+
name: geekmasher/ghastoolkit-python
4+
version: 0.1.0
5+
dependencies:
6+
codeql/python-all: "^0.9.2"
7+
defaultSuiteFile: default.qls
8+

src/ghastoolkit/codeql/packs/__main__.py

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
from argparse import ArgumentParser
55
from ghastoolkit import __banner__
6+
from ghastoolkit.codeql.packs.pack import CodeQLPack
67
from ghastoolkit.codeql.packs.packs import CodeQLPacks
78
from ghastoolkit.octokit.codescanning import CodeScanning
89
from ghastoolkit.octokit.github import GitHub
@@ -21,6 +22,9 @@
2122
"-dd", "--display-dependencies", action="store_true", help="Display Dependencies"
2223
)
2324

25+
parser_bumper = parser.add_argument_group("Bumper")
26+
parser_bumper.add_argument("-b", "--bump", default="minor", help="Bump version")
27+
2428
arguments = parser.parse_args()
2529
remote = True if arguments.repository else False
2630

@@ -33,24 +37,46 @@
3337

3438
code_scanning = CodeScanning()
3539

40+
if arguments.bump:
41+
# version bump mode
42+
logging.info(f"Bumping CodeQL Pack Versions")
43+
logging.info(f" - Bump: {arguments.bump}")
44+
45+
if os.path.isdir(arguments.packs_path):
46+
packs = CodeQLPacks(arguments.packs_path)
47+
for pack in packs:
48+
old_version = pack.version
49+
v = pack.updateVersion(arguments.bump)
50+
logging.info(f"Pack :: {pack.name} - {old_version} -> {v}")
51+
pack.updatePack()
52+
53+
elif os.path.isfile(arguments.packs_path):
54+
pack = CodeQLPack(arguments.packs_path)
55+
old_version = pack.version
56+
new_version = pack.updateVersion(arguments.bump)
57+
logging.info(f"Pack :: {pack.name} - {old_version} -> {new_version}")
3658

37-
logging.debug(f"Loading packs from environment...")
59+
pack.updatePack()
60+
61+
else:
62+
# display-only mode
63+
logging.debug(f"Loading packs from environment...")
3864

39-
packs = CodeQLPacks()
40-
packs.load(arguments.packs_path)
65+
packs = CodeQLPacks()
66+
packs.load(arguments.packs_path)
4167

42-
logging.info(f"CodeQL Packs :: {len(packs)}")
43-
logging.info("")
68+
logging.info(f"CodeQL Packs :: {len(packs)}")
69+
logging.info("")
4470

45-
for pack in packs:
46-
logging.info(f" - {pack}")
71+
for pack in packs:
72+
logging.info(f" - {pack}")
4773

48-
if remote:
49-
remote_version = pack.remote_version
50-
if remote_version:
51-
logging.info(f" |> Remote Version: `{pack.remote_version}`")
74+
if remote:
75+
remote_version = pack.remote_version
76+
if remote_version:
77+
logging.info(f" |> Remote Version: `{pack.remote_version}`")
5278

53-
if arguments.display_dependencies:
54-
logging.info(f" |> Dependencies")
55-
for dep in pack.dependencies:
56-
logging.info(f" |--> {dep}")
79+
if arguments.display_dependencies:
80+
logging.info(f" |> Dependencies")
81+
for dep in pack.dependencies:
82+
logging.info(f" |--> {dep}")

src/ghastoolkit/codeql/packs/pack.py

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import json
55
import glob
66
import logging
7-
from typing import List, Optional
7+
from typing import Any, List, Optional
8+
from collections import OrderedDict
9+
from semantic_version import Version
810
import yaml
911

1012
from ghastoolkit.codeql.cli import CodeQL
@@ -20,16 +22,22 @@ class CodeQLPack:
2022
"""CodeQL Packages Location"""
2123

2224
def __init__(
23-
self, path: Optional[str] = None, cli: Optional[CodeQL] = None
25+
self,
26+
path: Optional[str] = None,
27+
library: Optional[bool] = None,
28+
name: Optional[str] = None,
29+
version: Optional[str] = None,
30+
cli: Optional[CodeQL] = None,
2431
) -> None:
2532
"""Initialise CodeQL Pack."""
2633
self.cli = cli or CodeQL()
2734

2835
self.path = path
29-
self.library = False
30-
self.name = ""
31-
self.version = ""
32-
self.dependencies = []
36+
self.library: bool = library or False
37+
self.name: str = name or ""
38+
self.version: str = version or "0.0.0"
39+
self.dependencies: List["CodeQLPack"] = []
40+
self.default_suite: Optional[str] = None
3341

3442
if path:
3543
# if its a file
@@ -39,6 +47,8 @@ def __init__(
3947
self.path = os.path.realpath(os.path.expanduser(path))
4048
self.load()
4149

50+
logger.debug(f"Finished loading Pack :: {self}")
51+
4252
@property
4353
def qlpack(self) -> str:
4454
"""QL Pack Location."""
@@ -62,7 +72,11 @@ def load(self):
6272
self.library = bool(data.get("library"))
6373
self.name = data.get("name", "")
6474
self.version = data.get("version", "")
65-
self.dependencies.extend(data.get("dependencies", []))
75+
self.default_suite = data.get("defaultSuiteFile")
76+
77+
for name, version in data.get("dependencies", {}).items():
78+
print(f" >> {name} == {version}")
79+
self.dependencies.append(CodeQLPack(name=name, version=version))
6680

6781
def run(self, *args, display: bool = False) -> Optional[str]:
6882
"""Run Pack command."""
@@ -128,6 +142,42 @@ def remote_version(self) -> Optional[str]:
128142
logging.debug(f"Error getting remote version")
129143
return None
130144

145+
def updatePack(self) -> dict[str, Any]:
146+
"""Update Local CodeQL Pack."""
147+
deps = {}
148+
for dep in self.dependencies:
149+
deps[dep.name] = dep.version
150+
151+
data = {
152+
"library": self.library,
153+
"name": self.name,
154+
"version": self.version,
155+
"dependencies": deps,
156+
}
157+
158+
if self.path:
159+
logger.info(f"Saving pack to path :: {self.path}")
160+
with open(self.qlpack, "w") as handle:
161+
yaml.safe_dump(data, handle, sort_keys=False)
162+
163+
return data
164+
165+
def updateVersion(self, name: str = "patch", version: Optional[str] = None) -> str:
166+
"""Update CodeQL Pack version."""
167+
if version:
168+
self.version = version
169+
return version
170+
171+
v = Version(self.version)
172+
if name == "major":
173+
v = v.next_major()
174+
elif name == "minor":
175+
v = v.next_minor()
176+
elif name == "patch":
177+
v = v.next_patch()
178+
self.version = str(v)
179+
return self.version
180+
131181
def __str__(self) -> str:
132182
"""To String."""
133183
if self.name != "":

0 commit comments

Comments
 (0)