Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Fix broken regeneration of YAML CSV
Browse files Browse the repository at this point in the history
ruamel will introduce a line break if the yaml line is longer than
yaml.width.  Unfortunately, this causes issues for JSON values nested
within a YAML file, e.g.  metadata.annotations."alm-examples" in a CSV
file.  The default value is 80. Set it to a more forgivinng higher
number to avoid issues

Signed-off-by: Luiz Carvalho <lucarval@redhat.com>
  • Loading branch information
lcarva committed Oct 19, 2020
1 parent 3b024cc commit bf4bd36
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 8 deletions.
18 changes: 17 additions & 1 deletion omps/api/common.py
Expand Up @@ -10,6 +10,7 @@

from ruamel.yaml import YAML

from omps.constants import YAML_WIDTH
from omps.errors import OMPSAuthorizationHeaderRequired


Expand Down Expand Up @@ -64,7 +65,7 @@ def adjust_csv_annotations(quay_org, dir_path, context):
if not quay_org.csv_annotations:
return

yaml = YAML()
yaml = get_yaml_parser()
# for filename in sorted(os.listdir(dir_path)):
for filename in _yield_yaml_files(dir_path):
with open(filename, 'r+') as f:
Expand All @@ -90,3 +91,18 @@ def _yield_yaml_files(dir_path):
fname_lower = fname.lower()
if fname_lower.endswith('.yml') or fname_lower.endswith('.yaml'):
yield os.path.join(root, fname)


def get_yaml_parser():
"""Returns an instance of the YAML parser with common settings
:rtype: ruamel.yaml.YAML
:return: YAML parser
"""
yaml = YAML()
# IMPORTANT: ruamel will introduce a line break if the yaml line is longer than
# yaml.width. Unfortunately, this causes issues for JSON values nested within a
# YAML file, e.g. metadata.annotations."alm-examples" in a CSV file. The default
# value is 80. Set it to a more forgiving higher number to avoid issues
yaml.width = YAML_WIDTH
return yaml
10 changes: 7 additions & 3 deletions omps/api/v1/push.py
Expand Up @@ -10,10 +10,14 @@
import zipfile

from flask import jsonify, current_app, request
from ruamel.yaml import YAML

from . import API
from omps.api.common import extract_auth_token, replace_registries, adjust_csv_annotations
from omps.api.common import (
adjust_csv_annotations,
extract_auth_token,
get_yaml_parser,
replace_registries,
)
from omps.constants import (
ALLOWED_EXTENSIONS,
DEFAULT_ZIPFILE_MAX_UNCOMPRESSED_SIZE,
Expand Down Expand Up @@ -173,7 +177,7 @@ def _process_package_name(quay_org, dir_path):
If not found, or malformed, raise PackageValidationError.
If package_name_suffix is configured, modify the packageName.
"""
yaml = YAML()
yaml = get_yaml_parser()
for filename in sorted(os.listdir(dir_path)):
filename = os.path.join(dir_path, filename)
if filename.endswith('.yaml') or filename.endswith('.yml'):
Expand Down
6 changes: 6 additions & 0 deletions omps/constants.py
Expand Up @@ -15,3 +15,9 @@
ALLOWED_EXTENSIONS = {".zip", }

KOJI_OPERATOR_MANIFESTS_ARCHIVE_KEY = 'operator_manifests_archive'

# IMPORTANT: ruamel will introduce a line break if the yaml line is longer than
# yaml.width. Unfortunately, this causes issues for JSON values nested within a
# YAML file, e.g. metadata.annotations."alm-examples" in a CSV file. The default
# value is 80. Set it to a more forgiving higher number to avoid issues
YAML_WIDTH = 200
18 changes: 14 additions & 4 deletions tests/api/test_common.py
Expand Up @@ -5,9 +5,13 @@

import os

from ruamel.yaml import YAML

from omps.api.common import replace_registries, adjust_csv_annotations, _yield_yaml_files
from omps.constants import YAML_WIDTH
from omps.api.common import (
_yield_yaml_files,
adjust_csv_annotations,
get_yaml_parser,
replace_registries,
)
from omps.quay import QuayOrganization


Expand Down Expand Up @@ -61,9 +65,15 @@ def test_adjust_csv_annotations(datadir):

adjust_csv_annotations(quay_org, dir_path, {'package_name': 'etcd'})

yaml = YAML()
yaml = get_yaml_parser()
for fpath in should_have_annotations:
with open(fpath, 'r') as f:
contents = yaml.load(f.read())
for name, value in expected_annotations.items():
assert contents['metadata']['annotations'][name] == value


def test_get_yaml_parser():
"""Test if yaml parser is configured correctly"""
yaml = get_yaml_parser()
assert yaml.width == YAML_WIDTH

0 comments on commit bf4bd36

Please sign in to comment.