Skip to content

Commit

Permalink
repair sync_repo() by conditionally git pull'ing (#313)
Browse files Browse the repository at this point in the history
* git over ssh support

* consisting protocol expectation

* source utilities

* refactor common logic and only conditionally git pull (git merge)

* invoke function

* use get_github_url()

* log instead of echo
  • Loading branch information
sdolenc authored and eltoncarr committed Mar 30, 2018
1 parent 8b84704 commit 5632d12
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 44 deletions.
9 changes: 7 additions & 2 deletions onebox.sh
Expand Up @@ -272,8 +272,13 @@ get_current_org()

if git status > /dev/null ; then
remoteUrl="$(git config --get remote.origin.url)"
if echo $remoteUrl | grep -i "http" > /dev/null ; then
organization=$(echo $remoteUrl | tr / "\n" | head -4 | tail -1)

if echo $repoInfo | grep "@.*:.*/" > /dev/null 2>&1 ; then
#ssh
organization=$(echo $remoteUrl | tr : "\n" | tr / "\n" | head -2 | tail -1)
else
#http or https
organization=$(echo $remoteUrl | tr / "\n" | tail -2 | head -1)
fi
fi

Expand Down
2 changes: 1 addition & 1 deletion playbooks/roles/edxapp/files/enable_msft_oauth.sh
Expand Up @@ -27,7 +27,7 @@ fix_platform()

if ! ( grep -i "live" lms/envs/aws.py ) ; then
log "Ensure remote has commit"
add_remote msft_plat https://github.com/microsoft/edx-platform.git
add_remote msft_plat $(get_github_url microsoft edx-platform)

# Ficus fix. Apply
# https://github.com/Microsoft/edx-platform/pull/158
Expand Down
2 changes: 1 addition & 1 deletion scripts/bootstrap-db.sh
Expand Up @@ -280,7 +280,7 @@ parse_args $@ # pass existing command line arguments
##
BOOTSTRAP_HOME=$(dirname $0)
OXA_PATH=/oxa
OXA_TOOLS_REPO="https://github.com/microsoft/oxa-tools.git"
OXA_TOOLS_REPO=$(get_github_url microsoft oxa-tools)
OXA_TOOLS_PATH=$OXA_PATH/oxa-tools
OXA_TOOLS_CONFIG_PATH=$OXA_PATH/oxa-tools-config
CONFIGURATION_PATH=$OXA_PATH/configuration
Expand Down
4 changes: 2 additions & 2 deletions scripts/bootstrap.sh
Expand Up @@ -313,15 +313,15 @@ fix_npm_python()
fix_hosts_file()
{
# Apply https://github.com/Microsoft/edx-configuration/pull/90
add_remote msft_conf "https://github.com/microsoft/edx-configuration.git"
add_remote msft_conf $(get_github_url microsoft edx-configuration)
if grep "127.0.0.1 localhost" playbooks/roles/local_dev/tasks/main.yml ; then
cherry_pick_wrapper f3d59dd09dbbd8b60c9049292c3c814f4de715c5 "$EDXAPP_SU_EMAIL"
fi
}

ansible_try_catch()
{
add_remote msft_conf "https://github.com/microsoft/edx-configuration.git"
add_remote msft_conf $(get_github_url microsoft edx-configuration)

# Apply https://github.com/Microsoft/edx-configuration/pull/91
cherry_pick_wrapper a6304eaaefc24d2c3c59d57606c059cdd75b1dd4 "$EDXAPP_SU_EMAIL"
Expand Down
23 changes: 22 additions & 1 deletion templates/stamp/utilities.sh
Expand Up @@ -618,7 +618,13 @@ sync_repo()
else
pushd $repo_path

sudo git fetch --all --tags --prune
if is_valid_branch $(get_current_branch) ; then
# git pull is a fetch then merge, but merge only
# makes sense when the local repo has a branch.
sudo git pull --all --tags --prune
else
sudo git fetch --all --tags --prune
fi
exit_on_error "Failed syncing repository $repo_url to $repo_path"

popd
Expand Down Expand Up @@ -664,6 +670,21 @@ cherry_pick_wrapper()
exit_on_error "Failed to cherry pick essential fix"
}

get_current_branch()
{
# Current branch is prefixed with an asterisk. Remove it.
local prefix='* '
echo $(git branch | grep "$prefix" | sed "s/$prefix//g")
}

is_valid_branch()
{
local branch=$1

# Is branch useful?
[[ -n "$branch" ]] && [[ $branch != null ]] && [[ $branch != *"no branch"* ]] && [[ $branch != *"detached"* ]]
}

#############################################################################
# Create theme directory before edx playbook
#############################################################################
Expand Down
35 changes: 17 additions & 18 deletions tests/deploy-onebox.sh
Expand Up @@ -15,13 +15,12 @@ get_branch()
elif [[ -n $TRAVIS_PULL_REQUEST_BRANCH ]] ; then
branchInfo=$TRAVIS_PULL_REQUEST_BRANCH
else
# Current branch is prefixed with an asterisk. Remove it.
local prefix='* '
branchInfo=`git branch | grep "$prefix" | sed "s/$prefix//g"`
branchInfo=$(get_current_branch)

# Ensure branch information is useful.
if [[ -z "$branchInfo" ]] || [[ $branchInfo == *"no branch"* ]] || [[ $branchInfo == *"detached"* ]] ; then
branchInfo="oxa/dev.fic"
if ! is_valid_branch $branchInfo ; then
log "Unable to determine branch for testing"
exit 1
fi
fi

Expand All @@ -30,29 +29,29 @@ get_branch()

get_repo()
{
local protocol="https://"
local repoInfo=

if [[ -n $CIRCLE_PROJECT_USERNAME ]] && [[ $CIRCLE_PROJECT_REPONAME ]] ; then
repoInfo="github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"
elif [[ -n $TRAVIS_REPO_SLUG ]] ; then
repoInfo="github.com/${TRAVIS_REPO_SLUG}"
if [[ -n $CIRCLE_PROJECT_USERNAME ]] && [[ -n $CIRCLE_PROJECT_REPONAME ]] ; then
repoInfo=$(get_github_url $CIRCLE_PROJECT_USERNAME $CIRCLE_PROJECT_REPONAME)
else
if [[ -n $CIRCLE_REPOSITORY_URL ]] ; then
repoInfo=$CIRCLE_REPOSITORY_URL
repoInfo="${protocol}${CIRCLE_REPOSITORY_URL}"
else
repoInfo=$(git config --get remote.origin.url)
fi

# Convert ssh repo url into https
# Convert ssh repo into https
if echo $repoInfo | grep "@.*:.*/" > /dev/null 2>&1 ; then
echo $repoInfo | tr @ "\n" | tr : / | tail -1
return
repoInfo=$(echo $repoInfo | tr : / | sed "s#git@#${protocol}#g")
fi
fi

echo "$repoInfo"
}

CURRENT_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source $CURRENT_SCRIPT_DIR/../templates/stamp/utilities.sh
BRANCH=$(get_branch)

set -o pipefail
Expand All @@ -61,10 +60,10 @@ REPO=$(get_repo)
FOLDER=$(basename $REPO .git)
CONTAINER_NAME=$(echo "$ONEBOX_PARAMS" | tr -d "-" | tr -d " ")

echo "BRANCH=$BRANCH, REPO=$REPO, FOLDER=$FOLDER"
echo "ONEBOX_PARAMS=$ONEBOX_PARAMS"
echo "CONTAINER_NAME=$CONTAINER_NAME"
echo
log "BRANCH=$BRANCH, REPO=$REPO, FOLDER=$FOLDER"
log "ONEBOX_PARAMS=$ONEBOX_PARAMS"
log "CONTAINER_NAME=$CONTAINER_NAME"
log

# keep alive
bash ./tests/keep-alive.sh &
Expand Down Expand Up @@ -92,7 +91,7 @@ fi
# clone repo
mkdir /oxa
pushd /oxa
if git clone --quiet --depth=50 --branch=$BRANCH https://${REPO} ; then
if git clone --quiet --depth=50 --branch=$BRANCH ${REPO} ; then
echo "success: clone repo inside of container"
else
echo "FAILURE: can't clone repo inside of container"
Expand Down
30 changes: 11 additions & 19 deletions tests/filterBranch.sh
Expand Up @@ -3,16 +3,14 @@

set -e

get_current_branch()
get_branch()
{
local branchInfo=

if [[ -n $CIRCLE_BRANCH ]] ; then
branchInfo=$CIRCLE_BRANCH
else
# Current branch is prefixed with an asterisk. Remove it.
local prefix='* '
branchInfo=$(git branch | grep "$prefix" | sed "s/$prefix//g")
branchInfo=$(get_current_branch)
fi

echo "$branchInfo"
Expand All @@ -32,52 +30,46 @@ get_base_branch()
echo "$baseBranch"
}

is_valid_branch()
{
local branch=$1

# Is branch useful?
[[ -n "$branch" ]] && [[ $branch != null ]] && [[ $branch != *"no branch"* ]] && [[ $branch != *"detached"* ]]
}

branch_in_list()
{
local branch=$1

for filter in $ONLY_BRANCHES; do
if [[ $branch == $filter ]] ; then
echo
echo "branch in filter"
log "branch in filter"
return 0
fi
done

return 1
}

CURRENT_SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
env
source $CURRENT_SCRIPT_DIR/../templates/stamp/utilities.sh
sudo apt -qq update > /dev/null 2>&1
sudo apt -qq install -y jq curl > /dev/null 2>&1
sudo apt -qq install -y jq curl > /dev/null 2>&1

echo
echo "ONLY_BRANCHES=$ONLY_BRANCHES"
log "ONLY_BRANCHES=$ONLY_BRANCHES"

current_branch=$(get_current_branch)
echo "current_branch=$current_branch"
current_branch=$(get_branch)
log "current_branch=$current_branch"
if is_valid_branch $current_branch ; then
if branch_in_list $current_branch ; then
exit 0
fi
fi

base_branch=$(get_base_branch)
echo "base_branch=$base_branch"
log "base_branch=$base_branch"
if is_valid_branch $base_branch ; then
if branch_in_list $base_branch ; then
exit 0
fi
fi

echo
echo "branch NOT in filter"
log "branch NOT in filter"
exit 1

0 comments on commit 5632d12

Please sign in to comment.