Skip to content

Commit d0c6e9a

Browse files
authored
chore: fix spanner fallback and add test for split_release_note (#12896)
The original code assumed that if `.OwlBot-hermetic.yaml` existed, it must contain an `api-name`. This is not true for Spanner, which has the file but lacks the `api-name field`. So, fallback to `.repo-metadata.json` is needed sometimes even if `.OwlBot-hermetic.yaml` exists. Towards #12864.
1 parent 26b06f9 commit d0c6e9a

2 files changed

Lines changed: 45 additions & 17 deletions

File tree

.github/release-note-generation/split_release_note.py

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ def detect_modules(root_directory: Path):
6969
tree = ET.parse(module_pom_xml)
7070
root = tree.getroot()
7171
version = root.find('mvn:version', POM_NAMESPACES).text
72+
api_name = None
7273
if owlbot_yaml_path.exists():
7374
# If OwlBot configuration file exists (most cases), it's the better
7475
# source to get the OwlBot-generated pull request title prefix than
@@ -78,21 +79,21 @@ def detect_modules(root_directory: Path):
7879
match = re.search(r'api-name: (.+)', owlbot_yaml_content)
7980
if match:
8081
api_name = match.group(1)
81-
modules.append(LibraryModule(module_path, api_name,
82-
version,
83-
changelog))
82+
83+
if not api_name:
84+
# Fallback to repo-metadata.json (e.g. for vertexai or Spanner transitional state)
85+
if repo_metadata_path.exists():
86+
with open(repo_metadata_path, 'r') as file:
87+
repo_metadata = json.load(file)
88+
api_name = repo_metadata.get('api_shortname')
89+
90+
if api_name:
91+
modules.append(LibraryModule(module_path, api_name,
92+
version,
93+
changelog))
8494
else:
85-
# vertexai (handwritten) does not have OwlBot yaml file
86-
with open(repo_metadata_path, 'r') as file:
87-
repo_metadata = json.load(file)
88-
api_name = repo_metadata['api_shortname']
89-
if api_name:
90-
modules.append(LibraryModule(repo_metadata_path.parent, api_name,
91-
version,
92-
changelog))
93-
else:
94-
raise Exception(f'repo_metadata_path {repo_metadata_path} does'
95-
f' not have api_shortname field')
95+
raise Exception(f'Could not determine api-name for {repo_metadata_path}')
96+
9697

9798
return modules
9899

@@ -133,7 +134,7 @@ def group_changes_by_api(main_changes: [str]):
133134
elif section == BUG_FIXES_SECTION:
134135
api_to_changelog[api_name].bug_fixes.append(note)
135136
elif section == DEPENDENCIES_SECTION:
136-
api_to_changelog[api_name].dependencies.append(note)
137+
api_to_changelog[api_name].dependency_upgrades.append(note)
137138
return api_to_changelog
138139

139140

.github/release-note-generation/unit_test.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import unittest
2+
import tempfile
3+
import json
4+
from pathlib import Path
25

36
# Unit tests for split_release_note.py
47

5-
from split_release_note import LibraryModule, create_changelog_entry, group_changes_by_api, ChangesOnApi
6-
from pathlib import Path
8+
from split_release_note import LibraryModule, create_changelog_entry, group_changes_by_api, ChangesOnApi, detect_modules
79

810
dummy_module = LibraryModule(
911
Path('release-note-generation/test/java-analyics-admin'),
@@ -81,6 +83,31 @@ def test_group_changes_by_api(self):
8183
['No change']),
8284
['No change'])
8385

86+
def test_detect_modules_fallback(self):
87+
with tempfile.TemporaryDirectory() as tmpdirname:
88+
tmp_path = Path(tmpdirname)
89+
module_path = tmp_path / "java-spanner"
90+
module_path.mkdir()
91+
92+
# Create minimal pom.xml
93+
pom_path = module_path / "pom.xml"
94+
with open(pom_path, "w") as f:
95+
f.write('<project xmlns:mvn="http://maven.apache.org/POM/4.0.0"><mvn:version>1.0.0</mvn:version></project>')
96+
97+
# Create .repo-metadata.json with api_shortname
98+
metadata_path = module_path / ".repo-metadata.json"
99+
with open(metadata_path, "w") as f:
100+
json.dump({"api_shortname": "spanner"}, f)
101+
102+
# Create CHANGELOG.md
103+
changelog_path = module_path / "CHANGELOG.md"
104+
changelog_path.touch()
105+
106+
modules = detect_modules(tmp_path)
107+
self.assertEqual(len(modules), 1)
108+
self.assertEqual(modules[0].api_name, "spanner")
109+
self.assertEqual(modules[0].version, "1.0.0")
110+
84111

85112
if __name__ == "__main__":
86113
unittest.main()

0 commit comments

Comments
 (0)