From 59fe44fde9866a26e7ee4e4450fd79f67f8cf599 Mon Sep 17 00:00:00 2001 From: sofisl <55454395+sofisl@users.noreply.github.com> Date: Thu, 17 Aug 2023 12:14:00 -0700 Subject: [PATCH] fix: update release-please post-processing for nodejs apiary (#1850) * fix: update release-please post-processing for nodejs apiary --- synthtool/languages/node.py | 32 +++++++++++++++++++ .../release-please-config-post-apiary.json | 8 +++++ .../node_apiary/release-please-config.json | 6 ++++ .../node_apiary/src/apis/admin/index.ts | 0 .../node_apiary/src/apis/docs/index.ts | 0 tests/fixtures/node_apiary/src/apis/index.ts | 0 tests/test_node.py | 31 ++++++++++++++++++ 7 files changed, 77 insertions(+) create mode 100644 tests/fixtures/node_apiary/release-please-config-post-apiary.json create mode 100644 tests/fixtures/node_apiary/release-please-config.json create mode 100644 tests/fixtures/node_apiary/src/apis/admin/index.ts create mode 100644 tests/fixtures/node_apiary/src/apis/docs/index.ts create mode 100644 tests/fixtures/node_apiary/src/apis/index.ts diff --git a/synthtool/languages/node.py b/synthtool/languages/node.py index aa5c69b79..0a8b794f2 100644 --- a/synthtool/languages/node.py +++ b/synthtool/languages/node.py @@ -270,6 +270,22 @@ def postprocess_gapic_library_hermetic(hide_output=False): logger.debug("Post-processing completed") +# This function writes the release-please-config.json file +# It adds entries for each directory with a default {} to +# make sure we are tracking them for publishing +def write_release_please_config(dirs: list): + with open("release-please-config.json", "r") as f: + data = json.load(f) + for dir in dirs: + result = re.search(r"(src/apis/.*)", dir) + assert result is not None + data["packages"][result.group()] = {} + # Make sure base package is also published + data["packages"]["."] = {} + with open("release-please-config.json", "w") as f: + json.dump(data, f, indent=2) + + default_staging_excludes = ["README.md", "package.json", "src/index.ts"] default_templates_excludes: List[str] = [] @@ -278,6 +294,20 @@ def _noop(library: Path) -> None: pass +# This function walks through the apiary packages +# specifically in google-api-nodejs-client +# This determines the current list of APIs +def walk_through_apiary(dir, glob_to_search_for): + packages_to_exclude = [r"node_modules"] + dirs_to_return = [] + for path_object in Path(dir).glob(glob_to_search_for): + if not path_object.is_file() and not re.search( + "(?:% s)" % "|".join(packages_to_exclude), str(Path(path_object)) + ): + dirs_to_return.append(str(Path(path_object))) + return dirs_to_return + + def owlbot_main( template_path: Optional[Path] = None, staging_excludes: Optional[List[str]] = None, @@ -369,6 +399,8 @@ def owlbot_main( library_version = template_metadata().get("version") if library_version: common.update_library_version(library_version, _GENERATED_SAMPLES_DIRECTORY) + if Path("release-please-config.json").is_file(): + write_release_please_config(walk_through_apiary(Path.cwd(), "src/apis/**/*")) if __name__ == "__main__": diff --git a/tests/fixtures/node_apiary/release-please-config-post-apiary.json b/tests/fixtures/node_apiary/release-please-config-post-apiary.json new file mode 100644 index 000000000..aab64b59f --- /dev/null +++ b/tests/fixtures/node_apiary/release-please-config-post-apiary.json @@ -0,0 +1,8 @@ +{ + "release-type": "node", + "packages": { + "src/apis/admin": {}, + "src/apis/docs": {}, + ".": {} + } +} \ No newline at end of file diff --git a/tests/fixtures/node_apiary/release-please-config.json b/tests/fixtures/node_apiary/release-please-config.json new file mode 100644 index 000000000..8c58b9908 --- /dev/null +++ b/tests/fixtures/node_apiary/release-please-config.json @@ -0,0 +1,6 @@ +{ + "release-type": "node", + "packages": { + "src/apis/admin": {} + } +} \ No newline at end of file diff --git a/tests/fixtures/node_apiary/src/apis/admin/index.ts b/tests/fixtures/node_apiary/src/apis/admin/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/node_apiary/src/apis/docs/index.ts b/tests/fixtures/node_apiary/src/apis/docs/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/tests/fixtures/node_apiary/src/apis/index.ts b/tests/fixtures/node_apiary/src/apis/index.ts new file mode 100644 index 000000000..e69de29bb diff --git a/tests/test_node.py b/tests/test_node.py index 481327e2b..2a28410f0 100644 --- a/tests/test_node.py +++ b/tests/test_node.py @@ -17,12 +17,14 @@ from pathlib import Path from unittest import TestCase from unittest.mock import patch +import re import pytest import synthtool as s from synthtool.languages import node from . import util +from unittest.mock import Mock FIXTURES = Path(__file__).parent / "fixtures" TEMPLATES = Path(__file__).parent.parent / "synthtool" / "gcp" / "templates" @@ -240,6 +242,35 @@ def test_owlbot_main_with_staging_index_from_staging(hermetic_mock, nodejs_dlp): assert staging_text == text +def test_write_release_please_config(): + # use a non-nodejs template directory + with util.copied_fixtures_dir(FIXTURES / "node_apiary"): + node.write_release_please_config( + [ + "Users/person/src/apis/admin", + "tmpfs/src/apis/docs", + ] + ) + + assert filecmp.cmp( + pathlib.Path("release-please-config.json"), + pathlib.Path("release-please-config-post-apiary.json"), + ) + + +@patch("subprocess.run") +def test_walk_through_apiary(mock_subproc_popen): + process_mock = Mock() + attrs = {"communicate.return_value": ("output", "error")} + process_mock.configure_mock(**attrs) + mock_subproc_popen.return_value = process_mock + dirs = node.walk_through_apiary(FIXTURES / "node_apiary", "src/apis/**/*") + assert not mock_subproc_popen.called + assert re.search("src/apis/admin", dirs[0]) + assert re.search("src/apis/docs", dirs[1]) + assert len(dirs) == 2 + + @patch("synthtool.languages.node.postprocess_gapic_library_hermetic") def test_owlbot_main_with_staging_ignore_index(hermetic_mock, nodejs_dlp): original_text = open(