Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve custom DC stable release PR creation script #4735

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 112 additions & 33 deletions scripts/create_customdc_stable_update_pr.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,38 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# Opens a PR to merge new changes into the custom DC stable branch.
# Facilitates opening a PR to merge new changes into the custom DC stable branch.
# - Prompts for what commit to release at
# - Creates local and remote branches at that commit
# - Opens a web browser with pre-filled PR details

# Usage:
# ./scripts/create_customdc_stable_update_pr.sh

# If the script fails and you need to clean up old branches:
# ./scripts/create_customdc_stable_update_pr.sh --delete

# To skip branch creation and reuse existing branches:
# ./scripts/create_customdc_stable_update_pr.sh --reuse

set -e

MASTER_BRANCH=master
STABLE_BRANCH=customdc_stable
UPDATE_BRANCH=cdcStableUpdate

# Parse option flag if present
mode="create"
if [[ "$1" == "--delete" ]]; then
mode="delete"
elif [[ "$1" == "--reuse" ]]; then
mode="reuse"
elif [[ "$1" != "" ]]; then
echo "Unknown option: $1"
echo "Available options are '--delete' and '--reuse'."
exit 1
fi

# Check if GitHub CLI is installed
if ! command -v gh &> /dev/null; then
echo "GitHub CLI is not installed. Please install it to continue."
Expand All @@ -39,66 +63,121 @@ fi
if gh repo set-default --view 2>&1 >/dev/null | grep "no default repository"; then
# No default repository is set for GitHub CLI.
# We don't have to print an error message because the above already prints one.
echo "Choose your *forked repo* as the default."
echo "Choose datacommonsorg/website as the default."
exit 1
fi

# Find the remote associated with the main repo
upstream_name=$(git remote -v | grep "datacommonsorg" | awk '{print $1}' | head -n 1)
if [ -z "$upstream_name" ]; then
upstream_remote=$(git remote -v | grep "datacommonsorg" | awk '{print $1}' | head -n 1)
if [ -z "$upstream_remote" ]; then
echo "No remote found with 'datacommonsorg' in its URL."
exit 1
fi
echo "Remote for main repo is '${upstream_name}'".
echo "Remote for main repo is '${upstream_remote}'".

# Find the remote associated with the forked repo
fork_name=$(git remote -v | grep -v "datacommonsorg" | awk '{print $1}' | head -n 1)
if [ -z "$fork_name" ]; then
fork_remote=$(git remote -v | grep -v "datacommonsorg" | awk '{print $1}' | head -n 1)
if [ -z "$fork_remote" ]; then
echo "No remote found without 'datacommonsorg' in its URL."
exit 1
fi
echo "Remote for forked repo is '${fork_name}'".
echo "Remote for forked repo is '${fork_remote}'".

fork_owner=$(gh repo view $(git remote get-url origin) --json owner --jq '.owner.login')
fork_name=$(gh repo view $(git remote get-url origin) --json name --jq '.name')
echo "${fork_remote} = ${fork_owner}/${fork_name}"
echo ""

# If delete flag is specified, delete local and remote branches then exit.
if [[ $mode == "delete" ]]; then
if ! git merge-base --is-ancestor "${fork_remote}/${UPDATE_BRANCH}" "${upstream_remote}/${MASTER_BRANCH}"; then
echo "Remote branch "${fork_remote}/${UPDATE_BRANCH}" has unmerged changes. Not deleting."
exit 1
fi
echo "Deleting branch ${UPDATE_BRANCH} on both local and remote..."
git branch -d "$UPDATE_BRANCH"
git push "$fork_remote" --delete "$UPDATE_BRANCH"
echo ""
echo "Ready to re-run without '--delete'."
exit 0
fi

# Check whether a local update branch already exists.
if git show-ref --verify --quiet "refs/heads/${UPDATE_BRANCH}"; then
echo "A local branch named '${UPDATE_BRANCH}' already exists."
echo "Delete it, then re-run this script."
exit 1
# Fail unless mode is reuse
if [[ $mode != "reuse" ]]; then
echo "A local branch named '${UPDATE_BRANCH}' already exists."
echo "Run with '--delete' to remove it or '--reuse' to use it."
exit 1
fi
elif [[ $mode == "reuse" ]]; then
echo "Local branch '${UPDATE_BRANCH}' does not exist."
echo "Run without '--reuse' to create it."
fi

