Skip to content

Commit

Permalink
Remove interactive questions for release.create-rc (#32127)
Browse files Browse the repository at this point in the history
  • Loading branch information
CelianR authored Dec 16, 2024
1 parent e681f1f commit e56059f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
40 changes: 31 additions & 9 deletions tasks/libs/common/git.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from __future__ import annotations

import os
import sys
import tempfile
from contextlib import contextmanager
from time import sleep
from typing import TYPE_CHECKING

from invoke import Context
Expand Down Expand Up @@ -161,25 +163,45 @@ def check_base_branch(branch, release_version):
return branch == get_default_branch() or branch == release_version.branch()


def try_git_command(ctx, git_command):
"""
Try a git command that should be retried (after user confirmation) if it fails.
def try_git_command(ctx, git_command, non_interactive_retries=2, non_interactive_delay=5):
"""Try a git command that should be retried (after user confirmation) if it fails.
Primarily useful for commands which can fail if commit signing fails: we don't want the
whole workflow to fail if that happens, we want to retry.
Args:
ctx: The invoke context.
git_command: The git command to run.
non_interactive_retries: The number of times to retry the command if it fails when running non-interactively.
non_interactive_delay: The delay in seconds to retry the command if it fails when running non-interactively.
"""

do_retry = True
n_retries = 0
interactive = sys.stdin.isatty()

while do_retry:
res = ctx.run(git_command, warn=True)
if res.exited is None or res.exited > 0:
print(
color_message(
f"Failed to run \"{git_command}\" (did the commit/tag signing operation fail?)",
"orange",
if interactive:
print(
color_message(
f"Failed to run \"{git_command}\" (did the commit/tag signing operation fail?)",
"orange",
)
)
do_retry = yes_no_question("Do you want to retry this operation?", color="orange", default=True)
else:
# Non interactive, retry in `non_interactive_delay` seconds if we haven't reached the limit
n_retries += 1
if n_retries > non_interactive_retries:
print(f'{color_message("Error", Color.RED)}: Failed to run git command', file=sys.stderr)
return False

print(
f'{color_message("Warning", Color.ORANGE)}: Retrying git command in {non_interactive_delay}s',
file=sys.stderr,
)
)
do_retry = yes_no_question("Do you want to retry this operation?", color="orange", default=True)
sleep(non_interactive_delay)
continue

return True
Expand Down
33 changes: 16 additions & 17 deletions tasks/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,21 @@
BACKPORT_LABEL_COLOR = "5319e7"


def deduce_and_ask_version(ctx, branch, as_str=True, trust=False) -> str | Version:
def deduce_version(ctx, branch, as_str=True, trust=False) -> str | Version:
release_version = get_next_version_from_branch(ctx, branch, as_str=as_str)

if trust:
return release_version
print(
f'{color_message("Info", Color.BLUE)}: Version {release_version} deduced from branch {branch}', file=sys.stderr
)

if not os.isatty(sys.stdin.fileno()) or yes_no_question(
f'Version {release_version} deduced from branch {branch}. Is this the version you want to use?',
color="orange",
default=False,
if (
trust
or not os.isatty(sys.stdin.fileno())
or yes_no_question(
'Is this the version you want to use?',
color="orange",
default=False,
)
):
return release_version

Expand Down Expand Up @@ -170,7 +175,7 @@ def update_modules(ctx, release_branch=None, version=None, trust=False):

assert release_branch or version

agent_version = version or deduce_and_ask_version(ctx, release_branch, trust=trust)
agent_version = version or deduce_version(ctx, release_branch, trust=trust)

with agent_context(ctx, release_branch, skip_checkout=release_branch is None):
modules = get_default_modules()
Expand Down Expand Up @@ -235,7 +240,7 @@ def tag_modules(

assert release_branch or version

agent_version = version or deduce_and_ask_version(ctx, release_branch, trust=trust)
agent_version = version or deduce_version(ctx, release_branch, trust=trust)

tags = []
with agent_context(ctx, release_branch, skip_checkout=release_branch is None):
Expand Down Expand Up @@ -274,7 +279,7 @@ def tag_version(

assert release_branch or version

agent_version = version or deduce_and_ask_version(ctx, release_branch, trust=trust)
agent_version = version or deduce_version(ctx, release_branch, trust=trust)

# Always tag the main module
force_option = __get_force_option(force)
Expand Down Expand Up @@ -463,12 +468,6 @@ def create_rc(ctx, release_branch, patch_version=False, upstream="origin", slack
# Step 1: Update release entries
print(color_message("Updating release entries", "bold"))
new_version = next_rc_version(ctx, major_version, patch_version)
if not yes_no_question(
f'Do you want to create release candidate with:\n- new version: {new_version}\n- new highest version: {new_highest_version}\n- new final version: {new_final_version}?',
color="bold",
default=False,
):
raise Exit(color_message("Aborting.", "red"), code=1)

update_release_json(new_version, new_final_version)

Expand Down Expand Up @@ -1257,7 +1256,7 @@ def create_github_release(ctx, release_branch, draft=True):
)

notes = []
version = deduce_and_ask_version(ctx, release_branch)
version = deduce_version(ctx, release_branch)

with agent_context(ctx, release_branch):
for section, filename in sections:
Expand Down

0 comments on commit e56059f

Please sign in to comment.