Skip to content

Commit

Permalink
Enable "internal" repositories in use_github() (#1505)
Browse files Browse the repository at this point in the history
* Enable "internal" repositories in `use_github()`

Closes #1458

* Send a special header

* `.accept` is actually designed for this
  • Loading branch information
jennybc authored Oct 7, 2021
1 parent 7c427e6 commit 23fefcd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
31 changes: 28 additions & 3 deletions R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
#' such that you have permission to create repositories in this
#' `organisation`.
#' @param private If `TRUE`, creates a private repository.
#' @param visibility Only relevant for organisation-owned repos associated with
#' certain GitHub Enterprise products. The special "internal" `visibility`
#' grants read permission to all organisation members, i.e. it's intermediate
#' between "private" and "public", within GHE. When specified, `visibility`
#' takes precedence over `private = TRUE/FALSE`.
#' @inheritParams git_protocol
#' @param host GitHub host to target, passed to the `.api_url` argument of
#' [gh::gh()]. If unspecified, gh defaults to "https://api.github.com",
Expand Down Expand Up @@ -54,6 +59,7 @@
#' }
use_github <- function(organisation = NULL,
private = FALSE,
visibility = c("public", "private", "internal"),
protocol = git_protocol(),
host = NULL,
auth_token = deprecated(),
Expand All @@ -65,6 +71,8 @@ use_github <- function(organisation = NULL,
deprecate_warn_credentials("use_github")
}

visibility_specified <- !missing(visibility)
visibility <- match.arg(visibility)
check_protocol(protocol)
check_uses_git()
default_branch <- git_default_branch()
Expand All @@ -79,6 +87,20 @@ use_github <- function(organisation = NULL,
GitHub repo")
check_no_origin()

if (is.null(organisation)) {
if (visibility_specified) {
ui_stop("
The {ui_code('visibility')} setting is only relevant for
organisation-owned repos, within the context of certain \\
GitHub Enterprise products.")
}
visibility <- if (private) "private" else "public"
}

if (!is.null(organisation) && !visibility_specified) {
visibility <- if (private) "private" else "public"
}

whoami <- suppressMessages(gh::gh_whoami(.api_url = host))
if (is.null(whoami)) {
ui_stop("
Expand All @@ -100,8 +122,8 @@ use_github <- function(organisation = NULL,
repo_desc <- gsub("\n", " ", repo_desc)
repo_spec <- glue("{owner}/{repo_name}")

private_string <- if (private) "private " else ""
ui_done("Creating {private_string}GitHub repository {ui_value(repo_spec)}")
visibility_string <- if (visibility == "public") "" else glue("{visibility} ")
ui_done("Creating {visibility_string}GitHub repository {ui_value(repo_spec)}")
if (is.null(organisation)) {
create <- gh::gh(
"POST /user/repos",
Expand All @@ -116,7 +138,10 @@ use_github <- function(organisation = NULL,
org = organisation,
name = repo_name,
description = repo_desc,
private = private,
visibility = visibility,
# this is necessary to set `visibility` in GHE 2.22 (but not in 3.2)
# hopefully it's harmless when not needed
.accept = "application/vnd.github.nebula-preview+json",
.api_url = host
)
}
Expand Down
7 changes: 7 additions & 0 deletions man/use_github.Rd

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

0 comments on commit 23fefcd

Please sign in to comment.