# Check whether a remote update branch already exists.
if git ls-remote --heads "$fork_name" "$UPDATE_BRANCH" | grep -q "$UPDATE_BRANCH"; then
echo "A branch named '${UPDATE_BRANCH}' already exists on remote '${fork_name}'".
echo "Delete it, then re-run this script."
exit 1
if git ls-remote --heads "$fork_remote" "$UPDATE_BRANCH" | grep -q "$UPDATE_BRANCH"; then
if [[ $mode != "reuse" ]]; then
echo "A branch named '${UPDATE_BRANCH}' already exists on remote '${fork_remote}'".
echo "Run with '--delete' to remove it or '--reuse' to use it."
exit 1
fi
elif [[ $mode == "reuse" ]]; then
echo "Remote branch '${UPDATE_BRANCH}' does not exist."
echo "Run without '--reuse' to create it."
fi

# Fetch relevant branches from the main repo
git fetch $upstream_name $MASTER_BRANCH $STABLE_BRANCH
if [[ $mode == "create" ]]; then
# Fetch relevant branches from the main repo
git fetch $upstream_remote $MASTER_BRANCH $STABLE_BRANCH

# Show the user the commits that are in master but not in customdc_stable
echo ""
echo "The following commits are in ${MASTER_BRANCH} but not in ${STABLE_BRANCH}:"
echo ""
git log --pretty=format:"%h %s" ${upstream_name}/${STABLE_BRANCH}..${upstream_name}/${MASTER_BRANCH} --reverse
echo ""
# Show the user the commits that are in master but not in customdc_stable
echo ""
echo "The following commits are in ${MASTER_BRANCH} but not in ${STABLE_BRANCH}:"
echo ""
git log --pretty=format:"%h %s" ${upstream_remote}/${STABLE_BRANCH}..${upstream_remote}/${MASTER_BRANCH} --reverse
echo ""

# Ask the user to select a commit
read -p "Enter the commit hash to create the branch from: " commit_hash
# Ask the user to select a commit
read -p "Enter the commit hash to create the branch from: " commit_hash

# Create a new branch at the selected commit
git checkout -b "$UPDATE_BRANCH" "$commit_hash"
# Create a new branch at the selected commit
git branch "$UPDATE_BRANCH" "$commit_hash"
git push -u $fork_remote $UPDATE_BRANCH
fi

# Get the current date in YYYY-MM-DD format
current_date=$(date +%Y-%m-%d)

echo "Creating PR..."

# Open a draft PR using GitHub CLI
# Construct the PR body
pr_body="# Highlights
- **TODO** List changes to custom DC since last stable release"

# Add temporary reference links to PR body
generate_comparison_url() {
local submod_name=$1
local stable_commit=$(git rev-parse --short "$upstream_remote/$STABLE_BRANCH:${submod_name}")
local update_commit=$(git rev-parse --short "$UPDATE_BRANCH:${submod_name}")
echo "https://github.com/datacommonsorg/${submod_name}/compare/${stable_commit}...${update_commit}"
}
mixer_comparison_url=$(generate_comparison_url mixer)
import_comparison_url=$(generate_comparison_url import)
pr_body+="

# Resources - REMOVE BEFORE MERGING
- Diff links to help with writing highlights:
- Mixer: ${mixer_comparison_url}
- Import: ${import_comparison_url}
- Website changes are listed on this page.
- If you're unsure what affects custom DC, please message the team."

echo "Opening PR creation UI..."

# Open the PR creation UI using GitHub CLI
gh pr create \
--repo datacommonsorg/website \
--base customdc_stable \
--head "${fork_name}:${UPDATE_BRANCH}" \
--head "${fork_owner}:${fork_name}:${UPDATE_BRANCH}" \
--title "$current_date Custom DC stable release" \
--body "TODO: Summarize changes since last stable release." \
--draft
--body "$pr_body" \
--web
Loading