Skip to content

Commit d9ecaf4

Browse files
committed
Merge pull request #208 from ropensci/bot
First stab at automated comments
2 parents fafbff9 + f3b40a7 commit d9ecaf4

File tree

4 files changed

+105
-53
lines changed

4 files changed

+105
-53
lines changed

.push_test_table.sh

-51
This file was deleted.

.travis.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ r_packages:
1212
- httr
1313

1414
before_script:
15-
- chmod 755 ./.push_test_table.sh
15+
- chmod 755 inst/testscripts/.push_test_table.sh
1616

1717
after_success:
18-
- ./.push_test_table.sh
18+
- inst/testscripts/.push_test_table.sh
1919

2020
notifications:
2121
slack:

inst/testscripts/.push_test_table.sh

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# exit on error
4+
set -e
5+
6+
# -----------------------------------------------------------------------
7+
# Travis does two types of builds:
8+
#
9+
# (1) A so-called "push". This essentially does a checkout on the most
10+
# recent commit of the pull request, but *doesn't* merge with master.
11+
# In this case, $TRAVIS_PULL_REQUEST = "false"
12+
# (2) A so-called "pr" (pull request). This *does* merge with master.
13+
# In this case, $TRAVIS_PULL_REQUEST contains the pull request number.
14+
# -----------------------------------------------------------------------
15+
16+
# We need the pull request number to talk to the GitHub API, make comments, etc.
17+
[ "${TRAVIS_PULL_REQUEST}" = "false" ] && exit 0
18+
19+
git config --global user.name "cpsievert"
20+
git config --global user.email "[email protected]"
21+
22+
cd ..
23+
git clone https://github.com/ropensci/plotly-test-table.git
24+
cd plotly-test-table
25+
git checkout gh-pages
26+
27+
# Read more about Travis environment variables --
28+
# http://docs.travis-ci.com/user/ci-environment/
29+
Rscript ../plotly/inst/testscripts/comment.R $TRAVIS_PULL_REQUEST $TRAVIS_BUILD_ID $TRAVIS_COMMIT $GH_TOKEN

inst/testscripts/comment.R

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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

Comments
 (0)