Skip to content

Instantly share code, notes, and snippets.

@pasdam
Last active March 4, 2024 10:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pasdam/2a1af55be1bcf95120373a4f0b5fc427 to your computer and use it in GitHub Desktop.
Save pasdam/2a1af55be1bcf95120373a4f0b5fc427 to your computer and use it in GitHub Desktop.
Script to automatically create a PR to main
#!/bin/sh
set -e
REPO_DIR=$1
if [ -z "$REPO_DIR" ]; then
REPO_DIR="./"
fi
REPO_DIR=$(git rev-parse --show-toplevel)
if [ "$REPO_DIR" = ".git" ]; then
REPO_DIR=$(dirname $(git rev-parse --show-toplevel))
fi
cd "$REPO_DIR"
if [ -f "$REPO_DIR/.githooks/pre-commit" ]; then
"$REPO_DIR/.githooks/pre-commit"
if [ $? -ne 0 ]; then
1>&2 echo "[ERROR] Pre-commit validation failed, please fix it"
exit 1
fi
fi
terraform fmt -check -recursive
if [ $? -ne 0 ]; then
1>&2 echo "[ERROR] Terraform format validation failed, please fix it"
exit 1
fi
COMMITS=$(git --no-pager log --reverse --oneline $(git remote show origin | sed -n '/HEAD branch/s/.*: //p')..HEAD)
TICKETS=($(echo "$COMMITS" | egrep -o "[A-Z]{2,4}-\d{2,5}" | sort | uniq))
for ticket in "${TICKETS[@]}"; do
TITLE="${TITLE}${ticket} "
TICKETS_LINKS="${TICKETS_LINKS}[${ticket}](https://sadapay.atlassian.net/browse/${ticket})/"
done
CHANGELOG=$(printf "%s" "$COMMITS" | sed 's/^.*:/-/g')
BODY="# ${TICKETS_LINKS::${#TICKETS_LINKS}-1}
## Behavior Changes
$CHANGELOG
## Engineering Notes
-"
echo "Commits:"
printf "%s\n\n" "$COMMITS"
echo "Tickets: $TITLE"
read -p "PR title (press ENTER to use the first commit message): " TITLE
if [ -z "$TITLE" ]; then
TITLE=$(echo "$COMMITS" | head -n1 | sed -E 's/^[0-9a-z]+ //')
fi
gh pr create --draft --body "$BODY" --title "$TITLE" --reviewer "@SadaPay/infrastructure-engineers"
while true; do
printf "Do you want to switch to master branch (y|yes / n|no)? "
read response
case $response in
y | yes)
current_branch=$(git rev-parse --abbrev-ref HEAD)
default_branch=$(git symbolic-ref refs/remotes/origin/HEAD | sed 's@^refs/remotes/origin/@@')
git switch $default_branch
git branch -d $current_branch
break ;;
n | no)
break ;;
*)
echo 1>&2 "Invalid answer: $response"
;;
esac
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment