Skip to content

Commit

Permalink
asd
Browse files Browse the repository at this point in the history
  • Loading branch information
regner committed May 14, 2018
1 parent c3923b4 commit 51d01f6
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,30 @@ jobs:
path: /tmp/circleci-artifacts
- store_artifacts:
path: /tmp/circleci-test-results

upstream_sync:
working_directory: ~/2600hz/kazoo
docker:
- image: circleci/python:3.6.5-jessie
steps:
- checkout
- run: ./scripts/sync-upstream-git.sh master
- run: ./scripts/sync-upstream-git.sh 4.1
- run: ./scripts/sync-upstream-git.sh 4.2

workflows:
version: 2
commit:
jobs:
- build

nightly:
jobs:
- upstream_sync
triggers:
- schedule:
cron: "0 * * * *"
filters:
branches:
only:
- otf
105 changes: 105 additions & 0 deletions scripts/sync-upstream-git.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/bash

# Environment Variable Requirements:
# - kazootestkey: A base64 encoded private SSH key that has its public key added to the repo
# - GITHUB_ACCOUNT: Account name to use when creating PR requests
# - GITHUB_TOKEN: Personal access token for the GITHUB_ACCOUNT

IFS=$'\n'

update_branch() {
echo "Updating $1 branch..."
git checkout $1
git reset --hard $1
git merge upstream/$1
git push origin $1
}

setup_ssh() {
echo "Adding SSH key..."
echo $kazootestkey | base64 --decode > ~/.ssh/kazootestkey
chmod 400 ~/.ssh/kazootestkey
ssh-add -D
ssh-add ~/.ssh/kazootestkey
}

setup_git() {
echo "Setting up git config..."
git config --global user.email "[email protected]"
git config --global user.name "OTF Git Syncaroo"

echo "Adding upstream..."
git remote add upstream https://github.com/2600hz/kazoo.git

echo "Fetching from upstream..."
git fetch upstream
}

cleanup() {
# Doing this for debug purposes, if we need to debug the script
# then we need to be back on the otf branch as that is the only
# place the script exists.
git checkout origin/otf-master
git reset --hard HEAD
}

generate_pr_post_body() {
cat <<EOF
{
"title": "Upstream merge: $1",
"body": "Automated daily PR. Deal with this ASAP please! :D",
"head": "$2",
"base": "otf-$1"
}
EOF
}

pr_upstream() {
PRS=$(curl --silent -X GET "https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/kazoo/pulls")
COUNT=$(echo $PRS | jq '. | length')
DATE=`date +%Y-%m-%d`
BRANCH="upstream-sync-$1-$DATE"

if [ "$COUNT" != "0" ]
then
for prtitle in $(echo "$PRS" | jq -r '.[].title'); do
if [ "$prtitle" == "Upstream merge: $1" ]
then
echo "Found existing PR for this branch ($1), skipping the creation of a new one."
return
fi
done
fi

git checkout origin/$1
git reset --hard origin/$1
git fetch
git checkout -b $BRANCH
git push -u origin $BRANCH

curl --silent \
-u $GITHUB_ACCOUNT:$GITHUB_TOKEN \
-H "Content-Type: application/json" \
-X POST "https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/kazoo/pulls" \
--data "$(generate_pr_post_body $1 $BRANCH)"
}

# Quick test to see if we should run this script. If you're
# trying to debug/run this script in your repo add your
# username for circleci to this section.
VALID_USERNAMES="Regner OpenTelecom"

if [[ "$CIRCLE_PROJECT_USERNAME" =~ ^(Regner|OpenTelecom)$ ]]; then
echo "$CIRCLE_PROJECT_USERNAME is in the list of repos to run against..."
else
echo "$CIRCLE_PROJECT_USERNAME is not in the list of repos to run against. Aborting."
exit 0
fi

setup_ssh
setup_git

update_branch $1
pr_upstream $1

cleanup

0 comments on commit 51d01f6

Please sign in to comment.