diff --git a/.push_test_table.sh b/.push_test_table.sh deleted file mode 100644 index b3dd96a8bb..0000000000 --- a/.push_test_table.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash - -# ----------------------------------------------------------------------- -# When pushing to a pull request on GitHub, Travis does two builds: -# (1) One for the pull request itself. In this case, $TRAVIS_PULL_REQUEST -# is 'false' and $TRAVIS_BRANCH contains the branch name -# (2) One to *simulate* a merge with master. In this case, $TRAVIS_PULL_REQUEST -# is the pull ID number and $TRAVIS_BRANCH = 'master' -# -# Read more about Travis environment variables -- -# http://docs.travis-ci.com/user/ci-environment/ -# ----------------------------------------------------------------------- - -# Only build test table if $TRAVIS_PULL_REQUEST is false -[ "${TRAVIS_PULL_REQUEST}" != "false" ] && exit 0 - -git config --global user.name "cpsievert" -git config --global user.email "cpsievert1@gmail.com" - -# Since Travis does `git clone --branch=$TRAVIS_BRANCH --depth=50`, -# we can't simply `git checkout master`. As far as I can tell, we are -# forced to re-clone -- https://gist.github.com/cpsievert/698c7f1404f972782e71 - -cd .. -rm -rf plotly/ -git clone https://github.com/ropensci/plotly.git -cd plotly -echo "user,SHA1,label" >> ../code_commits.csv -echo "ropensci,`git rev-parse HEAD`,master" >> ../code_commits.csv -git checkout $TRAVIS_BRANCH -echo "ropensci,`git rev-parse HEAD`,${TRAVIS_BRANCH}" >> ../code_commits.csv -Rscript -e "devtools::install()" - -cd .. -git clone https://github.com/ropensci/plotly-test-table.git -cd plotly-test-table -git checkout gh-pages - -mv ../code_commits.csv . -cat code_commits.csv -touch table.R -make - -# add, commit, push to gh-pages branch of plotly-test-table -./git-add.sh -git commit -a -m "Travis build number ${TRAVIS_BUILD_NUMBER} of ${TRAVIS_REPO_SLUG}" -# This post explains how this works -- http://rmflight.github.io/posts/2014/11/travis_ci_gh_pages.html -GH_REPO="@github.com/ropensci/plotly-test-table.git" -FULL_REPO="https://${GH_TOKEN}${GH_REPO}" -git pull $FULL_REPO gh-pages -git push $FULL_REPO gh-pages diff --git a/.travis.yml b/.travis.yml index f79a945730..afe1413a1a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,10 +12,10 @@ r_packages: - httr before_script: - - chmod 755 ./.push_test_table.sh + - chmod 755 inst/testscripts/.push_test_table.sh after_success: - - ./.push_test_table.sh + - inst/testscripts/.push_test_table.sh notifications: slack: diff --git a/inst/testscripts/.push_test_table.sh b/inst/testscripts/.push_test_table.sh new file mode 100644 index 0000000000..b6ef2e1976 --- /dev/null +++ b/inst/testscripts/.push_test_table.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +# exit on error +set -e + +# ----------------------------------------------------------------------- +# Travis does two types of builds: +# +# (1) A so-called "push". This essentially does a checkout on the most +# recent commit of the pull request, but *doesn't* merge with master. +# In this case, $TRAVIS_PULL_REQUEST = "false" +# (2) A so-called "pr" (pull request). This *does* merge with master. +# In this case, $TRAVIS_PULL_REQUEST contains the pull request number. +# ----------------------------------------------------------------------- + +# We need the pull request number to talk to the GitHub API, make comments, etc. +[ "${TRAVIS_PULL_REQUEST}" = "false" ] && exit 0 + +git config --global user.name "cpsievert" +git config --global user.email "cpsievert1@gmail.com" + +cd .. +git clone https://github.com/ropensci/plotly-test-table.git +cd plotly-test-table +git checkout gh-pages + +# Read more about Travis environment variables -- +# http://docs.travis-ci.com/user/ci-environment/ +Rscript ../plotly/inst/testscripts/comment.R $TRAVIS_PULL_REQUEST $TRAVIS_BUILD_ID $TRAVIS_COMMIT $GH_TOKEN diff --git a/inst/testscripts/comment.R b/inst/testscripts/comment.R new file mode 100644 index 0000000000..31786439ec --- /dev/null +++ b/inst/testscripts/comment.R @@ -0,0 +1,74 @@ +# first argument is the pull request number (TRAVIS_PULL_REQUEST) +# second is travis build ID (TRAVIS_BUILD_ID) +# third is the commit SHA1 currently being tested (TRAVIS_COMMIT) +# fourth is the github authentication token +a <- commandArgs(TRUE) +# gistr is a good reference for talking to the github API via httr +# https://github.com/ropensci/gistr/blob/master/R/zzz.R +library("httr") +base <- 'https://api.github.com/repos/ropensci/plotly/' +pr <- sprintf(paste0(base, 'pulls/%s'), a[1]) +header <- add_headers(`User-Agent` = "plotly", + `Accept` = 'application/vnd.github.v3+json', + `Authorization` = paste0("token ", a[4])) +# Must be successful since we grab the branch name for this pull request +# and SHA1 info from the request content +res <- GET(url = pr, header) +stop_for_status(res) +info <- content(res) +# find the branch name for this pull request +# http://stackoverflow.com/questions/15096331/github-api-how-to-find-the-branches-of-a-pull-request +branch <- strsplit(info$head$label, ":")[[1]][2] + +# plotly-test-table build script assumes we've checkout the dev branch. +# Note that travis does something like this for "pr" build: +#$ git fetch origin +refs/pull/number/merge: +#$ git checkout -qf FETCH_HEAD +# this leaves HEAD in a detached state, but we should be able to do: +# git checkout -b new_branch_name +setwd("../plotly") +if (system(paste("git checkout -b", branch)) != 0L) + stop(paste("Failed to 'git checkout -b'", branch, "branch")) +devtools::install() +setwd("../plotly-test-table") +cat("user,SHA1,label", file = "code_commits.csv") +row1 <- paste0("\nropensci,", info$base$sha, ",master") +cat(row1, file = "code_commits.csv", append = TRUE) +row2 <- paste0("\nropensci,", a[3], ",", branch) +cat(row2, file = "code_commits.csv", append = TRUE) + +# copy over file (created during Rscript) +# with sha/branch info for building test table +system("touch table.R") +if (system("make") != 0L) stop("Failed to 'make' test table") + +# add, commit, push to gh-pages branch of plotly-test-table +system("git add index.html") +system("git add tables/*/*.html") +system("git add data/*/*.png") +system("git add data/*/*.log") +build_link <- paste0('https://travis-ci.org/ropensci/plotly/builds/', a[2]) +commit_msg <- paste0('"Pushed from ', build_link, '"') +system(paste('git commit -m', commit_msg)) +# This post explains how this works -- http://rmflight.github.io/posts/2014/11/travis_ci_gh_pages.html +repo <- sprintf("https://%s@github.com/ropensci/plotly-test-table.git", a[4]) +system(paste("git pull -q", repo, "gh-pages")) +system(paste("git push -q", repo, "gh-pages")) + +# post comment if a link to this SHA doesn't exist +# (needed since Travis randomly re-builds stuff) +tbl_link <- sprintf("http://ropensci.github.io/plotly-test-table/tables/%s/index.html", a[3]) +msg <- sprintf("On TravisCI, commit %s was successfully merged with %s (master) to create %s. A visual testing table comparing %s with %s can be found here:\n %s", + info$head$sha, info$base$sha, a[3], info$base$sha, a[3], tbl_link) +msg <- paste("> The message below was automatically generated after build", build_link, "\n\n", msg) +commentz <- sprintf(paste0(base, 'issues/%s/comments'), a[1]) +res <- GET(commentz, header) +warn_for_status(res) +info <- content(res) +old_body <- unlist(lapply(info, "[", "body")) +if (!any(grepl(tbl_link, old_body))) { + json <- jsonlite::toJSON(list(body = msg), auto_unbox = TRUE) + httr::POST(url = commentz, header, body = json, encode = "json") +} else { + message("Link already posted") +}