Skip to content

Commit

Permalink
fix: update release-please post-processing for nodejs apiary (#1850)
Browse files Browse the repository at this point in the history
* fix: update release-please post-processing for nodejs apiary
  • Loading branch information
sofisl authored Aug 17, 2023
1 parent 1a24315 commit 59fe44f
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 0 deletions.
32 changes: 32 additions & 0 deletions synthtool/languages/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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] = []

Expand All @@ -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,
Expand Down Expand Up @@ -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__":
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"release-type": "node",
"packages": {
"src/apis/admin": {},
"src/apis/docs": {},
".": {}
}
}
6 changes: 6 additions & 0 deletions tests/fixtures/node_apiary/release-please-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"release-type": "node",
"packages": {
"src/apis/admin": {}
}
}
Empty file.
Empty file.
Empty file.
31 changes: 31 additions & 0 deletions tests/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 59fe44f

Please sign in to comment.