Skip to content

Commit

Permalink
add untested GitHub repo creation
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Nov 7, 2018
1 parent 9da7052 commit 113f362
Show file tree
Hide file tree
Showing 13 changed files with 110 additions and 57 deletions.
6 changes: 6 additions & 0 deletions R/createAnalysisProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ createAnalysisProject <- function(name, folder = getwd(),
travis = TRUE,
packagedeps = "packrat",
git = TRUE,
github = gh::gh_whoami()$login,
private = FALSE,
protocol = "ssh",
readme = TRUE,
dirs = c("data", "analysis", "outputs")) {
packagedeps <- match.arg(packagedeps, okpackagedeps())
Expand All @@ -24,6 +27,9 @@ createAnalysisProject <- function(name, folder = getwd(),
travis = travis,
packagedeps = packagedeps,
git = git,
github = github,
private = private,
protocol = protocal,
readme = readme)
createdirs(dirs)
invisible(TRUE)
Expand Down
9 changes: 9 additions & 0 deletions R/createBasicProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
#' @param travis Configure Travis-CI
#' @param packagedeps Set a tool for package reproducibility
#' @param git Configure Git
#' @param github username or organization name to use for GitHub.
#' If NULL, no GitHub repo is created.
#' @param private whether to make the created GitHub repo private
#' @param protocol "ssh" or "https", protocol to use for GitHub
#' @param readme Include a README
#'
#' @export
Expand All @@ -20,6 +24,9 @@ createBasicProject <- function(name, folder = getwd(),
travis = TRUE,
packagedeps = "packrat",
git = TRUE,
github = gh::gh_whoami()$login,
private = FALSE,
protocol = "ssh",
readme = TRUE) {

packagedeps <- match.arg(packagedeps, okpackagedeps())
Expand Down Expand Up @@ -50,6 +57,8 @@ createBasicProject <- function(name, folder = getwd(),

if (git) usethis::use_git()
if (readme) usethis::use_readme_rmd(open = FALSE)
if (!is.null(github)) setup_repo(username = github,
private, protocol)

}
,
Expand Down
12 changes: 9 additions & 3 deletions R/createPackageProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#' @param folder Folder under which to create the project
#' @param bestPractices Run additional best practice commands
#' @param coverage What code coverage platform to use, "codecov" or "coveralls".
#' @param private Whether to create the GH repo as private
#' @param github username or organization name to use for GitHub.
#' If NULL, no GitHub repo is created.
#' @param private whether to make the created GitHub repo private
#' @param protocol "ssh" or "https", protocol to use for GitHub
#'
#' @export
#'
Expand All @@ -19,7 +22,9 @@
createPackageProject <- function(name, folder = getwd(),
bestPractices = TRUE,
coverage = "codecov",
private = TRUE) {
github = gh::gh_whoami()$login,
private = FALSE,
protocol = "ssh") {
if (is_available(name)) {
tryCatch({

Expand Down Expand Up @@ -61,7 +66,8 @@ createPackageProject <- function(name, folder = getwd(),
usethis::use_testthat()
usethis::use_vignette(name)
usethis::use_git()
#use_github(private = private)
if (!is.null(github)) setup_repo(username = github,
private, protocol)
}

}
Expand Down
6 changes: 6 additions & 0 deletions R/createTrainingProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ createTrainingProject <- function(name, folder = getwd(),
travis = TRUE,
packagedeps = "packrat",
git = TRUE,
github = gh::gh_whoami()$login,
private = FALSE,
protocol = "ssh",
readme = TRUE) {
packagedeps <- match.arg(packagedeps, okpackagedeps())

Expand All @@ -46,6 +49,9 @@ createTrainingProject <- function(name, folder = getwd(),
folder = folder,
travis = travis,
packagedeps = "none",
github = github,
private = private,
protocol = protocal,
git = git,
readme = readme)
createdirs(dirs)
Expand Down
67 changes: 27 additions & 40 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,51 +67,38 @@ is_available <- function(name) {
invisible(TRUE)
}

# from usethis since pushing doesn't work yet,
# removed pushing
use_github <- function (organisation = NULL, private = TRUE,
protocol = "ssh",
host = NULL)
{
auth_token <- usethis:::gh_token()
usethis:::check_gh_token(auth_token)

pkg <- usethis:::project_data()
repo_name <- pkg$Project %||% gsub("\n", " ", pkg$Package)
repo_desc <- pkg$Title %||% ""

is_org <- function(username){
info <- gh::gh("GET /users/:username", username = username)
info$type == "Organization"
}

usethis:::done("Creating GitHub repository")
if (is.null(organisation)) {
create <- gh::gh("POST /user/repos", name = repo_name,
description = repo_desc, private = private, .api_url = host,
.token = auth_token)
# very much inspired by usethis!
setup_repo <- function(username, private, protocol){
# create repo
if(is_org(username)){
endpoint <- "POST /orgs/:org/repos"
}else{
endpoint <- "POST /user/repos"
}
else {
create <- gh::gh("POST /orgs/:org/repos", org = organisation,
name = repo_name, description = repo_desc, private = private,
.api_url = host, .token = auth_token)
}
usethis:::done("Adding GitHub remote")
create <- gh::gh(endpoint,
org = username,
name = as.character(
fs::path_file(usethis::proj_get())),
description = as.character(
desc::desc(usethis::proj_get())$get("Title")),
private = tolower(as.character(private)))

r <- git2r::repository(usethis::proj_get())
protocol <- match.arg(protocol)
origin_url <- switch(protocol, https = create$clone_url,
ssh = create$ssh_url)
git2r::remote_add(r, "origin", origin_url)
if (usethis:::is_package()) {
usethis:::done("Adding GitHub links to DESCRIPTION")
use_github_links(auth_token = auth_token, host = host)
if (git_uncommitted()) {
git2r::add(r, "DESCRIPTION")
git2r::commit(r, "Add GitHub links to DESCRIPTION")
}
}
usethis:::done("Setting remote tracking branch")

git2r::branch_set_upstream(git2r::repository_head(r), "origin/master")
view_url(create$html_url)
invisible(NULL)
git2r::checkout(r, "master")
#git2r::branch_set_upstream(git2r::repository_head(r),
# "origin/master")
desc::desc_set("URL", create$html_url,
file = usethis::proj_get())
desc::desc_set("BugReports",
paste0(create$html_url, "/issues"),
file = usethis::proj_get())
}
# https://github.com/r-lib/usethis/blob/1e3c6a66e8b2d2790ee6d7e6d5651c52fb61abfc/R/utils.R#L100
"%||%" <- function(a, b) if (!is.null(a)) a else b

12 changes: 10 additions & 2 deletions man/createAnalysisProject.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 10 additions & 1 deletion man/createBasicProject.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions man/createPackageProject.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion man/createTrainingProject.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions tests/testthat/test-AAA-createBasicProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ project_name <- "basicProject"
test_that("createBasicProject() creates as expected when using defaults", {

createBasicProject(project_name, folder = tmp,
packagedeps = "packrat")
packagedeps = "packrat",
github = NULL)

expect_true(file.exists(file.path(tmp, project_name, paste0(project_name, ".Rproj"))))
expect_true(file.exists(file.path(tmp, project_name, "DESCRIPTION")))
Expand Down Expand Up @@ -46,7 +47,8 @@ usethis::proj_set(getwd())
test_that("createBasicProject() cleans if there was an error", {
m <- mockery::mock(stop())
with_mock(dir.create = m, {
expect_message(createBasicProject("blablabla"),
expect_message(createBasicProject("blablabla",
github = NULL),
"Oops")
})

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-createAnalysisProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ project_name <- "analysisProject"

test_that("createAnalysisProject() creates as expected when using defaults",{

createAnalysisProject(project_name, folder = tmp)
createAnalysisProject(project_name, folder = tmp,
github = NULL)

#expect_true(file.exists(file.path(tmp, project_name, paste0(project_name, ".Rproj"))))
expect_true(file.exists(file.path(tmp, project_name, "DESCRIPTION")))
Expand Down
6 changes: 4 additions & 2 deletions tests/testthat/test-createPackageProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ project_name <- "packageProject2"

test_that("createPackageProject() creates as expected when using defaults",{

createPackageProject(project_name, folder = tmp)
createPackageProject(project_name, folder = tmp,
github = NULL)
expect_true(file.exists(file.path(tmp, project_name, paste0(project_name, ".Rproj"))))
#expect_true(file.exists(file.path(tmp, project_name, "codecov.yml")))
expect_true(file.exists(file.path(tmp, project_name, "CODE_OF_CONDUCT.md")))
Expand All @@ -40,7 +41,8 @@ usethis::proj_set(getwd())
test_that("createPackageProject() cleans if there was an error", {
m <- mockery::mock(stop())
with_mock(dir.create = m, {
expect_message(createPackageProject("blablabla"),
expect_message(createPackageProject("reallynotapkgnameplease",
github = NULL),
"Oops")
})
})
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-createTrainingProject.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ project_name <- "trainingProject2"
test_that("createTrainingProject() creates as expected when using defaults",{

createTrainingProject(project_name, folder = tmp,
packagedeps = "none")
packagedeps = "none",
github = NULL)
expect_true(file.exists(file.path(tmp, project_name, paste0(project_name, ".Rproj"))))
expect_true(file.exists(file.path(tmp, project_name, "DESCRIPTION")))
expect_true(file.exists(file.path(tmp, project_name, "R")))
Expand All @@ -29,7 +30,8 @@ test_that("createTrainingProject() creates as expected when using bookdown and r

createTrainingProject(project_name, folder = tmp,
handoutEngine = "bookdown", slideEngine = "revealjs",
packagedeps = "none")
packagedeps = "none",
github = NULL)
expect_true(file.exists(file.path(tmp, project_name, paste0(project_name, ".Rproj"))))
expect_true(file.exists(file.path(tmp, project_name, "DESCRIPTION")))
expect_true(file.exists(file.path(tmp, project_name, "R")))
Expand All @@ -50,7 +52,8 @@ usethis::proj_set(getwd())
test_that("createTrainingProject() creates as expected when using tufte and xaringan",{
createTrainingProject(project_name, folder = tmp,
handoutEngine = "tufte", slideEngine = "xaringan",
packagedeps = "none")
packagedeps = "none",
github = NULL)

expect_true(file.exists(file.path(tmp, project_name, paste0(project_name, ".Rproj"))))
expect_true(file.exists(file.path(tmp, project_name, "DESCRIPTION")))
Expand Down

0 comments on commit 113f362

Please sign in to comment.