Skip to content

Commit ae23adf

Browse files
mtnbikencclaude
andcommitted
[hack] Address review feedback in create-release-tag.py
- Resolve git executable via shutil.which() and catch OSError in git() so callers always receive RuntimeError instead of an unhandled FileNotFoundError when git is missing or fails to launch - Use git rev-parse --verify refs/tags/<tag> in tag_exists() so only actual tags (not branches with the same name) return True - Replace regex date validation with date.fromisoformat() to reject invalid calendar dates such as 2026-02-31 at argument-parse time - Wrap fetch_bundle_info() and fetch_operator_push_date() calls in try/except so network, HTTP, or JSON errors surface as clean ERROR messages matching the existing git-failure error style Signed-off-by: Russell Teague <rteague@redhat.com> Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 192dbea commit ae23adf

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

hack/create-release-tag.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@
9999
# pylint: disable=invalid-name # hyphenated script name is intentional
100100

101101
import argparse
102+
from datetime import date
102103
import os
103104
import re
105+
import shutil
104106
import subprocess
105107
import sys
106108

@@ -211,11 +213,23 @@ def fetch_operator_push_date(version: str) -> str:
211213
# Git helpers
212214
# ---------------------------------------------------------------------------
213215

216+
_GIT_BIN = shutil.which("git")
217+
218+
214219
def git(*args, env=None) -> str:
215220
"""Run a git command and return stdout, raising on failure."""
216-
result = subprocess.run(
217-
["git", *args], capture_output=True, text=True, env=env, check=False
218-
)
221+
if not _GIT_BIN:
222+
raise RuntimeError("git executable not found in PATH")
223+
try:
224+
result = subprocess.run(
225+
[_GIT_BIN, *args],
226+
capture_output=True,
227+
text=True,
228+
env=env,
229+
check=False,
230+
)
231+
except OSError as exc:
232+
raise RuntimeError(f"failed to execute git: {exc}") from exc
219233
if result.returncode != 0:
220234
raise RuntimeError(result.stderr.strip())
221235
return result.stdout.strip()
@@ -224,7 +238,7 @@ def git(*args, env=None) -> str:
224238
def tag_exists(tag: str) -> bool:
225239
"""Return True if the git tag already exists in this repository."""
226240
try:
227-
git("rev-parse", tag)
241+
git("rev-parse", "-q", "--verify", f"refs/tags/{tag}")
228242
return True
229243
except RuntimeError:
230244
return False
@@ -294,8 +308,11 @@ def main():
294308
if not re.fullmatch(r"\d+\.\d+\.\d+", args.version):
295309
parser.error(f"version must be X.Y.Z, got: {args.version!r}")
296310

297-
if args.date and not re.fullmatch(r"\d{4}-\d{2}-\d{2}", args.date):
298-
parser.error(f"--date must be YYYY-MM-DD, got: {args.date!r}")
311+
if args.date:
312+
try:
313+
date.fromisoformat(args.date)
314+
except ValueError:
315+
parser.error(f"--date must be a valid YYYY-MM-DD date, got: {args.date!r}")
299316

300317
version = args.version
301318
tag = f"v{version}"
@@ -321,7 +338,14 @@ def main():
321338
commit_sha = args.commit
322339
commit_source = "provided manually"
323340
else:
324-
commit_sha, commit_source = fetch_bundle_info(version)
341+
try:
342+
commit_sha, commit_source = fetch_bundle_info(version)
343+
except Exception as err: # pylint: disable=broad-except
344+
print(
345+
f"\nERROR: fetch_bundle_info failed: {err}",
346+
file=sys.stderr,
347+
)
348+
sys.exit(1)
325349
if not commit_sha:
326350
print(
327351
f"\nERROR: Could not resolve commit SHA for {tag}.",
@@ -342,7 +366,14 @@ def main():
342366
published_date = args.date
343367
date_source = "provided manually"
344368
else:
345-
published_date = fetch_operator_push_date(version)
369+
try:
370+
published_date = fetch_operator_push_date(version)
371+
except Exception as err: # pylint: disable=broad-except
372+
print(
373+
f"\nERROR: fetch_operator_push_date failed: {err}",
374+
file=sys.stderr,
375+
)
376+
sys.exit(1)
346377
date_source = "operator image push date"
347378
if not published_date:
348379
print(

0 commit comments

Comments
 (0)