Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Imports:
glue (>= 1.3.0),
jsonlite,
lifecycle (>= 1.0.0),
mime,
purrr,
rappdirs,
rlang (>= 1.1.0),
Expand Down
5 changes: 3 additions & 2 deletions R/release.R
Original file line number Diff line number Diff line change
Expand Up @@ -393,14 +393,15 @@ get_release_news <- function(
news <- if (file_exists(news_path)) read_utf8(news_path) else NULL
} else {
news <- tryCatch(
read_github_file(
get_github_file(
tr$repo_spec,
path = "NEWS.md",
ref = SHA,
host = tr$api_url
),
github_error = NULL
)
) |>
read_utf8()
}

if (is.null(news)) {
Expand Down
44 changes: 33 additions & 11 deletions R/use_github_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#' @param ref The name of a branch, tag, or commit. By default, the file at
#' `path` will be copied from its current state in the repo's default branch.
#' This is extracted from `repo_spec` when user provides a URL.
#' @param open Open the newly created file for editing, if it is a text file? Happens in RStudio, if
#' applicable, or via [utils::file.edit()] otherwise. Binary files will not be opened.
#' @inheritParams use_template
#' @inheritParams use_github
#' @inheritParams write_over
Expand Down Expand Up @@ -77,37 +79,57 @@ use_github_file <- function(
"v" = "Saving {.val {github_string}} to {.path {pth(save_as)}}."
))

lines <- read_github_file(
tf <- get_github_file(
repo_spec = repo_spec,
path = path,
ref = ref,
host = host
)
new <- write_over(
proj_path(save_as),
lines,
quiet = TRUE,
overwrite = overwrite
)

# If it's a text file, we read and write to make sure it is utf-8,
# otherwise, copy to its final destination
is_text <- grepl("^text/", mime::guess_type(tf))

if (is_text) {
new <- write_over(
proj_path(save_as),
read_utf8(tf),
quiet = TRUE,
overwrite = overwrite
)
} else {
file_copy(tf, proj_path(save_as), overwrite = overwrite)
new <- file_exists(proj_path(save_as))
}

if (ignore) {
use_build_ignore(save_as)
}

if (open && new) {
if (is_text && open && new) {
edit_file(proj_path(save_as))
}

invisible(new)
}

read_github_file <- function(repo_spec, path, ref = NULL, host = NULL) {
get_github_file <- function(
repo_spec,
path,
ref = NULL,
host = NULL,
envir = parent.frame()
) {
# https://docs.github.com/en/rest/reference/repos#contents
# https://docs.github.com/en/rest/reference/repos#if-the-content-is-a-symlink
# If the requested {path} points to a symlink, and the symlink's target is a
# normal file in the repository, then the API responds with the content of the
# file....
tf <- withr::local_tempfile()
tf <- withr::local_tempfile(
fileext = paste0(".", path_ext(path)),
.local_envir = envir
)

gh::gh(
"/repos/{repo_spec}/contents/{path}",
repo_spec = repo_spec,
Expand All @@ -117,7 +139,7 @@ read_github_file <- function(repo_spec, path, ref = NULL, host = NULL) {
.destfile = tf,
.accept = "application/vnd.github.v3.raw"
)
read_utf8(tf)
tf
}

# https://github.com/OWNER/REPO/blob/REF/path/to/some/file
Expand Down
8 changes: 7 additions & 1 deletion R/use_standalone.R
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ use_standalone <- function(repo_spec, file = NULL, ref = NULL, host = NULL) {
src_path <- path("R", file)
dest_path <- path("R", as_standalone_dest_file(file))

lines <- read_github_file(repo_spec, path = src_path, ref = ref, host = host)
lines <- get_github_file(
repo_spec,
path = src_path,
ref = ref,
host = host
) |>
read_utf8()
lines <- c(standalone_header(repo_spec, src_path, ref, host), lines)
write_over(proj_path(dest_path), lines, overwrite = TRUE)

Expand Down
4 changes: 2 additions & 2 deletions man/use_github_file.Rd

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

10 changes: 10 additions & 0 deletions tests/testthat/test-use_github_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,13 @@ test_that("parse_file_url() errors when it should", {
"https://gitlab.com/OWNER/REPO/path/to/file"
))
})

test_that("use_github_file works with non-text files", {
create_local_project()
use_github_file(
"https://github.com/r-lib/usethis/blob/main/man/figures/logo.png",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is probably stable, but I can put a small png in tests/testthat/ if that would be better?

save_as = "logo.png"
)

expect_proj_file("logo.png")
})
Loading