Skip to content

Instantly share code, notes, and snippets.

@kushal
Last active January 25, 2021 07:23
Show Gist options
  • Save kushal/656331109dcce1849fd7a18d52ae8ee5 to your computer and use it in GitHub Desktop.
Save kushal/656331109dcce1849fd7a18d52ae8ee5 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
function ynprompt {
read -n 1 -r -p "KEEP GOING? [Y/n] " response
if [[ $response =~ ^([nN])$ ]]
then
exit
fi
}
function lint {
# we need errors to be allowed so we can grab them
set +e
./server/scripts/lint.sh
if [ $? -eq 1 ]; then
echo "CIRCLECI WILL FAIL WITH THIS CHANGE."
read -n 1 -r -p "KEEP GOING? [y/N] " response
if [[ $response =~ ^([yY])$ ]]; then
echo "STRANGE CHOICE, BUT OKAY"
else
exit;
fi
fi
# restore error failing
set -e
}
function newreview {
ASSIGNEES=""
ASSIGNEES_JSON=""
COMMA=""
for HANDLE in "$@"
do
HANDLE=$(./server/scripts/slacktogithub.sh $HANDLE)
ASSIGNEES="$ASSIGNEES$COMMA$HANDLE"
ASSIGNEES_JSON="$ASSIGNEES_JSON$COMMA\"$HANDLE\""
COMMA=", "
done
echo "HERE'S YOUR DIFF"
git diff origin/master || true
ynprompt
lint
REPO=tryscroll/scroll
BRANCH=review-${RANDOM}
echo "Sending review to $ASSIGNEES $BRANCH"
git stash save $BRANCH && git pull
if git stash list | grep "$BRANCH"
then
git stash pop
fi
git checkout -b $BRANCH
git add -A
git commit -a || true
git push -u origin $BRANCH
TITLE=$(git log -1 --pretty=%B|head -1)
curl -d "{\"title\":\"$TITLE\",\"head\":\"$BRANCH\",\"base\":\"master\"}" "https://api.github.com/repos/$REPO/pulls?access_token=$TOKEN" > proutput
ISSUE=$( grep number proutput | head | tr -dc '0-9')
curl -d "{\"assignees\":[$ASSIGNEES_JSON]}" "https://api.github.com/repos/$REPO/issues/$ISSUE/assignees?access_token=$TOKEN" > assignoutput
rm proutput assignoutput
}
function update {
echo "HERE'S YOUR DIFF"
git diff || true
ynprompt
lint
echo "COMMITTING"
git add -A
git commit -a --amend --reset-author --no-edit
echo "REBASING"
git fetch
git rebase origin/master
echo "PUSHING UPDATES TO ${CURRENT_BRANCH}"
git push -f
}
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD)
if [ -z "$1" ]
then
echo "Must provide at least one assignee"
exit
fi
if [ ! -e ~/.ghtoken ]
then
echo "First create a token at https://github.com/settings/tokens with access to the repo and add to ~/.ghtoken"
echo "(Also make sure you're in the notification server's user map!)"
exit
fi
TOKEN=$(cat ~/.ghtoken)
if [ "`git rev-parse --show-cdup`" != "" ]; then cd `git rev-parse --show-cdup`; fi
if [[ $CURRENT_BRANCH == review* ]] ; then
if [ "$1" != "update" ]; then
echo "YOU ARE ON $CURRENT_BRANCH, PLEASE RUN ./review.sh update TO PUSH CHANGES"
exit
else
echo "UPDATING $CURRENT_BRANCH WITH YOUR LOCAL CHANGES"
fi
update
exit
elif [[ $CURRENT_BRANCH != master ]] ; then
echo "YOU ARE ON $CURRENT_BRANCH, WHICH IS NOT A REVIEW BRANCH. REVIEWS MUST BE STARTED FROM 'master'"
exit
elif [ "$1" == "update" ]; then
echo "YOU ARE ON 'master', YOU MUST SPECIFY THE NAME OF A REVIEWER AND NOT 'update'"
exit
fi
newreview "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment