Skip to content
Snippets Groups Projects
Commit cd8de4ff authored by Tim Jaacks's avatar Tim Jaacks
Browse files

.gitlab-ci: add analyze stage for limiting YAML script blocks

parent e943a55e
No related branches found
No related tags found
1 merge request!256.gitlab-ci: add analyze stage for limiting YAML script blocks
Pipeline #58222 skipped with stage
...@@ -62,6 +62,14 @@ pylint: ...@@ -62,6 +62,14 @@ pylint:
- cd scripts - cd scripts
- pylint --rcfile=pylintrc *.py - pylint --rcfile=pylintrc *.py
script_limit:
extends: .analyze
script:
- scripts/check_yaml_value_length.py *.yml --limit 800
--key script
--key before_script
--key after_script
yamllint: yamllint:
extends: .analyze extends: .analyze
script: script:
......
#!/usr/bin/env python3
import argparse
import glob
import sys
from collections import OrderedDict
from ruamel.yaml import YAML, CommentedMap
from colors import colors
def check_value_length(filename: str, data: OrderedDict, keys: list[str], limit: int):
"""Check if values of given YAML keys exceed a given limit.
Args:
filename: name of the file where data has been read from (used for debug output)
data: ordered dictionary containing YAML elements
keys: list of keys to check values of
limit: maximum number of allowed characters for any of the keys' values
Returns:
True if a limit exceedence has been found
False else
"""
exceeded = False
for key, value in data.items():
if key in keys:
count = 0
for item in value:
count += len(item)
if count > limit:
exceeded = True
print(
colors.fg.purple
+ filename
+ ":"
+ colors.fg.green
+ str(data[key].lc.line)
+ colors.reset
+ " value of '%s' exceeds character limit (%d > %d)"
% (key, count, limit)
)
if type(value) is CommentedMap:
exceeded = check_value_length(filename, value, keys, limit) | exceeded
return exceeded
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--key",
help="""YAML key to check (can be passed multiple times for multiple keys)""",
dest="key",
action="append",
required=True,
)
parser.add_argument(
"--limit",
help="""Character limit for given key(s)""",
dest="limit",
type=int,
required=True,
)
parser.add_argument(
"filename",
help="""File(s) to check""",
nargs="+",
)
args, _ = parser.parse_known_args()
files = []
for filename in args.filename:
for f in glob.glob(filename):
files.append(f)
exceeded = False
for f in files:
with open(f, "r", encoding="utf8") as fp:
yaml = YAML()
data = yaml.load(fp)
exceeded = check_value_length(f, data, args.key, args.limit) | exceeded
if exceeded:
sys.exit(1)
if __name__ == "__main__":
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment