|
| 1 | +# first argument is the pull request number (TRAVIS_PULL_REQUEST) |
| 2 | +# second is travis build ID (TRAVIS_BUILD_ID) |
| 3 | +# third is the commit SHA1 currently being tested (TRAVIS_COMMIT) |
| 4 | +# fourth is the github authentication token |
| 5 | +a <- commandArgs(TRUE) |
| 6 | +# gistr is a good reference for talking to the github API via httr |
| 7 | +# https://github.com/ropensci/gistr/blob/master/R/zzz.R |
| 8 | +library("httr") |
| 9 | +base <- 'https://api.github.com/repos/ropensci/plotly/' |
| 10 | +pr <- sprintf(paste0(base, 'pulls/%s'), a[1]) |
| 11 | +header <- add_headers(`User-Agent` = "plotly", |
| 12 | + `Accept` = 'application/vnd.github.v3+json', |
| 13 | + `Authorization` = paste0("token ", a[4])) |
| 14 | +# Must be successful since we grab the branch name for this pull request |
| 15 | +# and SHA1 info from the request content |
| 16 | +res <- GET(url = pr, header) |
| 17 | +stop_for_status(res) |
| 18 | +info <- content(res) |
| 19 | +# find the branch name for this pull request |
| 20 | +# http://stackoverflow.com/questions/15096331/github-api-how-to-find-the-branches-of-a-pull-request |
| 21 | +branch <- strsplit(info$head$label, ":")[[1]][2] |
| 22 | + |
| 23 | +# plotly-test-table build script assumes we've checkout the dev branch. |
| 24 | +# Note that travis does something like this for "pr" build: |
| 25 | +#$ git fetch origin +refs/pull/number/merge: |
| 26 | +#$ git checkout -qf FETCH_HEAD |
| 27 | +# this leaves HEAD in a detached state, but we should be able to do: |
| 28 | +# git checkout -b new_branch_name |
| 29 | +setwd("../plotly") |
| 30 | +if (system(paste("git checkout -b", branch)) != 0L) |
| 31 | + stop(paste("Failed to 'git checkout -b'", branch, "branch")) |
| 32 | +devtools::install() |
| 33 | +setwd("../plotly-test-table") |
| 34 | +cat("user,SHA1,label", file = "code_commits.csv") |
| 35 | +row1 <- paste0("\nropensci,", info$base$sha, ",master") |
| 36 | +cat(row1, file = "code_commits.csv", append = TRUE) |
| 37 | +row2 <- paste0("\nropensci,", a[3], ",", branch) |
| 38 | +cat(row2, file = "code_commits.csv", append = TRUE) |
| 39 | + |
| 40 | +# copy over file (created during Rscript) |
| 41 | +# with sha/branch info for building test table |
| 42 | +system("touch table.R") |
| 43 | +if (system("make") != 0L) stop("Failed to 'make' test table") |
| 44 | + |
| 45 | +# add, commit, push to gh-pages branch of plotly-test-table |
| 46 | +system("git add index.html") |
| 47 | +system("git add tables/*/*.html") |
| 48 | +system("git add data/*/*.png") |
| 49 | +system("git add data/*/*.log") |
| 50 | +build_link <- paste0('https://travis-ci.org/ropensci/plotly/builds/', a[2]) |
| 51 | +commit_msg <- paste0('"Pushed from ', build_link, '"') |
| 52 | +system(paste('git commit -m', commit_msg)) |
| 53 | +# This post explains how this works -- http://rmflight.github.io/posts/2014/11/travis_ci_gh_pages.html |
| 54 | +repo <- sprintf( "https://%[email protected]/ropensci/plotly-test-table.git", a[ 4]) |
| 55 | +system(paste("git pull -q", repo, "gh-pages")) |
| 56 | +system(paste("git push -q", repo, "gh-pages")) |
| 57 | + |
| 58 | +# post comment if a link to this SHA doesn't exist |
| 59 | +# (needed since Travis randomly re-builds stuff) |
| 60 | +tbl_link <- sprintf("http://ropensci.github.io/plotly-test-table/tables/%s/index.html", a[3]) |
| 61 | +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", |
| 62 | + info$head$sha, info$base$sha, a[3], info$base$sha, a[3], tbl_link) |
| 63 | +msg <- paste("> The message below was automatically generated after build", build_link, "\n\n", msg) |
| 64 | +commentz <- sprintf(paste0(base, 'issues/%s/comments'), a[1]) |
| 65 | +res <- GET(commentz, header) |
| 66 | +warn_for_status(res) |
| 67 | +info <- content(res) |
| 68 | +old_body <- unlist(lapply(info, "[", "body")) |
| 69 | +if (!any(grepl(tbl_link, old_body))) { |
| 70 | + json <- jsonlite::toJSON(list(body = msg), auto_unbox = TRUE) |
| 71 | + httr::POST(url = commentz, header, body = json, encode = "json") |
| 72 | +} else { |
| 73 | + message("Link already posted") |
| 74 | +} |
0 commit comments