Skip to content

Commit

Permalink
Merge pull request #179 from ligangty/main
Browse files Browse the repository at this point in the history
Fix a npm metadata serialization issue
  • Loading branch information
ligangty authored Sep 13, 2023
2 parents 281cfd8 + a7927a7 commit 4c5a5d7
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 12 deletions.
13 changes: 9 additions & 4 deletions charon/pkgs/npm.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import logging
import os
import sys
from json import load, loads, dump, JSONDecodeError
from json import load, loads, dump, JSONDecodeError, JSONEncoder
import tarfile
from tempfile import mkdtemp
from typing import List, Set, Tuple
Expand All @@ -32,7 +32,7 @@
from charon.pkgs.pkg_utils import upload_post_process, rollback_post_process
from charon.utils.strings import remove_prefix
from charon.utils.files import write_manifest
from charon.utils.map import del_none
from charon.utils.map import del_none, replace_field

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -61,10 +61,15 @@ def __init__(self, metadata, is_version):
self.dist_tags = {'latest': metadata.get('version')}
self.versions = {metadata.get('version'): metadata}
else:
self.dist_tags = metadata.get('dist_tags', None)
self.dist_tags = metadata.get('dist-tags', None)
self.versions = metadata.get('versions', None)


class NPMPackageMetadataEncoder(JSONEncoder):
def default(self, o):
return replace_field(del_none(o.__dict__.copy()), "dist_tags", "dist-tags")


def handle_npm_uploading(
tarball_path: str,
product: str,
Expand Down Expand Up @@ -549,7 +554,7 @@ def _write_package_metadata_to_file(package_metadata: NPMPackageMetadata, root='
final_package_metadata_path = os.path.join(root, package_metadata.name, PACKAGE_JSON)
try:
with open(final_package_metadata_path, mode='w', encoding='utf-8') as f:
dump(del_none(package_metadata.__dict__.copy()), f)
dump(obj=package_metadata, cls=NPMPackageMetadataEncoder, fp=f)
return final_package_metadata_path
except FileNotFoundError:
logger.error(
Expand Down
7 changes: 7 additions & 0 deletions charon/utils/map.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,10 @@ def del_none(d):
elif isinstance(value, dict):
del_none(value)
return d


def replace_field(d, field, new_field):
if d[field]:
d[new_field] = d[field]
del d[field]
return d
2 changes: 1 addition & 1 deletion tests/test_npm_del.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def __test_prefix(self, prefix: str = None):
self.assertIn("\"versions\": {\"7.15.8\":", meta_content_client)
self.assertNotIn("\"7.14.5\": {\"name\":", meta_content_client)
self.assertIn("\"license\": \"MIT\"", meta_content_client)
self.assertIn("\"dist_tags\": {\"latest\": \"7.15.8\"}", meta_content_client)
self.assertIn("\"dist-tags\": {\"latest\": \"7.15.8\"}", meta_content_client)

test_tgz = os.path.join(INPUTS, "code-frame-7.15.8.tgz")
handle_npm_del(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_npm_del_multi_tgts.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __test_prefix(self, prefix: str = None):
"\"license\": \"MIT\"", meta_content_client, msg=f'{bucket_name}'
)
self.assertIn(
"\"dist_tags\": {\"latest\": \"7.15.8\"}",
"\"dist-tags\": {\"latest\": \"7.15.8\"}",
meta_content_client, msg=f'{bucket_name}'
)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_npm_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_handle_npm_uploading_for_old_version(self):
bucket = self.mock_s3.Bucket(MY_BUCKET)
original_version_0_5_8_package_json = """
{"name": "@redhat/kogito-tooling-workspace",
"dist_tags": {"latest": "0.5.8"}, "versions": {"0.5.8": {"name":
"dist-tags": {"latest": "0.5.8"}, "versions": {"0.5.8": {"name":
"@redhat/kogito-tooling-workspace", "version": "0.5.8", "title": "0.5.8title",
"description": "0.5.8description", "keywords": ["0.5.8"], "maintainers": [
"0.5.8maintainer"], "repository": {"type": "git", "url": "https://github.com/0.5.8.git"},
Expand Down Expand Up @@ -98,7 +98,7 @@ def test_handle_npm_uploading_for_old_version(self):
def test_handle_npm_uploading_for_new_version(self):
bucket = self.mock_s3.Bucket(MY_BUCKET)
original_version_1_0_1_package_json = """
{"name": "@redhat/kogito-tooling-workspace", "dist_tags": {"latest": "1.0.1"},
{"name": "@redhat/kogito-tooling-workspace", "dist-tags": {"latest": "1.0.1"},
"versions": {"1.0.1": {"name": "@redhat/kogito-tooling-workspace", "version": "1.0.1",
"title": "1.0.1title", "description": "1.0.1description", "keywords": ["1.0.1"],
"maintainers": ["1.0.1maintainer"], "repository": {"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions tests/test_npm_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_double_uploads(self):
self.assertIn("\"7.15.8\": {\"name\":", meta_content_client)
self.assertIn("\"7.14.5\": {\"name\":", meta_content_client)
self.assertIn("\"license\": \"MIT\"", meta_content_client)
self.assertIn("\"dist_tags\": {\"latest\": \"7.15.8\"}", meta_content_client)
self.assertIn("\"dist-tags\": {\"latest\": \"7.15.8\"}", meta_content_client)

def __test_prefix(self, prefix: str = None):
test_tgz = os.path.join(INPUTS, "code-frame-7.14.5.tgz")
Expand Down Expand Up @@ -131,4 +131,4 @@ def __test_prefix(self, prefix: str = None):
self.assertIn("\"version\": \"7.14.5\"", meta_content_client)
self.assertIn("\"versions\": {\"7.14.5\":", meta_content_client)
self.assertIn("\"license\": \"MIT\"", meta_content_client)
self.assertIn("\"dist_tags\": {\"latest\": \"7.14.5\"}", meta_content_client)
self.assertIn("\"dist-tags\": {\"latest\": \"7.14.5\"}", meta_content_client)
4 changes: 2 additions & 2 deletions tests/test_npm_upload_multi_tgts.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_double_uploads(self):
"\"license\": \"MIT\"", meta_content_client, msg=f'{bucket_name}'
)
self.assertIn(
"\"dist_tags\": {\"latest\": \"7.15.8\"}", meta_content_client, msg=f'{bucket_name}'
"\"dist-tags\": {\"latest\": \"7.15.8\"}", meta_content_client, msg=f'{bucket_name}'
)

def __test_prefix(self, prefix: str = None):
Expand Down Expand Up @@ -192,6 +192,6 @@ def __test_prefix(self, prefix: str = None):
msg=f'{bucket_name}'
)
self.assertIn(
"\"dist_tags\": {\"latest\": \"7.14.5\"}",
"\"dist-tags\": {\"latest\": \"7.14.5\"}",
meta_content_client, msg=f'{bucket_name}'
)

0 comments on commit 4c5a5d7

Please sign in to comment.