Skip to content

Commit d9ab91d

Browse files
Merge pull request #6476 from agullon/USHIFT-6769
USHIFT-6786: Preserve GITOPS_VERSION when access.redhat.com API fetch fails
2 parents 0b83947 + 2d538ff commit d9ab91d

1 file changed

Lines changed: 18 additions & 9 deletions

File tree

test/bin/pyutils/generate_common_versions.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,23 +214,25 @@ def get_gitops_version(minor_version):
214214
"""
215215
url = "https://access.redhat.com/product-life-cycles/api/v1/products"
216216
params = {"name": "Red Hat OpenShift GitOps"}
217-
resp = None
217+
versions = []
218218
for attempt in range(1, 4):
219219
try:
220220
resp = requests.get(url, params=params, timeout=10)
221221
resp.raise_for_status()
222+
data = resp.json()
223+
product_entries = data.get("data") or []
224+
if not product_entries:
225+
raise ValueError("Missing GitOps lifecycle data")
226+
versions = product_entries[0].get("versions", [])
222227
break
223-
except Exception as e:
228+
except (requests.RequestException, ValueError, AttributeError) as e:
229+
if attempt == 3:
230+
logging.error(f"Failed to fetch GitOps lifecycle data from {url} after 3 attempts: {e}")
231+
return None
224232
logging.warning(f"Attempt {attempt} failed with error: {e}. Retrying...")
225233
time.sleep(2)
226-
continue
227-
228-
if resp is None:
229-
logging.error(f"Failed to fetch data from {url} after 3 attempts")
230-
return ""
231-
data = resp.json()
232234
for current_microshift_minor_version in range(minor_version, minor_version - 4, -1):
233-
for gitops_version_from_api_docs in data.get("data", [{}])[0].get("versions", []):
235+
for gitops_version_from_api_docs in versions:
234236
gitops_version_ocp_compatibility = gitops_version_from_api_docs.get("openshift_compatibility") or ""
235237
gitops_version_number = gitops_version_from_api_docs.get("name")
236238
if f"4.{current_microshift_minor_version}" in gitops_version_ocp_compatibility:
@@ -289,8 +291,15 @@ def generate_common_versions(minor_version):
289291
rhocp_minor_y2 = yminus2_minor_version
290292

291293
# The current version of the microshift-gitops package.
294+
# If the API fetch fails, preserve the existing value to avoid
295+
# creating PRs that clear the version due to transient failures.
292296
logging.info("Getting GITOPS_VERSION")
293297
gitops_version = get_gitops_version(minor_version)
298+
if gitops_version is None:
299+
target_file = pathlib.Path(__file__).resolve().parent / '../common_versions.sh'
300+
args = ['grep', '-oP', '(?<=GITOPS_VERSION=).*', str(target_file)]
301+
gitops_version = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, check=True).stdout.strip()
302+
logging.info(f"API fetch failed, preserving existing GITOPS_VERSION={gitops_version}")
294303

295304
template_path = pathlib.Path(__file__).resolve().parent / '../../assets/common_versions.sh.template'
296305

0 commit comments

Comments
 (0)