Skip to content

Commit

Permalink
Merge pull request #65 from tanho63/tandev
Browse files Browse the repository at this point in the history
Refactoring of pb_list() and other changes
  • Loading branch information
cboettig authored Apr 26, 2022
2 parents 37abeca + 13fd478 commit 42eba8c
Show file tree
Hide file tree
Showing 37 changed files with 522 additions and 552 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
^paper\.bib$
^codemeta\.json$
^docs$
^dev$
^.*\.gz$
mtcars.tsv.gz$
^\.github$
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ License: GPL-3
Encoding: UTF-8
ByteCompile: true
Imports:
cli,
glue,
gh,
httr,
jsonlite,
fs,
crayon,
clisymbols,
lubridate,
memoise
Suggests:
Expand Down
20 changes: 1 addition & 19 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,5 @@ export(pb_download)
export(pb_download_url)
export(pb_list)
export(pb_new_release)
export(pb_releases)
export(pb_upload)
importFrom(clisymbols,symbol)
importFrom(crayon,blue)
importFrom(crayon,green)
importFrom(fs,dir_create)
importFrom(gh,gh)
importFrom(gh,gh_token)
importFrom(httr,GET)
importFrom(httr,POST)
importFrom(httr,add_headers)
importFrom(httr,content)
importFrom(httr,progress)
importFrom(httr,stop_for_status)
importFrom(httr,upload_file)
importFrom(httr,write_disk)
importFrom(jsonlite,toJSON)
importFrom(lubridate,as_datetime)
importFrom(memoise,forget)
importFrom(memoise,memoise)
importFrom(memoise,timeout)
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
* update intro vignette to remove mention of path name handling and instead provide examples of how path names are handled.
* update intro vignette instructions for git authentication
* `pb_new_release()` now reports HTTP errors when attempting to create a new release and returns the contents of the error if it fails.
* `pb_releases()` created - it returns a list of releases available in the repository.
* Internal function `pb_info()` refactored to search for the specified tag(s) which should improve performance. Should handle multiple tags gracefully.
* Internal function `pb_info()` (and therefore `pb_list()`, `pb_download()`, `pb_download_url()`) no longer ask about creating new releases if the release is not found.
* `pb_upload()` is now the only function that offers (interactively) to create a new release if release is not found. If noninteractive, user must run `pb_new_release()` manually prior to uploading.
* CLI messaging now consistently uses `{cli}` package and no longer uses clisymbols or crayon - this is to align with the imports from the `{gh}` package.
* Documentation updated.

# piggyback 0.1.1

Expand Down
83 changes: 0 additions & 83 deletions R/gh.R

This file was deleted.

