gemini-code-assist[bot] commented on code in PR #38815:
URL: https://github.com/apache/beam/pull/38815#discussion_r3357636112
##########
scripts/tools/bomupgrader.py:
##########
@@ -254,18 +255,54 @@ def write_back(self):
for line in self.target_lines:
fout.write(line)
+ def _get_opentelemetry_version(self):
+ for line in self.target_lines:
+ match = self.VERSION_STRING.match(line)
+ if match and match.group(1) == 'opentelemetry':
+ return match.group(2)
+ logging.warning('opentelemetry_version not found in BeamModulePlugin')
+ return None
+
+ @staticmethod
+ def _opentelemetry_license_yaml_block(version):
+ license_url = (
+ 'https://raw.githubusercontent.com/open-telemetry/'
+ 'opentelemetry-java/v{}/LICENSE'.format(version))
+ return (
+ 'opentelemetry-bom:\n'
+ " '{version}':\n"
+ ' license: "{license_url}"\n'
+ ' type: "Apache License 2.0"\n'
+ 'opentelemetry-bom-alpha:\n'
+ " '{version}-alpha':\n"
+ ' license: "{license_url}"\n'
+ ' type: "Apache License 2.0"\n'.format(
+ version=version, license_url=license_url))
+
def write_license_script(self):
logging.info("-----Update dep_urls_java.yaml-----")
- with open(os.path.join(self.project_root, self.LICENSE_SC_PATH),
- 'r') as fin:
- lines = fin.readlines()
- with open(os.path.join(self.project_root, self.LICENSE_SC_PATH),
- 'w') as fout:
- for idx, line in enumerate(lines):
- if line.strip() == 'libraries-bom:':
- lines[idx + 1] = re.sub(
- r'[\'"]\d[\.\d]+[\'"]', f"'{self.bom_version}'", lines[idx + 1])
- fout.write(line)
+ license_path = os.path.join(self.project_root, self.LICENSE_SC_PATH)
+ with open(license_path, 'r') as fin:
+ content = fin.read()
+
+ content = re.sub(
+ r"(libraries-bom:\n)(\s*['\"])\d[\d\.]+(['\"])",
+ r"\1\2{}\3".format(self.bom_version),
+ content,
+ count=1)
+
+ otel_version = self._get_opentelemetry_version()
+ if otel_version:
+ content = re.sub(
+ r'opentelemetry-bom:.*?(?=\nzstd-jni:)',
+ self._opentelemetry_license_yaml_block(otel_version),
+ content,
+ flags=re.DOTALL)
Review Comment:

Relying on a hardcoded next key like `zstd-jni:` in the regular expression
is fragile. If the order of keys in `dep_urls_java.yaml` changes, or if
`zstd-jni:` is removed or renamed, this regex will match up to the next
occurrence of `zstd-jni:` (or fail to match), potentially deleting or
corrupting large sections of the YAML file.
Instead, you can match up to the next top-level YAML key (which starts at
the beginning of a line with no indentation followed by a colon).
```suggestion
content = re.sub(
r'opentelemetry-bom:.*?(?=\n[^ \n]+:)',
self._opentelemetry_license_yaml_block(otel_version),
content,
flags=re.DOTALL)
```
##########
scripts/tools/bomupgrader.py:
##########
@@ -254,18 +255,54 @@ def write_back(self):
for line in self.target_lines:
fout.write(line)
+ def _get_opentelemetry_version(self):
+ for line in self.target_lines:
+ match = self.VERSION_STRING.match(line)
+ if match and match.group(1) == 'opentelemetry':
+ return match.group(2)
+ logging.warning('opentelemetry_version not found in BeamModulePlugin')
+ return None
+
+ @staticmethod
+ def _opentelemetry_license_yaml_block(version):
+ license_url = (
+ 'https://raw.githubusercontent.com/open-telemetry/'
+ 'opentelemetry-java/v{}/LICENSE'.format(version))
+ return (
+ 'opentelemetry-bom:\n'
+ " '{version}':\n"
+ ' license: "{license_url}"\n'
+ ' type: "Apache License 2.0"\n'
+ 'opentelemetry-bom-alpha:\n'
+ " '{version}-alpha':\n"
+ ' license: "{license_url}"\n'
+ ' type: "Apache License 2.0"\n'.format(
+ version=version, license_url=license_url))
Review Comment:

The indentation for the `opentelemetry-bom-alpha` version block has 3 spaces
of indentation (` '{version}-alpha':`), whereas `opentelemetry-bom` uses 2
spaces (` '{version}':`). It is better to keep the indentation consistent at 2
spaces for both blocks.
```python
'opentelemetry-bom-alpha:\n'
" '{version}-alpha':\n"
' license: "{license_url}"\n'
' type: "Apache License 2.0"\n'.format(
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]