Skip to content

Commit

Permalink
chore(scripts/create-adaptation-pr): Add automatic mode using zulip-s…
Browse files Browse the repository at this point in the history
…end (#17060)
  • Loading branch information
jcommelin committed Sep 23, 2024
1 parent 3b44012 commit 2625464
Showing 1 changed file with 105 additions and 9 deletions.
114 changes: 105 additions & 9 deletions scripts/create-adaptation-pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,52 @@ set -e # abort whenever a command in the script fails
# So please do not delete the following line, or the final two lines of this script.
{

if [ $# -ne 2 ]; then
# Default values
AUTO="no"

# Function to display usage
usage() {
echo "Usage: $0 <BUMPVERSION> <NIGHTLYDATE>"
echo " or"
echo " $0 --bumpversion=<BUMPVERSION> --nightlydate=<NIGHTLYDATE> [--auto=<yes|no>]"
echo "BUMPVERSION: The upcoming release that we are targeting, e.g., 'v4.10.0'"
echo "NIGHTLYDATE: The date of the nightly toolchain currently used on 'nightly-testing'"
echo "AUTO: Optional flag to specify automatic mode, default is 'no'"
exit 1
}

# Parse arguments
if [ $# -eq 2 ] && [[ $1 != --* ]] && [[ $2 != --* ]]; then
BUMPVERSION=$1
NIGHTLYDATE=$2
elif [ $# -ge 2 ]; then
for arg in "$@"; do
case $arg in
--bumpversion=*)
BUMPVERSION="${arg#*=}"
shift
;;
--nightlydate=*)
NIGHTLYDATE="${arg#*=}"
shift
;;
--auto=*)
AUTO="${arg#*=}"
shift
;;
*)
usage
;;
esac
done
else
usage
fi

BUMPVERSION=$1 # "v4.10.0"
NIGHTLYDATE=$2 # "2024-06-25"
# Validate required arguments
if [ -z "$BUMPVERSION" ] || [ -z "$NIGHTLYDATE" ]; then
usage
fi

# Check if 'gh' command is available
if ! command -v gh &> /dev/null; then
Expand All @@ -38,8 +75,13 @@ if [ "$status" != "completed" ]; then
gh run list --branch nightly-testing
exit 1
else
echo "The latest commit on 'nightly-testing' is still running CI."
read -p "Press enter to continue, or ctrl-C if you'd prefer to wait for CI."
if [ "$AUTO" = "yes" ]; then
echo "Auto mode enabled. Bailing out because the latest commit on 'nightly-testing' is still running CI."
exit 1
else
echo "The latest commit on 'nightly-testing' is still running CI."
read -p "Press enter to continue, or ctrl-C if you'd prefer to wait for CI."
fi
fi
fi

Expand Down Expand Up @@ -79,6 +121,13 @@ if git diff --name-only --diff-filter=U | grep -q .; then
fi
fi

if git diff --name-only --diff-filter=U | grep -q . || ! git diff-index --quiet HEAD --; then
if [ "$AUTO" = "yes" ]; then
echo "Auto mode enabled. Bailing out due to unresolved conflicts or uncommitted changes."
exit 1
fi
fi

# Loop until all conflicts are resolved and committed
while git diff --name-only --diff-filter=U | grep -q . || ! git diff-index --quiet HEAD --; do
echo
Expand Down Expand Up @@ -111,6 +160,13 @@ if git diff --name-only --diff-filter=U | grep -q .; then
git add lean-toolchain lake-manifest.json
fi

if git diff --name-only --diff-filter=U | grep -q .; then
if [ "$AUTO" = "yes" ]; then
echo "Auto mode enabled. Bailing out due to unresolved conflicts or uncommitted changes."
exit 1
fi
fi

# Check if there are more merge conflicts
if git diff --name-only --diff-filter=U | grep -q .; then
echo
Expand Down Expand Up @@ -143,16 +199,21 @@ if git diff --name-only bump/$BUMPVERSION bump/nightly-$NIGHTLYDATE | grep -q .;
echo "Here is a suggested 'gh' command to do this:"
gh_command="gh pr create -t \"$pr_title\" -b '' -B bump/$BUMPVERSION"
echo "> $gh_command"
echo "Shall I run this command for you? (y/n)"
read answer
if [ "$AUTO" = "yes" ]; then
echo "Auto mode enabled. Running the command..."
answer="y"
else
echo "Shall I run this command for you? (y/n)"
read answer
fi
if [ "$answer" != "${answer#[Yy]}" ]; then
gh_output=$(eval $gh_command)
# Extract the PR number from the output
pr_number=$(echo $gh_output | sed 's/.*\/pull\/\([0-9]*\).*/\1/')
fi

echo
echo "### [user] post a link to the PR on Zulip"
echo "### [auto/user] post a link to the PR on Zulip"

zulip_title="#$pr_number adaptations for nightly-$NIGHTLYDATE"
zulip_body="> $pr_title #$pr_number"
Expand All @@ -161,7 +222,33 @@ if git diff --name-only bump/$BUMPVERSION bump/nightly-$NIGHTLYDATE | grep -q .;
echo "Here is a suggested message:"
echo "Title: $zulip_title"
echo " Body: $zulip_body"
read -p "Press enter to continue"

if command -v zulip-send >/dev/null 2>&1; then
zulip_command="zulip-send --stream nightly-testing --subject \"$zulip_title\" --message \"$zulip_body\""
echo "Here is a suggested 'zulip-send' command to do this:"
echo "> $zulip_command"

if [ "$AUTO" = "yes" ]; then
echo "Auto mode enabled. Running the command..."
answer="y"
else
echo "Shall I run this command for you? (y/n)"
read answer
fi

if [ "$answer" != "${answer#[Yy]}" ]; then
eval $zulip_command
fi
else
echo "Zulip CLI is not installed. Please install it to send messages automatically."
if [ "$AUTO" = "yes" ]; then
exit 1
fi
fi

if [ "$AUTO" != "yes" ]; then
read -p "Press enter to continue"
fi

# else, let the user know that no PR is needed
else
Expand Down Expand Up @@ -195,6 +282,15 @@ if git diff --name-only --diff-filter=U | grep -q .; then
fi
fi

if git diff --name-only --diff-filter=U | grep -q . || ! git diff-index --quiet HEAD --; then
if [ "$AUTO" = "yes" ]; then
echo "Auto mode enabled. Bailing out due to unresolved conflicts or uncommitted changes."
echo "PR has been created, and message posted to Zulip."
echo "Error occured while merging the new branch into 'nightly-testing'."
exit 2
fi
fi

# Loop until all conflicts are resolved and committed
while git diff --name-only --diff-filter=U | grep -q . || ! git diff-index --quiet HEAD --; do
echo
Expand Down

0 comments on commit 2625464

Please sign in to comment.