Last active
July 10, 2023 20:59
-
-
Save geekflyer/95e61e967a3276fbbf669fedb495b3dd to your computer and use it in GitHub Desktop.
migrate from branch to fork based PR workflow on aptos-labs/aptos-core
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# run this script via: curl -sSL https://gist.github.com/geekflyer/95e61e967a3276fbbf669fedb495b3dd/raw/migrate-from-branch-based-to-fork-based-prs.sh | bash | |
set -e pipefail | |
# Verify whether the GitHub CLI is installed and prompt the user to install it if necessary | |
if ! command -v gh &>/dev/null; then | |
echo "GitHub CLI not found. Please install it from https://cli.github.com/ and then re-run this script." | |
exit 1 | |
fi | |
# Verify whether the user is authenticated with the GitHub CLI and authenticate if necessary | |
if ! gh auth status &>/dev/null; then | |
echo "GitHub CLI not authenticated. Starting login flow..." | |
# Start the GitHub CLI login flow | |
gh auth login -h github.com -p https | |
fi | |
# get the current github username from the github cli | |
GITHUB_USERNAME=$(gh api /user | jq -r '.login') | |
echo "Your GitHub username is $GITHUB_USERNAME" | |
# Check whether the script is being run inside inside a git remote that has any remote that points to the aptos-core repository | |
if ! git remote -v | grep -q "aptos-labs/aptos-core"; then | |
echo "Please run this script inside a clone of the aptos-core repository." | |
exit 1 | |
fi | |
# Check whether the user is already using a fork-based PR workflow | |
if git remote -v | grep -q "origin.*aptos-labs/aptos-core"; then | |
echo "renaming remote origin to upstream" | |
git remote rename origin upstream | |
elif git remote -v | grep -q "origin.*$GITHUB_USERNAME/aptos-core"; then | |
echo "You appear to already be using a fork-based PR workflow. Nothing to do here." | |
exit 0 | |
else | |
echo "Encountered an unexpected list of remotes. Please send the output of \"git remote -v\" to @christian on Slack." | |
git remote -v | |
exit 0 | |
fi | |
# Check whether the user has a repository named "aptos-core" under their own GitHub username | |
echo "Checking whether you have a fork of the aptos-core repository..." | |
if ! gh api "/repos/$GITHUB_USERNAME/aptos-core" &>/dev/null; then | |
echo "Forking the aptos-labs/aptos-core repository using the GitHub CLI" | |
gh repo fork aptos-labs/aptos-core --default-branch-only --clone=false | |
fi | |
# # Add a new origin remote pointing to the user's fork | |
git remote add origin "$(gh api "/repos/$GITHUB_USERNAME/aptos-core" | jq -r '.clone_url')" | |
gh repo set-default aptos-labs/aptos-core | |
echo "" | |
echo "Done! You can now push your changes to your fork and open a PR against the aptos-labs/aptos-core repository." | |
echo "" | |
echo "To do so run: git push -u origin $(git branch --show-current)" | |
echo "And then create a PR via: gh pr create --draft" | |
echo "" | |
echo "To pull in the latest changes from the upstream main run: git pull upstream main --rebase" | |
echo "" | |
echo "If you have any questions, please reach out to #prod-eng and @christian on Slack." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
run this script via
curl -sSL https://gist.github.com/geekflyer/95e61e967a3276fbbf669fedb495b3dd/raw/migrate-from-branch-based-to-fork-based-prs.sh | bash