diff --git a/R/bookdown_zenodo.R b/R/bookdown_zenodo.R index 734824fd..d6b8738e 100644 --- a/R/bookdown_zenodo.R +++ b/R/bookdown_zenodo.R @@ -3,6 +3,9 @@ #' First clears all the existing files in the `output_dir` set in #' `_bookdown_.yml`. #' Then renders all required output formats and uploads them to Zenodo. +#' `bookdown_zenodo()` creates a draft record when you don't specify a community +#' in the yaml. +#' Otherwise it creates a review request for the first community. #' @param path The root folder of the report #' @param zip_format A vector with output formats that generate multiple files. #' The function will bundle all the files in a zip file for every format. diff --git a/R/citation_bookdown.R b/R/citation_bookdown.R index e41a9fde..8470a64a 100644 --- a/R/citation_bookdown.R +++ b/R/citation_bookdown.R @@ -94,9 +94,21 @@ citation_bookdown <- function(meta) { "No `publication_type` element found"[!has_name(yaml, "publication_type")] ) |> c(cit_meta$notes) -> cit_meta$notes + cit_meta$meta$community <- split_community(cit_meta$meta$community) return(cit_meta) } +#' @importFrom assertthat assert_that +split_community <- function(community) { + if (is.null(community)) { + return(NULL) + } + assert_that(is.character(community)) + strsplit(community, split = "\\s*;\\s*") |> + unlist() |> + unique() +} + #' @importFrom assertthat has_name yaml_author <- function(yaml, org) { author <- vapply( diff --git a/R/upload_zenodo.R b/R/upload_zenodo.R index 9d77ef71..ce1e61ff 100644 --- a/R/upload_zenodo.R +++ b/R/upload_zenodo.R @@ -40,7 +40,9 @@ upload_zenodo <- function(path, token, sandbox = TRUE, logger = NULL) { ) zen_rec$setPublisher(cit_meta$publisher) - zen_rec <- zen_upload(zenodo, zen_rec, path) + zen_rec <- zen_upload( + zenodo, zen_rec, path, community = cit_meta$communities + ) return(invisible(zen_rec)) } @@ -63,11 +65,11 @@ zen_contributor <- function(zen_rec, contributors) { return(zen_rec) } -#' @importFrom assertthat assert_that has_name +#' @importFrom assertthat assert_that has_name is.string noNA #' @importFrom cli cli_alert_info cli_alert_warning #' @importFrom fs dir_ls #' @importFrom utils browseURL -zen_upload <- function(zenodo, zen_rec, path) { +zen_upload <- function(zenodo, zen_rec, path, community = NULL) { zen_rec <- zenodo$depositRecord(zen_rec, publish = FALSE) assert_that( has_name(zen_rec, "status"), @@ -88,15 +90,33 @@ zen_upload <- function(zenodo, zen_rec, path) { for (filename in to_upload) { zenodo$uploadFile(filename, record = zen_rec) } - c( - "Draft uploaded to Zenodo.", - "Please visit {zen_rec$links$self_html} to publish." - ) |> - paste(collapse = " ") |> - cli_alert_info() - cli_alert_warning( - "Remember to add the publication to the relevant communities." + + if (is.null(community)) { + c( + "Draft uploaded to Zenodo.", + "Please visit {zen_rec$links$self_html} to publish." + ) |> + paste(collapse = " ") |> + cli_alert_info() + if (interactive()) { + browseURL(zen_rec$links$self_html) + } + return(zen_rec) + } + + zenodo$createReviewRequest( + record = zen_rec, community = unlist(head(community, 1)) ) + zenodo$submitRecordForReview( + recordId = zen_rec$id, + message = paste( + "Automated review request created by the checklist package.", + "More information at https://inbo.github.io/checklist/." + ) + ) + if (length(community) > 1) { + cli_alert_warning("Remember to add the additional communities.") + } if (interactive()) { browseURL(zen_rec$links$self_html) } diff --git a/inst/bookdown/index.Rmd b/inst/bookdown/index.Rmd index c0f5a2b4..f79c6270 100644 --- a/inst/bookdown/index.Rmd +++ b/inst/bookdown/index.Rmd @@ -17,7 +17,7 @@ reviewer: affiliation: Wesleyan University lang: nl keywords: checklist; unit test -community: inbo +community: checklist; inbo publication_type: publication-report publisher: Research Institute for Nature and Forest (INBO) funder: Research Institute for Nature and Forest (INBO) diff --git a/man/bookdown_zenodo.Rd b/man/bookdown_zenodo.Rd index 897e0c09..768f4248 100644 --- a/man/bookdown_zenodo.Rd +++ b/man/bookdown_zenodo.Rd @@ -40,6 +40,9 @@ Use \code{NULL} to hide the logs. First clears all the existing files in the \code{output_dir} set in \verb{_bookdown_.yml}. Then renders all required output formats and uploads them to Zenodo. +\code{bookdown_zenodo()} creates a draft record when you don't specify a community +in the yaml. +Otherwise it creates a review request for the first community. } \seealso{ Other utils: diff --git a/tests/testthat/test_e_bookdown_zenodo.R b/tests/testthat/test_e_bookdown_zenodo.R index 05f99ff5..b342f208 100644 --- a/tests/testthat/test_e_bookdown_zenodo.R +++ b/tests/testthat/test_e_bookdown_zenodo.R @@ -69,6 +69,38 @@ test_that("bookdown_zenodo() works", { ) sink() manager <- zen4R::ZenodoManager$new(sandbox = TRUE, token = sandbox_token) + zen_com <- manager$getCommunityById("checklist") + sprintf( + "status:submitted AND receiver.community:%s AND topic.record:%s", + zen_com$id, x$id + ) |> + manager$getRequests() -> reqs + expect_true( + manager$cancelRequest(reqs[[1]]$id), + label = paste("Failed to delete review request", reqs[[1]]$id) + ) + expect_true( + manager$deleteRecord(x$id), + label = paste("Remove Zenodo sandbox record", x$links$self_html) + ) + + # test without community + c("---", index[!grepl("^community:", index)]) |> + writeLines(path(root, "index.Rmd")) + organisation$new( + github = NA_character_, community = NA_character_, email = NA_character_, + funder = NA_character_, rightsholder = NA_character_ + ) |> + write_organisation(root) + zenodo_out3 <- tempfile(fileext = ".txt") + defer(file_delete(zenodo_out3)) + sink(zenodo_out3) + suppressMessages( + x <- bookdown_zenodo( + root, logger = NULL, sandbox = TRUE, token = sandbox_token + ) + ) + sink() expect_true( manager$deleteRecord(x$id), label = paste("Remove Zenodo sandbox record", x$links$self_html) @@ -76,18 +108,17 @@ test_that("bookdown_zenodo() works", { path(root, "index.Rmd") |> readLines() -> index - head(index, 4) |> - c(" - Josiah Carberry", tail(index, -11)) |> + head(index, 3) |> + c(" - Josiah Carberry", tail(index, -10)) |> writeLines(path(root, "index.Rmd")) expect_warning( - x <- citation_meta$new(root), - "Errors found parsing citation meta data" + x <- citation_meta$new(root), "Errors found parsing citation meta data" ) expect_identical( x$get_errors, c("no author with `corresponding: true`", "person must be a list") ) - head(index, 4) |> + head(index, 3) |> c(" - name: Josiah Carberry", tail(index, -10)) |> writeLines(path(root, "index.Rmd")) expect_warning(