Skip to content

Commit f06dc95

Browse files
authored
Add Buildkite schema and fix minor script issues (#217)
1. Add Buildkite to the catalog 2. Add a test-case from the Buildkite repo 3. Fix minor issues in the vendor-schemas script - bugfix KeyError when adding new schemas - strip trailing whitespace so that pre-commit run doesn't munge hash comparison 4. Update generate-hooks-config script to handle 'types_or' 5. Run vendor-schemas and generate-hooks-config Resolves #198
1 parent a11eb1c commit f06dc95

File tree

12 files changed

+1429
-20
lines changed

12 files changed

+1429
-20
lines changed

.pre-commit-hooks.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,20 @@
3434
files: ^bamboo-specs/.*\.(yml|yaml)$
3535
types: [yaml]
3636

37+
- id: check-buildkite
38+
name: Validate Buildkite Pipelines
39+
description: 'Validate Buildkite Pipelines against the schema provided by Buildkite'
40+
entry: check-jsonschema --builtin-schema vendor.buildkite
41+
language: python
42+
files: >
43+
(?x)^(
44+
buildkite\.(yml|yaml|json)|
45+
buildkite\.(.+)\.(yml|yaml|json)|
46+
(.*/)?\.buildkite/pipeline\.(yml|yaml|json)|
47+
(.*/)?\.buildkite/pipeline\.(.+)\.(yml|yaml|json)
48+
)$
49+
types_or: [json,yaml]
50+
3751
- id: check-dependabot
3852
name: Validate Dependabot Config (v2)
3953
description: 'Validate Dependabot Config (v2) against the schema provided by SchemaStore'

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Unreleased
1414
- Add ``--fill-defaults`` argument which eagerly populates ``"default"``
1515
values whenever they are encountered and a value is not already present
1616
(:issue:`200`)
17+
- Add Buildkite schema and pre-commit hook (:issue:`198`)
1718

1819
0.19.2
1920
------

docs/precommit_usage.rst

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ Validate Bamboo Specs against the schema provided by SchemaStore
7171
- id: check-bamboo-spec
7272
7373
74+
``check-buildkite``
75+
~~~~~~~~~~~~~~~~~~~
76+
77+
Validate Buildkite Pipelines against the schema provided by Buildkite
78+
79+
.. code-block:: yaml
80+
:caption: example config
81+
82+
- repo: https://github.com/python-jsonschema/check-jsonschema
83+
rev: 0.19.2
84+
hooks:
85+
- id: check-buildkite
86+
87+
7488
``check-dependabot``
7589
~~~~~~~~~~~~~~~~~~~~
7690

docs/usage.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ SchemaStore and other sources:
7070
7171
- ``vendor.azure-pipelines``
7272
- ``vendor.bamboo-spec``
73+
- ``vendor.buildkite``
7374
- ``vendor.dependabot``
7475
- ``vendor.github-actions``
7576
- ``vendor.github-workflows``

scripts/generate-hooks-config.py

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from __future__ import annotations
33

44
import importlib.metadata
5+
import typing as t
56

67
from check_jsonschema.catalog import SCHEMA_CATALOG
78

@@ -40,10 +41,21 @@ def update_hook_config(new_config: str) -> None:
4041
fp.write(content)
4142

4243

43-
def format_hook(config) -> str:
44+
def generate_hook_lines(config) -> t.Iterator[str]:
45+
yield f"- id: {config['id']}"
46+
yield f" name: {config['name']}"
47+
yield f" description: '{config['description']}'"
48+
4449
add_args = " ".join(config.get("add_args", []))
4550
if add_args:
4651
add_args = " " + add_args
52+
yield (
53+
" entry: check-jsonschema --builtin-schema "
54+
f"vendor.{config['schema_name']}{add_args}"
55+
)
56+
57+
yield " language: python"
58+
4759
if isinstance(config["files"], list):
4860
config[
4961
"files"
@@ -54,23 +66,23 @@ def format_hook(config) -> str:
5466
"|\n ".join(config["files"])
5567
)
5668

57-
config_str = f"""\
58-
- id: {config["id"]}
59-
name: {config["name"]}
60-
description: '{config["description"]}'
61-
entry: check-jsonschema --builtin-schema vendor.{config["schema_name"]}{add_args}
62-
language: python
63-
files: {config["files"]}
64-
"""
69+
yield f" files: {config['files']}"
70+
6571
if "types" in config:
66-
config_str += f"""\
67-
types: [{','.join(config['types'])}]
68-
"""
69-
return config_str
72+
yield f" types: [{','.join(config['types'])}]"
73+
if "types_or" in config:
74+
yield f" types_or: [{','.join(config['types_or'])}]"
75+
76+
yield ""
77+
78+
79+
def generate_all_hook_config_lines() -> t.Iterator[str]:
80+
for hook_config in iter_catalog_hooks():
81+
yield from generate_hook_lines(hook_config)
7082

7183

72-
def generate_hook_config() -> str:
73-
return "\n".join(format_hook(h) for h in iter_catalog_hooks())
84+
def format_all_hook_config() -> str:
85+
return "\n".join(generate_all_hook_config_lines())
7486

7587

7688
def update_usage_list_schemas() -> None:
@@ -139,7 +151,7 @@ def update_docs() -> None:
139151

140152

141153
def main() -> None:
142-
update_hook_config(generate_hook_config())
154+
update_hook_config(format_all_hook_config())
143155
update_docs()
144156

145157

scripts/vendor-schemas.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def load_old_hashes() -> None:
5050
OLD_HASHES[name] = fp.read().strip()
5151

5252

53+
def normalize_schema_contents(data: bytes) -> bytes:
54+
lines = data.split(b"\n")
55+
return b"\n".join(line.rstrip(b" ") for line in lines)
56+
57+
5358
def download_schemas() -> None:
5459
print("downloading schemas to check for updates")
5560
session = requests.Session()
@@ -59,17 +64,18 @@ def download_schemas() -> None:
5964

6065
print(f" {schema_name} ({schema_url})")
6166
res = session.get(schema_url)
67+
new_content = normalize_schema_contents(res.content)
68+
6269
sha = hashlib.sha256()
63-
sha.update(res.content)
70+
sha.update(new_content)
6471
new_digest = sha.hexdigest()
6572

6673
# early abort if downloaded content matches old hash
6774
if new_digest == OLD_HASHES.get(schema_name):
6875
continue
6976

70-
print(" content changed")
7177
with open(schema2filename(schema_name), "wb") as fp:
72-
fp.write(res.content)
78+
fp.write(new_content)
7379
UPDATED_SCHEMAS.add(schema_name)
7480

7581

@@ -105,7 +111,7 @@ def _save_new_hash(name: str, digest: str) -> None:
105111
_save_new_hash(name, digest)
106112

107113
# if the existing hash does not match the new hash, save the new one
108-
if digest != OLD_HASHES[name]:
114+
elif digest != OLD_HASHES[name]:
109115
_save_new_hash(name, digest)
110116

111117

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2018 Buildkite
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

src/check_jsonschema/builtin_schemas/vendor/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,8 @@ renovatebot
2929
The GitLab CI schema is provided by GitLab and licensed under the license for
3030
the public gitlab repo. In particular, it falls under the "MIT Expat" portion
3131
of the license for that repo.
32+
33+
### Buildkite
34+
35+
The Buildkite schema is provided by Buildkite and licensed under the license
36+
for their 'pipeline-schema' repo.

0 commit comments

Comments
 (0)