Updating Apex to OpenStack Ocata
[apex.git] / build / functions.sh
1 ##############################################################################
2 # Copyright (c) 2016 Dan Radez (Red Hat) and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 clone_fork () {
11     # ARG 1: opnfv-tht or opnfv-puppet-tripleo
12     echo "Cloning $1"
13
14     # Use apex tripleo-heat-templates fork
15     local ghcreds=""
16     local pr_num=""
17     local ref="stable/euphrates"
18     local repo="https://github.com/trozet/$1"
19
20     if git log -1 | grep "${1}-pr:" | grep -o '[0-9]*'; then
21       pr_num=$(git log -1 | grep "${1}-pr:" | grep -o '[0-9]*')
22     fi
23
24     if [ "$pr_num" != "" ]; then
25       echo "Using pull request $pr_num from $repo"
26       # Source credentials since we are rate limited to 60/day
27       if [ -f ~/.githubcreds ]; then
28         source ~/.githubcreds
29         ghcreds=" -u $GHUSERNAME:$GHACCESSTOKEN"
30       fi
31
32       PR=$(curl $ghcreds https://api.github.com/repos/trozet/$1/pulls/$pr_num)
33
34       # Do not pull from merged branches
35       MERGED=$(python -c "import json; print json.loads('''$PR'''.replace('\n', '').replace('\r', ''))['merged']")
36       if [ "$MERGED" == "False" ]; then
37         ref=$(python -c "import json; print json.loads('''$PR'''.replace('\n', '').replace('\r', ''))['head']['ref']")
38         echo "Setting GitHub Ref to: $REF"
39         repo=$(python -c "import json; print json.loads('''$PR'''.replace('\n', '').replace('\r', ''))['head']['repo']['clone_url']")
40         echo "Setting GitHub URL to: $repo"
41       fi
42     fi
43
44     rm -rf $1
45     git clone $repo -b $ref $1
46 }