Skip to content

Commit

Permalink
bookdown_zenodo() creates a review request when the community is defined
Browse files Browse the repository at this point in the history
thanks to @eblondel for the feedback via eblondel/zen4R#158
  • Loading branch information
ThierryO committed May 28, 2024
1 parent 0df293b commit e78f8e6
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 17 deletions.
3 changes: 3 additions & 0 deletions R/bookdown_zenodo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
12 changes: 12 additions & 0 deletions R/citation_bookdown.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
42 changes: 31 additions & 11 deletions R/upload_zenodo.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand All @@ -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"),
Expand All @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion inst/bookdown/index.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions man/bookdown_zenodo.Rd

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

41 changes: 36 additions & 5 deletions tests/testthat/test_e_bookdown_zenodo.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,56 @@ 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)
)

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(
Expand Down

0 comments on commit e78f8e6

Please sign in to comment.