30 changes: 21 additions & 9 deletions R/pb_delete.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#' ## Upload
#' pb_upload("mtcars.tsv.gz",
#' repo = "cboettig/piggyback-tests",
#' overwrite = TRUE)
#' overwrite = TRUE)
#' pb_delete("mtcars.tsv.gz",
#' repo = "cboettig/piggyback-tests",
#' tag = "v0.0.1")
Expand All @@ -22,25 +22,37 @@
pb_delete <- function(file = NULL,
repo = guess_repo(),
tag = "latest",
.token = get_token()) {
.token = gh::gh_token()) {
df <- pb_info(repo, tag, .token)

if (is.null(file)) {
ids <- df$id
} else {
if (is.null(file)) ids <- df$id

if(!is.null(file) && any(!file %in% df$file_name)){
missing <- file[!file %in% df$file_name]
file <- file[file != missing]
cli::cli_warn("{.val {missing}} not found in {.val {tag}} release of {.val {repo}}")
}

if(!is.null(file)){
ids <- df[df$file_name %in% file, "id"]
}

if (length(ids) < 1) {
message(paste(file, "not found on GitHub"))
return(NULL)
cli::cli_warn("No file deletions performed.")
return(invisible(NULL))
}

lapply(ids, function(id) {
## If we find matching id, Delete file from release.
gh::gh("DELETE /repos/:owner/:repo/releases/assets/:id",
owner = df$owner[[1]], repo = df$repo[[1]], id = id, .token = .token
owner = df$owner[[1]],
repo = df$repo[[1]],
id = id,
.token = .token
)
})
invisible(TRUE)

cli::cli_alert_info("Deleted {.val {file}} from {.val {tag}} release on {.val {repo}}")

return(invisible(TRUE))
}
47 changes: 19 additions & 28 deletions R/pb_download.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
#' @param ignore a list of files to ignore (if downloading "all" because
#' `file=NULL`).
#' @inheritParams pb_upload
#' @importFrom httr GET add_headers write_disk
#' @importFrom gh gh gh_token
#' @importFrom fs dir_create
#'
#' @export
#' @examples \dontrun{
#' ## Download a specific file.
Expand All @@ -35,12 +33,11 @@ pb_download <- function(file = NULL,
ignore = "manifest.json",
use_timestamps = TRUE,
show_progress = TRUE,
.token = get_token()) {
.token = gh::gh_token()) {

progress <- httr::progress("down")
if (!show_progress) {
progress <- NULL
}

if (!show_progress) progress <- NULL

df <- pb_info(repo, tag, .token)

Expand All @@ -50,12 +47,7 @@ pb_download <- function(file = NULL,
if (!is.null(file)) {
i <- which(df$file_name %in% file)
if (length(i) < 1) {
warning(paste(
"file(s)",
paste(crayon::blue(file), collapse = " "),
"not found in repo",
crayon::blue(repo)
))
cli::cli_warn("file(s) {.file {file}} not found in repo {.val {repo}}")
}

df <- df[i, ]
Expand All @@ -69,11 +61,10 @@ pb_download <- function(file = NULL,


## if dest paths are not provided, we will write all files to dest dir
# User is responsible for making sure dest dir exists!
if (length(dest) == 1) {
i <- which(df$file_name %in% file)
dest <- file.path(dest, df$file_name[i])
# User is responsible for making sure dest dir exists!
# fs::dir_create(fs::path_dir(dest))
}
# dest should now be of length df
df$dest <- dest
Expand All @@ -86,43 +77,42 @@ pb_download <- function(file = NULL,
df <- df[update, ]

if (dim(df)[[1]] < 1) {
message(paste("All files up-to-date already\n"))
cli::cli_alert_info("All local files already up-to-date!")
}
}

resp <- lapply(seq_along(df$id), function(i)
gh_download_asset(df$owner[[1]],
df$repo[[1]],
id = df$id[i],
destfile = dest[i],
destfile = df$dest[i],
overwrite = overwrite,
progress = progress
))
invisible(resp)
}




## gh() fails on this, so we do with httr. See https://github.com/r-lib/gh/issues/57
## Consider option to supress progress bar?
## Consider option to suppress progress bar?
gh_download_asset <- function(owner,
repo,
id,
destfile,
overwrite = TRUE,
.token = get_token(),
.token = gh::gh_token(),
progress = httr::progress("down")) {
if (fs::file_exists(destfile) && !overwrite) {
warning(paste(
destfile, "already exists, skipping download.",
"Set overwrite = TRUE to overwrite files."
))
cli::cli_warn(
c("!"="{.val {destfile}} already exists, skipping download.",
"Set {.code overwrite = TRUE} to overwrite files.")
)
return(NULL)
}

if (!is.null(progress)) {
message(paste("downloading", basename(destfile), "..."))
cli::cli_alert_info(
"Downloading {.val {basename(destfile)}}..."
)
}

resp <- httr::GET(
Expand All @@ -149,9 +139,10 @@ gh_download_asset <- function(owner,
}

# handle error cases? resp not found
if(getOption("verbose")) httr::warn_for_status(resp)
if(getOption("piggyback.verbose", default = TRUE)) httr::warn_for_status(resp)

invisible(resp)

# gh::gh(paste0(
# "https://api.github.com/repos/", owner, "/",
# repo, "/", "releases/assets/", id),
Expand Down
23 changes: 15 additions & 8 deletions R/pb_download_url.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#' Get the download url of a given file
#'
#' Returns the URL download for a public file. This can be useful when writing
Expand All @@ -17,13 +16,21 @@
pb_download_url <- function(file = NULL,
repo = guess_repo(),
tag = "latest",
.token = get_token()) {
.token = gh::gh_token()) {
df <- pb_info(repo, tag, .token)
if (is.null(file)) {
return(df$browser_download_url)
} else if (file %in% df$file_name) {
return(df[file == df$file_name, "browser_download_url"])
} else {
stop(paste("file", file, "not found in release", tag, "for repo", repo))

if(is.null(file)) return(df$browser_download_url)

if(any(!file %in% df$file_name)) {

missing <- file[!file %in% df$file_name]

cli::cli_warn("file {.val {missing}} not found in release {.val {tag}} for repo {.val {repo}}")

file <- file[file %in% df$file_name]
}

if(length(file) == 0) return(cli::cli_abort("No download URLs to return."))

return(df[df$file_name %in% file,"browser_download_url"])
}
Loading

0 comments on commit 42eba8c

Please sign in to comment.