From 0b2a51a93f974b9273eb011829a0c0489b7f00b3 Mon Sep 17 00:00:00 2001 From: Derek Stavis Date: Thu, 1 Oct 2015 00:40:22 -0300 Subject: [PATCH] Get PR URL and Branch using GitHub API --- .travis.yml | 2 +- tools/branch-name.sh | 16 ---------------- tools/travis-github-pr-integration.sh | 13 +++++++++++++ 3 files changed, 14 insertions(+), 17 deletions(-) delete mode 100755 tools/branch-name.sh create mode 100755 tools/travis-github-pr-integration.sh diff --git a/.travis.yml b/.travis.yml index 6f20a20..60c128a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -9,7 +9,7 @@ env: - OMF_REPO_BRANCH="$TRAVIS_BRANCH" before_install: - - export OMF_REPO_BRANCH=`tools/branch-name.sh` + - source tools/travis-github-pr-integration.sh - docker build -t fish . < Dockerfile before_script: diff --git a/tools/branch-name.sh b/tools/branch-name.sh deleted file mode 100755 index 7fa4e56..0000000 --- a/tools/branch-name.sh +++ /dev/null @@ -1,16 +0,0 @@ -#!/usr/bin/env sh - -URL=https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$TRAVIS_PULL_REQUEST - -BRANCH_NAME=`curl -s $URL ^/dev/null \ - | grep -C 2 "head" \ - | grep \"ref\" \ - | cut -d':' -f2- \ - | sed -e 's/["|,]//g;s/^[ \t]//g' - ` - -if [ -n "$BRANCH_NAME" ]; then - echo $BRANCH_NAME -else - echo $TRAVIS_BRANCH -fi diff --git a/tools/travis-github-pr-integration.sh b/tools/travis-github-pr-integration.sh new file mode 100755 index 0000000..fb0ac36 --- /dev/null +++ b/tools/travis-github-pr-integration.sh @@ -0,0 +1,13 @@ +# Return if we are not in a Pull Request +[[ "$TRAVIS_PULL_REQUEST" = "false" ]] && return + +GITHUB_PR_URL=https://api.github.com/repos/$TRAVIS_REPO_SLUG/pulls/$TRAVIS_PULL_REQUEST +GITHUB_PR_BODY=$(curl -s $GITHUB_PR_URL 2>/dev/null) + +if [[ $GITHUB_PR_BODY =~ \"ref\":\ *\"([a-zA-Z0-9_-]*)\" ]]; then + export OMF_REPO_BRANCH=${BASH_REMATCH[1]} +fi + +if [[ $GITHUB_PR_BODY =~ \"repo\":.*\"clone_url\":\ *\"(https://github\.com/[a-zA-Z0-9_-]*/[a-zA-Z0-9_-]*\.git).*\"base\" ]]; then + export OMF_REPO_URI=${BASH_REMATCH[1]} +fi