Skip to content

Commit b1fed2a

Browse files
authored
DEVPROD-3981: Migration of marketplace APIs v2 -> v3 (#1131)
1 parent e3f26c8 commit b1fed2a

1 file changed

Lines changed: 13 additions & 38 deletions

File tree

src/test/scripts/update_versions.py

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
import json
21
import logging
3-
import urllib.request
42

53
import requests
64
import yaml
@@ -10,12 +8,15 @@
108
# USER_AGENT constant for marketplace API calls
119
USER_AGENT = "Mozilla_dc_core_eng"
1210

11+
# Marketplace API base host.
12+
MARKETPLACE_BASE_URL = "https://marketplace.atlassian.com"
13+
1314
"""
1415
This script is used to update the product versions in the helm charts descriptors (Chart.yaml).
1516
It fetches the latest available version (LTS for products with LTS) from marketplace and updates the required
1617
tag in the product chart. It also updates expected output for the product.
1718
18-
Script is currently executed manually and is in a fairly rough shape.
19+
Script is currently executed manually and is in a fairly rough shape.
1920
"""
2021

2122
logging.basicConfig(level=logging.INFO, format="%(levelname).1s %(message)s")
@@ -27,7 +28,7 @@
2728

2829
def update_versions(product_to_update, new_version):
2930
products_to_update = [product_to_update]
30-
if product == 'bamboo':
31+
if product_to_update == 'bamboo':
3132
products_to_update.append("bamboo-agent")
3233

3334
chart_files = [f'../../main/charts/{p}/Chart.yaml' for p in products_to_update]
@@ -62,43 +63,19 @@ def update_expected_output(products_to_update, new_version):
6263
logging.info('Updated expected output file: %s', output_file)
6364

6465

65-
def product_versions_marketplace(product_key):
66-
mac_url = 'https://marketplace.atlassian.com'
67-
request_url = f'/rest/2/products/key/{product_key}/versions'
68-
params = {'offset': 0, 'limit': 50}
69-
versions = set()
70-
page = 1
71-
while True:
72-
logging.debug(f'Retrieving Marketplace product versions for {product_key}: page {page}')
73-
headers = {'User-Agent': USER_AGENT}
74-
r = requests.get(mac_url + request_url, params=params, headers=headers)
75-
version_data = r.json()
76-
for version in version_data['_embedded']['versions']:
77-
if all(d.isdigit() for d in version['name'].split('.')):
78-
logging.debug(f"Adding version {version['name']}")
79-
versions.add(version['name'])
80-
if 'next' not in version_data['_links']:
81-
break
82-
request_url = version_data['_links']['next']['href']
83-
page += 1
84-
params = {}
85-
logging.debug(f'Found {len(versions)} versions')
86-
return sorted(list(versions), reverse=True)
87-
88-
89-
def latest_minor(version, mac_versions):
90-
major_minor_version = '.'.join(version.split('.')[:2])
91-
minor_versions = [v for v in mac_versions
92-
if v.startswith(f'{major_minor_version}.')]
93-
minor_versions.sort(key=lambda s: [int(u) for u in s.split('.')])
94-
return minor_versions[-1]
66+
def latest_marketplace_version(product_key):
67+
"""Return the latest version string for a host product via the v3
68+
`parent-software` API. Uses `?limit=1`; v3 returns the list in
69+
descending buildNumber order, so the first item is the latest."""
70+
url = f"{MARKETPLACE_BASE_URL}/rest/3/parent-software/{product_key}/versions"
71+
r = requests.get(url, params={'limit': 1}, headers={'User-Agent': USER_AGENT})
72+
return r.json()['versions'][0]['versionNumber']
9573

9674

9775
def update_mesh_tag():
9876
logging.info("-------------------------------")
9977
logging.info('- Updating Bitbucket Mesh tag -')
10078
logging.info("-------------------------------")
101-
mesh_repo = 'atlassian/bitbucket-mesh'
10279
new_version = product_versions.get_lts_version(['mesh']).replace(tag_suffix, "")
10380
bitbucket_values_file = '../../main/charts/bitbucket/values.yaml'
10481
expected_bitbucket_output_file = '../resources/expected_helm_output/bitbucket/output.yaml'
@@ -134,9 +111,7 @@ def update_mesh_tag():
134111
logging.info("Latest LTS version: %s", version)
135112
else:
136113
logging.info("Non-LTS product")
137-
headers = {'User-Agent': USER_AGENT}
138-
r = requests.get(f'https://marketplace.atlassian.com/rest/2/products/key/{product}/versions/latest', headers=headers)
139-
version = r.json()['name']
114+
version = latest_marketplace_version(product)
140115

141116
new_version_tag = f"{version}{tag_suffix}"
142117
logging.info(f"Latest version: %s, tagname: {version}{tag_suffix}", version)

0 commit comments

Comments
 (0)