Skip to content

Commit 59fe44f

Browse files
authored
fix: update release-please post-processing for nodejs apiary (#1850)
* fix: update release-please post-processing for nodejs apiary
1 parent 1a24315 commit 59fe44f

File tree

7 files changed

+77
-0
lines changed

7 files changed

+77
-0
lines changed

synthtool/languages/node.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,22 @@ def postprocess_gapic_library_hermetic(hide_output=False):
270270
logger.debug("Post-processing completed")
271271

272272

273+
# This function writes the release-please-config.json file
274+
# It adds entries for each directory with a default {} to
275+
# make sure we are tracking them for publishing
276+
def write_release_please_config(dirs: list):
277+
with open("release-please-config.json", "r") as f:
278+
data = json.load(f)
279+
for dir in dirs:
280+
result = re.search(r"(src/apis/.*)", dir)
281+
assert result is not None
282+
data["packages"][result.group()] = {}
283+
# Make sure base package is also published
284+
data["packages"]["."] = {}
285+
with open("release-please-config.json", "w") as f:
286+
json.dump(data, f, indent=2)
287+
288+
273289
default_staging_excludes = ["README.md", "package.json", "src/index.ts"]
274290
default_templates_excludes: List[str] = []
275291

@@ -278,6 +294,20 @@ def _noop(library: Path) -> None:
278294
pass
279295

280296

297+
# This function walks through the apiary packages
298+
# specifically in google-api-nodejs-client
299+
# This determines the current list of APIs
300+
def walk_through_apiary(dir, glob_to_search_for):
301+
packages_to_exclude = [r"node_modules"]
302+
dirs_to_return = []
303+
for path_object in Path(dir).glob(glob_to_search_for):
304+
if not path_object.is_file() and not re.search(
305+
"(?:% s)" % "|".join(packages_to_exclude), str(Path(path_object))
306+
):
307+
dirs_to_return.append(str(Path(path_object)))
308+
return dirs_to_return
309+
310+
281311
def owlbot_main(
282312
template_path: Optional[Path] = None,
283313
staging_excludes: Optional[List[str]] = None,
@@ -369,6 +399,8 @@ def owlbot_main(
369399
library_version = template_metadata().get("version")
370400
if library_version:
371401
common.update_library_version(library_version, _GENERATED_SAMPLES_DIRECTORY)
402+
if Path("release-please-config.json").is_file():
403+
write_release_please_config(walk_through_apiary(Path.cwd(), "src/apis/**/*"))
372404

373405

374406
if __name__ == "__main__":
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"release-type": "node",
3+
"packages": {
4+
"src/apis/admin": {},
5+
"src/apis/docs": {},
6+
".": {}
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"release-type": "node",
3+
"packages": {
4+
"src/apis/admin": {}
5+
}
6+
}

tests/fixtures/node_apiary/src/apis/admin/index.ts

Whitespace-only changes.

tests/fixtures/node_apiary/src/apis/docs/index.ts

Whitespace-only changes.

tests/fixtures/node_apiary/src/apis/index.ts

Whitespace-only changes.

tests/test_node.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
from pathlib import Path
1818
from unittest import TestCase
1919
from unittest.mock import patch
20+
import re
2021

2122
import pytest
2223

2324
import synthtool as s
2425
from synthtool.languages import node
2526
from . import util
27+
from unittest.mock import Mock
2628

2729
FIXTURES = Path(__file__).parent / "fixtures"
2830
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):
240242
assert staging_text == text
241243

242244

245+
def test_write_release_please_config():
246+
# use a non-nodejs template directory
247+
with util.copied_fixtures_dir(FIXTURES / "node_apiary"):
248+
node.write_release_please_config(
249+
[
250+
"Users/person/src/apis/admin",
251+
"tmpfs/src/apis/docs",
252+
]
253+
)
254+
255+
assert filecmp.cmp(
256+
pathlib.Path("release-please-config.json"),
257+
pathlib.Path("release-please-config-post-apiary.json"),
258+
)
259+
260+
261+
@patch("subprocess.run")
262+
def test_walk_through_apiary(mock_subproc_popen):
263+
process_mock = Mock()
264+
attrs = {"communicate.return_value": ("output", "error")}
265+
process_mock.configure_mock(**attrs)
266+
mock_subproc_popen.return_value = process_mock
267+
dirs = node.walk_through_apiary(FIXTURES / "node_apiary", "src/apis/**/*")
268+
assert not mock_subproc_popen.called
269+
assert re.search("src/apis/admin", dirs[0])
270+
assert re.search("src/apis/docs", dirs[1])
271+
assert len(dirs) == 2
272+
273+
243274
@patch("synthtool.languages.node.postprocess_gapic_library_hermetic")
244275
def test_owlbot_main_with_staging_ignore_index(hermetic_mock, nodejs_dlp):
245276
original_text = open(

0 commit comments

Comments
 (0)