Skip to content

Commit

Permalink
Add private flag to create_github_repo()
Browse files Browse the repository at this point in the history
  • Loading branch information
jabenninghoff committed May 8, 2024
1 parent d7c0c9c commit 478fb1b
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# rdev 1.10.7

* Added support for creating private repositories to `create_github_repo()`

# rdev 1.10.6

* Updated for R 4.4.0
Expand Down
33 changes: 19 additions & 14 deletions R/setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ fix_gitignore <- function(path = ".") {
#' 1. Creates a new GitHub repository using [gh::gh()] with license template from [get_license()]
#' 1. Activates Dependabot alerts per `getOption("rdev.dependabot", default = TRUE)`
#' 1. Activates Dependabot security updates per `getOption("rdev.dependabot", default = TRUE)`
#' 1. Adds branch protection to the default branch
#' 1. Adds branch protection to the default branch (if `private` is `FALSE`)
#' 1. Clones the repository locally with [usethis::create_from_github()]
#' 1. Creates a basic package using [usethis::create_package()]
#' 1. If running interactively on macOS, the repository will automatically be opened in RStudio,
Expand All @@ -206,10 +206,11 @@ fix_gitignore <- function(path = ".") {
#'
#' @return return value from [gh::gh()] creating the repository, invisibly
#' @export
create_github_repo <- function(repo_name, repo_desc = "", org = NULL,
create_github_repo <- function(repo_name, repo_desc = "", private = FALSE, org = NULL,
host = getOption("rdev.host")) {
checkmate::assert_string(repo_name, min.chars = 1)
checkmate::assert_string(repo_desc)
checkmate::assert_flag(private)
checkmate::assert_string(org, min.chars = 1, null.ok = TRUE)
checkmate::assert_string(host, min.chars = 1, null.ok = TRUE)

Expand All @@ -229,6 +230,7 @@ create_github_repo <- function(repo_name, repo_desc = "", org = NULL,
"POST /user/repos",
name = repo_name,
description = repo_desc,
private = private,
gitignore_template = "R",
license_template = license_template,
.api_url = host
Expand All @@ -239,6 +241,7 @@ create_github_repo <- function(repo_name, repo_desc = "", org = NULL,
org = org,
name = repo_name,
description = repo_desc,
private = private,
gitignore_template = "R",
license_template = license_template,
.api_url = host
Expand Down Expand Up @@ -283,18 +286,20 @@ create_github_repo <- function(repo_name, repo_desc = "", org = NULL,
} else {
required_pull_request_reviews <- NA
}
gh::gh(
"PUT /repos/{owner}/{repo}/branches/{branch}/protection",
owner = create$owner$login,
repo = create$name,
branch = create$default_branch,
required_status_checks = required_status_checks,
enforce_admins = NA,
required_pull_request_reviews = required_pull_request_reviews,
restrictions = NA,
required_linear_history = TRUE,
.api_url = host
)
if (!private) {
gh::gh(
"PUT /repos/{owner}/{repo}/branches/{branch}/protection",
owner = create$owner$login,
repo = create$name,
branch = create$default_branch,
required_status_checks = required_status_checks,
enforce_admins = NA,
required_pull_request_reviews = required_pull_request_reviews,
restrictions = NA,
required_linear_history = TRUE,
.api_url = host
)
}

# warning: duplicates .Rproj.user in .gitignore
fs_path <- usethis::create_from_github(
Expand Down
5 changes: 4 additions & 1 deletion man/create_github_repo.Rd

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

23 changes: 22 additions & 1 deletion tests/testthat/test-setup.R
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ test_that("create_github_repo validates arguments", {
expect_error(create_github_repo(repo_name = NA_character_), "'repo_name'")
expect_error(create_github_repo(repo_name = ""), "'repo_name'")
expect_error(create_github_repo("test", repo_desc = NA_character_), "'repo_desc'")
expect_error(create_github_repo("test", private = NA), "'private'")
expect_error(create_github_repo("test", org = NA_character_), "'org'")
expect_error(create_github_repo("test", org = ""), "'org'")
expect_error(create_github_repo("test", host = NA_character_), "'host'")
Expand All @@ -132,7 +133,7 @@ test_that("create_github_repo errors when proposed repo directory exists locally
)
})

test_that("create_github_repo options work", {
test_that("create_github_repo options and flags work", {
fs_path <- "/Users/test/Desktop/rdtest9" # nolint: absolute_path_linter.
create <- list(html_url = "https://github.com/test/rdtest9")
with_dependabot <- paste0(
Expand All @@ -155,6 +156,17 @@ test_that("create_github_repo options work", {
"update the Title and Description fields in `DESCRIPTION` without committing,\\n",
"and run either setup_ananlysis\\(\\) or setup_rdev\\(\\) to finish configuration\\.$"
)
with_private <- paste0(
"^POST /user/repos\\n",
"PUT /repos/\\{owner\\}/\\{repo\\}/vulnerability-alerts\\n",
"PUT /repos/\\{owner\\}/\\{repo\\}/automated-security-fixes\\n",
"\\nRepository created at: ", create$html_url, "\\n",
"Open the repository by executing: \\$ github ", fs_path, "\\n",
"Apply rdev conventions within the new project by running init\\(\\) without committing,\\n",
"update the Title and Description fields in `DESCRIPTION` without committing,\\n",
"and run either setup_ananlysis\\(\\) or setup_rdev\\(\\) to finish configuration\\.$"
)
without_private <- with_dependabot
gh_gh <- function(command, ...) {
writeLines(command)
create
Expand Down Expand Up @@ -187,6 +199,15 @@ test_that("create_github_repo options work", {
),
without_dependabot
)

expect_output(
create_github_repo("rdtest9", "rdev test analysis package 9", private = FALSE),
without_private
)
expect_output(
create_github_repo("rdtest9", "rdev test analysis package 9", private = TRUE),
with_private
)
})

test_that("create_github_repo generates expected output", {
Expand Down

0 comments on commit 478fb1b

Please sign in to comment.