Forked from matheusneder/vs-open-vsts-pr-code-for-review
Last active
April 22, 2020 19:55
-
-
Save allanpedroni/e5d92153e9cf47fcab77cd7951ee03be to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# PRE-REQ | |
# - INSTALL JQ | |
# - Required enviroment variables: | |
# export VSTS_BASE_URL = "(ex: https://YOUR_ORG.visualstudio.com/)" | |
# export VSTS_TOKEN = "(ex: xpto...)" | |
# export VS_DEVENV = "(ex: "/c/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/devenv.exe")" | |
set -e | |
function extract-branch-name | |
{ | |
cat - | sed 's/^refs\/heads\///g' | |
} | |
# Get PR info by ID | |
# Params: | |
# $1 - Pull Request ID | |
# Required enviroment variables | |
# VSTS_BASE_URL (ex: https://YOUR_ORG.visualstudio.com/) | |
# VSTS_TOKEN (ex: xpto...) | |
function vsts-get-pr-info-by-pr-id | |
{ | |
USAGE="$0 pull-request-id" | |
PRID=$1 | |
if [ -z "$PRID" ] || ! echo "$PRID" | egrep '^[1-9][0-9]*$' > /dev/null 2>&1 | |
then | |
echo $USAGE >> /dev/stderr | |
return 3 | |
fi | |
if [ -z "$VSTS_BASE_URL" ] || [ -z "$VSTS_TOKEN" ] | |
then | |
echo "Missing VSTS_BASE_URL and/or VSTS_TOKEN environmment variables." | |
return 4 | |
fi | |
URL="${VSTS_BASE_URL}_apis/git/pullrequests/$PRID?api-version=5.0" | |
PR_INFO=$(curl -s\ | |
-H"Content-Type: application/json" \ | |
-H"Authorization: Basic $(echo -n _:$VSTS_TOKEN | base64)" \ | |
"$URL" | \ | |
jq -c '. | { remoteUrl: .repository.remoteUrl, sourceRefName: .sourceRefName, targetRefName: .targetRefName }') | |
echo "$PR_INFO" | |
} | |
# Clone repository and checkout the provided branch. | |
# Params: | |
# $1 - Repository URL to clone | |
# $2 - Branch to checkout (PR source nranch) | |
# $3 - PR target branch (optional). When provided, it will perform a | |
# 'git diff --stat target-branch' | |
function checkout | |
{ | |
USAGE="$0 repo-url branch [target-pull-request-branch]" | |
REPO=$1 | |
BRANCH=$2 | |
TARGET_PR_BRANCH=$3 | |
TARGET_PR_BRANCH_PROVIDED=false | |
if [ ! -z "$TARGET_PR_BRANCH" ] | |
then | |
TARGET_PR_BRANCH_PROVIDED=true | |
fi | |
if [ -z "$BRANCH" ] | |
then | |
echo $USAGE >> /dev/stderr | |
return 1 | |
fi | |
WORKDIR=$(mktemp -d -t vs-checkout-XXXXXXXXXX) | |
echo "WORKDIR: $WORKDIR" | |
cd $WORKDIR | |
GIT_CLONE_XARGS='' | |
git clone $REPO | |
cd $(ls) | |
git checkout $BRANCH $GIT_CLONE_XARGS | |
if $TARGET_PR_BRANCH_PROVIDED | |
then | |
git diff --stat $TARGET_PR_BRANCH | |
fi | |
} | |
# Find for .sln files on current directory | |
# - if single sln file found: open it on Visual Studio | |
# - if multiple sln file found: prompt user to choose one then open it on Visual Studio | |
# - if no sln file found: Print error message to stderr then return with code 2 | |
# Required enviroment variables | |
# VS_DEVENV (ex: "/c/Program Files (x86)/Microsoft Visual Studio/2019/Enterprise/Common7/IDE/devenv.exe") | |
function open-vs-solution | |
{ | |
SLN_COUNT=0 | |
for SLN_PATH in $(find -name *.sln) | |
do | |
SLN_LIST[$SLN_COUNT]="$SLN_PATH" | |
SLN_COUNT=$((SLN_COUNT+1)) | |
done | |
if [ "$SLN_COUNT" -lt 1 ] | |
then | |
echo "No solution found." >> /dev/sdterr | |
return 2 | |
fi | |
SLN_TO_OPEN=${SLN_LIST[0]} | |
if [ "$SLN_COUNT" -gt 1 ] | |
then | |
echo "Multiple solution found:" | |
for i in $(seq 0 $((SLN_COUNT-1))) | |
do | |
echo "$i - ${SLN_LIST[$i]}" | |
done | |
while : ; do | |
echo -n "Choose one: "; | |
read USER_INPUT < /dev/stdin | |
if echo $USER_INPUT | egrep '^[0-9]+$' > /dev/null 2>&1 | |
then | |
if [ "$USER_INPUT" -lt "$SLN_COUNT" ] | |
then | |
break; | |
fi | |
fi | |
done | |
SLN_TO_OPEN=${SLN_LIST[$USER_INPUT]} | |
fi | |
cd $(dirname $SLN_TO_OPEN) | |
# Open solution on Visual Studio (detached) | |
( "$VS_DEVENV" "$(basename $SLN_TO_OPEN)"; cd $(dirname $WORKDIR); \ | |
sleep 30; rm -Rf $WORKDIR > /dev/null 2>&1 ) & | |
} | |
PRID=$1 | |
PR_INFO=$(vsts-get-pr-info-by-pr-id $PRID) | |
REPO=$(echo "$PR_INFO" | jq -r '.remoteUrl') | |
BRANCH=$(echo "$PR_INFO" | jq -r '.sourceRefName' | extract-branch-name) | |
TARGET_PR_BRANCH=$(echo "$PR_INFO" | jq -r '.targetRefName' | extract-branch-name) | |
echo "Pull Request URL (web): $REPO/pullrequest/$PRID" | |
checkout $REPO $BRANCH $TARGET_PR_BRANCH | |
open-vs-solution |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment