Skip to content

Commit 85ada50

Browse files
authored
Fix version update when a major.minor.0-beta.1 already exists (#45413)
Fix version update when a major.minor.0-beta.1 already exists
1 parent 4293198 commit 85ada50

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

eng/versioning/set_versions.py

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@
3636
from datetime import timedelta
3737
import os
3838
import re
39-
import sys
4039
import time
40+
import urllib.request
4141
from utils import BuildType
4242
from utils import CodeModule
43-
from utils import UpdateType
4443
from utils import version_regex_str_with_names_anchored
4544
from utils import prerelease_data_version_regex
4645
from utils import prerelease_version_regex_with_name
46+
import xml.etree.ElementTree as ET
4747

4848
# some things that should not be updated for devops builds, in the case where everything is being updated in one call
4949
items_we_should_not_update = ['com.azure:azure-sdk-all', 'com.azure:azure-sdk-parent', 'com.azure:azure-client-sdk-parent', 'com.azure.v2:azure-client-sdk-parent', 'com.azure:azure-data-sdk-parent', 'com.azure:azure-perf-test-parent', 'com.azure:azure-code-customization-parent', 'io.clientcore:clientcore-parent']
@@ -275,7 +275,9 @@ def increment_or_set_library_version(build_type, artifact_id, group_id, new_vers
275275
module = CodeModule(stripped_line)
276276
# Tick up the version here. If the version is already a pre-release
277277
# version then just increment the revision. Otherwise increment the
278-
# minor version, zero the patch and add "-beta.1" to the end
278+
# minor version, zero the patch and determine which "-beta.x" version
279+
# to use based on what has been released to Maven central. If "beta.1"
280+
# exists in Maven central then use "beta.2" and so on.
279281
# https://github.com/Azure/azure-sdk/blob/main/docs/policies/releases.md#java
280282
if module.name == library_to_update and hasattr(module, 'current'):
281283
artifact_found = True
@@ -302,9 +304,10 @@ def increment_or_set_library_version(build_type, artifact_id, group_id, new_vers
302304
rev += 1
303305
new_version = '{}.{}.{}-beta.{}'.format(vmatch.group('major'), vmatch.group('minor'), vmatch.group('patch'), str(rev))
304306
else:
307+
major = int(vmatch.group('major'))
305308
minor = int(vmatch.group('minor'))
306309
minor += 1
307-
new_version = '{}.{}.{}-beta.1'.format(vmatch.group('major'), minor, 0)
310+
new_version = '{}.{}.0-beta.{}'.format(major, minor, get_beta_version_to_use(group_id, artifact_id, major, minor))
308311
# The dependency version only needs to be updated it if is different from the current version.
309312
# This would be the case where a library hasn't been released yet and has been released (either GA or preview)
310313
if (module.dependency != module.current):
@@ -334,6 +337,46 @@ def increment_or_set_library_version(build_type, artifact_id, group_id, new_vers
334337
for line in newlines:
335338
f.write(line)
336339

340+
# Determines which beta version to use when incrementing the version of a library without a version specified.
341+
def get_beta_version_to_use(group_id: str, artifact_id: str, major_version: int, minor_version: int):
342+
# Pull version information from Maven central to determine which beta version to use.
343+
# If beta.1 exists then use beta.2, etc. If beta.1 doesn't exist then use beta.1
344+
url = 'https://repo1.maven.org/maven2/{}/{}/maven-metadata.xml'.format(group_id.replace('.', '/'), artifact_id)
345+
with urllib.request.urlopen(urllib.request.Request(url = url, method='GET')) as f:
346+
if (f.status != 200):
347+
raise ValueError('Unable to get maven-metadata.xml for groupId {} and artifactId {}. The status code was {}'.format(group_id, artifact_id, f.status))
348+
349+
# maven-metadata.xml as the following format:
350+
# <metadata>
351+
# <groupId>groupId</groupId>
352+
# <artifactId>artifactId</artifactId>
353+
# <versioning>
354+
# <latest>version-latest</latest>
355+
# <release>version-release</release>
356+
# <versions>
357+
# <version>a-version</version>
358+
# ... (more versions)
359+
# </versions>
360+
# </versioning>
361+
# </metadata>
362+
root = ET.fromstring(f.read().decode('utf-8'))
363+
starts_with = '{}.{}.0-beta.'.format(major_version, minor_version)
364+
highest_beta_version = 0
365+
for version in root.findall('./versioning/versions//version'):
366+
version_text = version.text
367+
if version_text.startswith(starts_with):
368+
# A beta version with the same major and minor version was already released.
369+
# Track the beta version number, at the end of this function we'll return the
370+
# highest beta version match found + 1.
371+
# For example, if the version is 1.2.0-beta.3 exists, return 4.
372+
beta_version_str = version_text[len(starts_with):]
373+
if beta_version_str != '':
374+
beta_version_int = int(beta_version_str)
375+
if beta_version_int > highest_beta_version:
376+
highest_beta_version = beta_version_int
377+
378+
return highest_beta_version + 1
379+
337380
# Verify that the current version of an artifact matches our versioning scheme. This is meant to be called
338381
# as part of the release pipeline for a given artifact to verify that we don't accidentally release a version
339382
# that doesn't match our versioning scheme

0 commit comments

Comments
 (0)