Skip to content

Instantly share code, notes, and snippets.

@aharonha
Last active September 15, 2024 12:05
Show Gist options
  • Save aharonha/ae331568baa147ec8087377aaec0acc9 to your computer and use it in GitHub Desktop.
Save aharonha/ae331568baa147ec8087377aaec0acc9 to your computer and use it in GitHub Desktop.
Script for creating both release and merge-back pull requests in github.
#!/usr/bin/env bash
RED='\033[0;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
BLUE='\033[0;94m'
NC='\033[0m' # No Color
APP_FNAME=$(basename -- "$0")
APP="${APP_FNAME%.*}"
function setupBranches() {
[ ! -f .branches.env ] || export $(grep -v '^#' .branches.env | xargs)
if [ -z "$MAIN" ]; then
export MAIN=main;
fi
if [ -z "$STAGING" ]; then
export STAGING=staging;
fi
if [ -z "$PRODUCTION" ]; then
export PRODUCTION=production;
fi
}
export SPRINT=$1
if [ -z "$SPRINT" ]; then
echo -e $RED please run $0 SPRINT \( gitPreRelease \| gitRelease \| gitMergeBackProdToStage \| gitMergeBackStageToDev \) $NC
exit 30;
fi
shift
if [ -z "$ACTION" ]; then
export ACTION=$1;
fi
if [ -z "$ACTION" ]; then
echo -e $RED please run $0 $SPRINT \( gitPreRelease \| gitRelease \| gitMergeBackProdToStage \| gitMergeBackStageToDev \) $NC
exit 30;
fi
if [ "$#" -gt 0 ]; then shift; fi
if [ -z "$FOLDER_NAME" ]; then
export FOLDER_NAME=$1;
fi;
if [ -z "$FOLDER_NAME" ]; then
export FOLDER_NAME=~/git
fi
installer=`which brew`
which gh >/dev/null || $installer install gh
if [ $? -ne 0 ]; then
if [ -z "$installer" ] ; then
echo -e $RED Unable to install gh, unable to find an installer.$NC
echo -e $RED Get yourself one of brew, yum, or apt!$NC
echo Someone told me that you can install brew simply running this:
echo -e $YELLOW '/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"' $NC
echo -e $YELLOW 'echo "$(brew shellenv)" >> ~/.bash_profile' $NC
echo -e $YELLOW 'eval "$(brew shellenv)"' $NC
echo Try and tell me...
exit 82
fi
echo -e $RED unable to install gh, please run $installer install gh $NC
exit 67
fi
function gitLastUpdated() {
SRC=$1
TARGET=$2
echo -e $BLUE last updated:
gh pr list -H $SRC -B $TARGET --json commits -q ".[0].commits | map( select(.messageHeadline | startswith(\"Merge\") | not ) ) | max_by(.committedDate) | .committedDate"
echo -e $NC
}
function gitPreRelease() {
PREV_SRC=`git branch --show-current`
SRC=$MAIN
TARGET=$STAGING
git fetch origin $SRC:$SRC
git checkout $SRC
git fetch origin $TARGET:$TARGET
git checkout $TARGET
if [ $? -eq 0 ]; then
echo -e $BLUE Creating Pre Release: $NC
gh pr create -a @me -H $SRC -B $TARGET -t "Sprint $SPRINT: Pre-release" --body "Sprint $SPRINT: Pre-release"
echo -e $BLUE --- --- --- --- --- $NC
echo -e $GREEN Pre Release: Created $NC
gitLastUpdated $SRC $TARGET
fi
git checkout $PREV_SRC
}
function gitStatus() {
git status
}
function gitRelease() {
TARGET=$PRODUCTION
SRC=$STAGING
PREV_BRANCH=`git branch --show-current`
git fetch origin $TARGET:$TARGET
git fetch origin $SRC:$SRC
git checkout $TARGET
if [ $? -eq 0 ]; then
echo -e $BLUE Creating Release: $NC
gh pr create -a @me -H $SRC -B $TARGET -l production -l release -t "Sprint $SPRINT: Release" --body "Sprint $SPRINT: Release"
echo -e $BLUE --- --- --- --- --- $NC
PR_URL=$(gh pr list -H $SRC -B $TARGET --json url -q '.[0].url')
echo -e $GREEN ~~~ Release: Created ~~~ $NC
echo -e $BLUE Release: $PR_URL $NC
gitLastUpdated $SRC $TARGET
fi
git checkout $PREV_BRANCH
}
function gitReleaseAction() {
SRC=$1
TARGET=$2
ACTION_NAME=$3
PREV_SRC=`git branch --show-current`
git fetch origin $TARGET:$TARGET
git fetch origin $SRC:$SRC
git checkout $TARGET
if [ $? -eq 0 ]; then
echo -e $GREEN Creating $ACTION_NAME: $NC
gh label create $TARGET
gh label create $ACTION_NAME -d "$ACTION_NAME"
gh pr create -a @me -B $SRC -H $TARGET -l $TARGET -l $ACTION_NAME -t "Sprint $SPRINT: $ACTION_NAME" --body "Sprint $SPRINT: $ACTION_NAME"
echo -e $GREEN --- --- --- --- --- $NC
echo -e $GREEN $ACTION_NAME: Created $NC
gitLastUpdated $SRC $TARGET
fi
git checkout $PREV_SRC
}
function gitMergeBackProdToStage() {
SRC=$PRODUCTION
TARGET=$STAGING
SRC=mergeback/$SPRINT/$SRC-$TARGET-mergeback-$SPRINT
gitMergeBack $SRC $TARGET $SRC
}
function gitCloseMergeBackProdToStage() {
SRC=$PRODUCTION
TARGET=$STAGING
SRC=mergeback/$SPRINT/$SRC-$TARGET-mergeback-$SPRINT
gitCloseMergeBack $SRC $TARGET $SRC
}
function gitMergeBackStageToDev() {
SRC=$STAGING
TARGET=$MAIN
SRC=mergeback/$SPRINT/$SRC-$TARGET-mergeback-$SPRINT
gitMergeBack $SRC $TARGET $SRC
}
function gitCloseMergeBackStageToDev() {
SRC=$STAGING
TARGET=$MAIN
SRC=mergeback/$SPRINT/$SRC-$TARGET-mergeback-$SPRINT
gitCloseMergeBack $SRC $TARGET $SRC
}
function gitMergeBack() {
SRC=$1
TARGET=$2
SRC=$3
echo -e $YELLOW --- gitMergeBack $SRC $TARGET $SRC --- $NC
PREV_SRC=`git branch --show-current`
git fetch origin $TARGET:$TARGET
git fetch origin $SRC:$SRC
git branch -D -r $SRC
git branch $SRC $TARGET
git checkout $SRC
git merge $SRC
if [ $? -eq 0 ]; then
git push --set-upstream origin $SRC
gh pr create -a @me -H $SRC -B $TARGET -t "Sprint $SPRINT: $SRC to $TARGET merge back" --body "Sprint $SPRINT: $SRC to $TARGET merge back"
if [ $? -eq 0 ]; then
echo -e $GREEN $PWD ~~~ Created ~~~ $NC
gitLastUpdated $SRC $TARGET
else
git branch -D -r $SRC
git push origin -d $SRC
git checkout $PREV_SRC
fi
else
git branch -D -r $SRC
git push origin -d $SRC
git checkout $PREV_SRC
fi
}
function gitCloseMergeBack() {
SRC=$1
TARGET=$2
SRC=$3
echo -e $YELLOW --- Close Merge Backs $SRC =\> $TARGET $SRC --- $NC
PR_URL=$(gh pr list -H $SRC -B $TARGET --json url -q '.[0].url')
if [ "$PR_URL" != "" ]; then
echo -e $BLUE +++ URL : $PR_URL +++ $NC
gh pr close $PR_URL -d -c "Automatically closed by $APP"
git pull
git prune
else
echo -e $YELLOW --- Close Merge Backs nothing to do! $SRC $TARGET $SRC --- $NC
fi
}
function gitCloseReleasePreRelease() {
SRC=$1
TARGET=$2
echo -e $YELLOW --- Close Release/Pre Release $SRC =\> $TARGET $SRC --- $NC
PR_URL=$(gh pr list -H $SRC -B $TARGET --json url -q '.[0].url')
if [ "$PR_URL" != "" ]; then
echo -e $BLUE +++ URL : $PR_URL +++ $NC
gh pr close $PR_URL -d -c "Automatically closed by $APP"
git pull
git prune
else
echo -e $YELLOW --- Close Release/Pre Release nothing to do! $SRC $TARGET $SRC --- $NC
fi
}
function gitCloseRelease() {
gitCloseReleasePreRelease $STAGING $PRODUCTION
}
function gitClosePreRelease() {
gitCloseReleasePreRelease $MAIN $STAGING
}
function pushd_ () {
command pushd "$@" > /dev/null || exit
}
function popd_ () {
command popd > /dev/null || exit
}
pushd_ $FOLDER_NAME || exit
clear
echo -e $BLUE -+-+-+- $APP -+-+-+- $NC
for f in `cat services.list`; do
setupBranches
DIR_NAME=$(basename -- "$f")
DIR="${DIR_NAME%.*}"
if [[ -d $f ]]; then
# $f is a directory, but skip if you can't pushd to it
pushd_ "$f" || continue
setupBranches
echo -e $GREEN +-+-+-+ Running $BLUE $ACTION $GREEN on $BLUE $DIR $GREEN +-+-+- $NC
# Change this to the required action
eval "$ACTION"
if [ $? -eq 0 ]; then
echo -e $GREEN +-+-+-+ SUCCESS: $BLUE $ACTION $GREEN on $BLUE $DIR $GREEN +-+-+- $NC
else
echo -e $RED +-+-+-+ FAILED: $BLUE $ACTION $RED on $BLUE $DIR $RED +-+-+- $NC
fi
popd_
fi
done
echo -e $GREEN ~~ DONE ~~ $NC
popd_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment