Skip to content

Commit 933ea66

Browse files
committed
#387 full zenodo workflows
1 parent 9cc1b6f commit 933ea66

File tree

3 files changed

+144
-74
lines changed

3 files changed

+144
-74
lines changed

inst/actions/zen4R_deposit_record.R

Lines changed: 139 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -33,53 +33,60 @@ function(action, entity, config){
3333
update_files <- action$getOption("update_files")
3434
communities <- action$getOption("communities")
3535

36-
#zenodo object
37-
zenodo_metadata <- NULL
38-
39-
#how to deal with existing records / new versions
40-
#we try first to use DOI if existing, assuming it's a concept DOI, if not a simple DOI
41-
#if nothing is found, we use the default get depositions by identifier, used if no DOI is specified
42-
#this approach is possible because the Zenodo record has a related identifier as URN, specified when
43-
#creating the record. For existing record we also check the presence of the URN as related identifier
44-
#e.g. my-metadata-identifier
45-
deposits <- NULL
46-
if(!is.null(entity$identifiers[["doi"]])){
47-
#TODO review if getting deposit with concept DOI we don't inherit bucket link for doing file upload with new API
48-
deposit <- try(ZENODO$getDepositionByConceptDOI(entity$identifiers[["doi"]]), silent = TRUE) #try to get latest record with concept DOI
49-
if(is(deposit, "try-error")) deposit = NULL
50-
if(is.null(deposit)) deposit <- ZENODO$getDepositionByDOI(entity$identifiers[["doi"]]) #try to get record with specific DOI
51-
if(!is.null(deposit)) deposits <- list(deposit)
52-
}
53-
54-
if(is.null(deposits)){
55-
deposit <- NULL
56-
if(!is.null(entity$identifiers[["zenodo_doi_to_save"]])){
57-
deposit <- ZENODO$getDepositionByDOI(entity$identifiers[["zenodo_doi_to_save"]]) #try to get record with specific DOI
36+
#get_zenodo_metadata
37+
get_zenodo_metadata = function(){
38+
#zenodo object
39+
zen_meta <- NULL
40+
41+
#how to deal with existing records / new versions
42+
#we try first to use DOI if existing, assuming it's a concept DOI, if not a simple DOI
43+
#if nothing is found, we use the default get depositions by identifier, used if no DOI is specified
44+
#this approach is possible because the Zenodo record has a related identifier as 'other', specified when
45+
#creating the record. For existing record we also check the presence of the 'other' as related identifier
46+
#e.g. my-metadata-identifier
47+
deposits <- NULL
48+
if(!is.null(entity$identifiers[["doi"]])){
49+
#TODO review if getting deposit with concept DOI we don't inherit bucket link for doing file upload with new API
50+
deposit <- try(ZENODO$getDepositionByConceptDOI(entity$identifiers[["doi"]]), silent = TRUE) #try to get latest record with concept DOI
51+
if(is(deposit, "try-error")) deposit = NULL
52+
if(is.null(deposit)) deposit <- ZENODO$getDepositionByDOI(entity$identifiers[["doi"]]) #try to get record with specific DOI
53+
if(!is.null(deposit)) deposits <- list(deposit)
5854
}
59-
if(!is.null(deposit)) deposits <- list(deposit)
60-
}
61-
62-
if(is.null(deposits)){
63-
deposits <- ZENODO$getDepositions(q = entity$identifiers[["id"]], size = 1000L)
64-
}
65-
#check related identifier
66-
if(length(deposits)>0){
67-
invisible(lapply(deposits, function(deposit){
68-
related_identifiers <- deposit$metadata$related_identifiers
69-
if(!is.null(related_identifiers)){
70-
for(related_identifier in related_identifiers){
71-
if(related_identifier$scheme == "urn"){
72-
if(related_identifier$identifier == entity$identifiers[["id"]] &
73-
related_identifier$relation_type$id == "isidenticalto"){
74-
zenodo_metadata <<- deposit
75-
break
55+
56+
if(is.null(deposits)){
57+
deposit <- NULL
58+
if(!is.null(entity$identifiers[["zenodo_doi_to_save"]])){
59+
deposit <- ZENODO$getDepositionByDOI(entity$identifiers[["zenodo_doi_to_save"]]) #try to get record with specific DOI
60+
}
61+
if(!is.null(deposit)) deposits <- list(deposit)
62+
}
63+
64+
if(is.null(deposits)){
65+
deposits <- ZENODO$getDepositions(q = entity$identifiers[["id"]], size = 1000L)
66+
}
67+
#check related identifier
68+
if(length(deposits)>0){
69+
invisible(lapply(deposits, function(deposit){
70+
related_identifiers <- deposit$metadata$related_identifiers
71+
if(!is.null(related_identifiers)){
72+
for(related_identifier in related_identifiers){
73+
if(related_identifier$scheme == "other"){
74+
if(related_identifier$identifier == entity$identifiers[["id"]] &
75+
related_identifier$relation_type$id == "isidenticalto"){
76+
zen_meta <<- deposit
77+
break
78+
}
7679
}
7780
}
7881
}
79-
}
80-
}))
82+
}))
83+
}
84+
return(zen_meta)
8185
}
8286

87+
#zenodo_metadata
88+
zenodo_metadata = get_zenodo_metadata()
89+
8390
#doi
8491
doi <- NULL
8592

@@ -99,12 +106,12 @@ function(action, entity, config){
99106
}
100107
zenodo_metadata$addRelatedIdentifier(
101108
identifier = entity$identifiers[["id"]],
102-
scheme = "urn",
109+
scheme = "other",
103110
relation_type = "isidenticalto"
104111
)
105112
record_status <- zenodo_metadata$status
106113
}else{
107-
config$logger.info(sprintf("Zenodo: Existing record with related identifier '%s' (URN scheme)", entity$identifiers[["id"]]))
114+
config$logger.info(sprintf("Zenodo: Existing record with related identifier '%s' ('other' scheme)", entity$identifiers[["id"]]))
108115
update <- TRUE
109116
record_status <- zenodo_metadata$status
110117

@@ -120,6 +127,24 @@ function(action, entity, config){
120127
}
121128
}
122129

130+
if(length(zenodo_metadata$getVersions())>0 && strategy == "newversion"){
131+
#to know if the record has been already published, we check if there is at least one version set
132+
#the property "is_published" is transient to the record and can't be used
133+
if(!depositWithFiles){
134+
#the first run of the action will be skipped and we will wait for the final generic_uploader run
135+
#with depositWithFiles=TRUE to run the version to avoid two versions to be created
136+
config$logger.info(sprintf("Zenodo: record '%s' already published. Skip 1st run to create version with generic uploader at the end of the workflow", zenodo_metadata$id))
137+
return(TRUE)
138+
}
139+
}
140+
141+
if(record_status == "new_version_draft"){
142+
config$logger.info("Draft version already set. Discard initial changes before publishing new version...")
143+
ZENODO$discardChanges(zenodo_metadata$id)
144+
zenodo_metadata = get_zenodo_metadata()
145+
record_status = zenodo_metadata$status
146+
}
147+
123148
#case where bucket is not kept by zenodo we try to get it from added resource
124149
if(!"bucket" %in% names(zenodo_metadata$links)) if(!is.null(entity$resources$zenodo_bucket)){
125150
zenodo_metadata$links$bucket <- entity$resources$zenodo_bucket
@@ -393,38 +418,80 @@ function(action, entity, config){
393418
}
394419
}
395420
}
396-
config$logger.info(sprintf("Deposit record with id '%s' - publish = %s", zenodo_metadata$id, tolower(as.character(publish))))
421+
print(record_status)
397422
out <- switch(record_status,
398-
"draft" = ZENODO$depositRecord(zenodo_metadata, publish = publish),
423+
"draft" = {
424+
config$logger.info(sprintf("Deposit draft record with id '%s' - publish = %s", zenodo_metadata$id, tolower(as.character(publish))))
425+
ZENODO$depositRecord(zenodo_metadata, publish = publish)
426+
},
399427
"published" = {
400428
switch(strategy,
401-
"edition" = ZENODO$depositRecord(zenodo_metadata, publish = publish),
402-
"newversion" = {
403-
data_files <- list.files(file.path(getwd(),"data"), pattern = depositDataPattern, full.names = T)
404-
405-
if(zipEachDataFile){
406-
config$logger.info("Zenodo: 'zipEachDaTafile' is true - zipping data files")
407-
data_files <- lapply(data_files, function(data_file){
408-
data_file = basename(data_file)
409-
config$logger.info(sprintf("Zenodo: 'zipEachDaTafile' is true - zipping each data file '%s'", data_file))
410-
fileparts <- unlist(strsplit(data_file, "\\."))
411-
if(length(fileparts)>1) fileparts <- fileparts[1:(length(fileparts)-1)]
412-
filename <- paste0(fileparts, collapse = ".")
413-
outfilename <- file.path(getwd(), "data", paste0(filename, ".zip"))
414-
zip::zipr(zipfile = outfilename, files = data_file)
415-
return(outfilename)
416-
})
417-
}
418-
419-
metadata_files <- list.files(file.path(getwd(),"metadata"), full.names = TRUE)
420-
files_to_upload <- if(depositWithFiles & (!update | (update & update_files))) c(data_files, metadata_files) else NULL
421-
ZENODO$depositRecordVersion(
422-
record = zenodo_metadata,
423-
delete_latest_files = deleteOldFiles,
424-
files = files_to_upload,
425-
publish = publish
426-
)
427-
}
429+
"edition" = {
430+
config$logger.info(sprintf("Edit published record with id '%s' - publish = %s", zenodo_metadata$id, tolower(as.character(publish))))
431+
ZENODO$depositRecord(zenodo_metadata, publish = publish)
432+
},
433+
"newversion" = {
434+
config$logger.info(sprintf("Deposit record version with id '%s' - publish = %s", zenodo_metadata$id, tolower(as.character(publish))))
435+
data_files <- list.files(file.path(getwd(),"data"), pattern = depositDataPattern, full.names = T)
436+
437+
if(zipEachDataFile){
438+
config$logger.info("Zenodo: 'zipEachDaTafile' is true - zipping data files")
439+
data_files <- lapply(data_files, function(data_file){
440+
data_file = basename(data_file)
441+
config$logger.info(sprintf("Zenodo: 'zipEachDaTafile' is true - zipping each data file '%s'", data_file))
442+
fileparts <- unlist(strsplit(data_file, "\\."))
443+
if(length(fileparts)>1) fileparts <- fileparts[1:(length(fileparts)-1)]
444+
filename <- paste0(fileparts, collapse = ".")
445+
outfilename <- file.path(getwd(), "data", paste0(filename, ".zip"))
446+
zip::zipr(zipfile = outfilename, files = data_file)
447+
return(outfilename)
448+
})
449+
}
450+
451+
metadata_files <- list.files(file.path(getwd(),"metadata"), full.names = TRUE)
452+
files_to_upload <- if(depositWithFiles & (!update | (update & update_files))) c(data_files, metadata_files) else NULL
453+
454+
config$logger.info("Deposit record version...")
455+
ZENODO$depositRecordVersion(
456+
record = zenodo_metadata,
457+
delete_latest_files = deleteOldFiles,
458+
files = files_to_upload,
459+
publish = publish
460+
)
461+
}
462+
)
463+
},
464+
"new_version_draft" = {
465+
switch(strategy,
466+
"newversion" = {
467+
config$logger.info(sprintf("Deposit record version with id '%s' - publish = %s", zenodo_metadata$id, tolower(as.character(publish))))
468+
data_files <- list.files(file.path(getwd(),"data"), pattern = depositDataPattern, full.names = T)
469+
470+
if(zipEachDataFile){
471+
config$logger.info("Zenodo: 'zipEachDaTafile' is true - zipping data files")
472+
data_files <- lapply(data_files, function(data_file){
473+
data_file = basename(data_file)
474+
config$logger.info(sprintf("Zenodo: 'zipEachDaTafile' is true - zipping each data file '%s'", data_file))
475+
fileparts <- unlist(strsplit(data_file, "\\."))
476+
if(length(fileparts)>1) fileparts <- fileparts[1:(length(fileparts)-1)]
477+
filename <- paste0(fileparts, collapse = ".")
478+
outfilename <- file.path(getwd(), "data", paste0(filename, ".zip"))
479+
zip::zipr(zipfile = outfilename, files = data_file)
480+
return(outfilename)
481+
})
482+
}
483+
484+
metadata_files <- list.files(file.path(getwd(),"metadata"), full.names = TRUE)
485+
files_to_upload <- if(depositWithFiles & (!update | (update & update_files))) c(data_files, metadata_files) else NULL
486+
487+
config$logger.info("Deposit record version...")
488+
ZENODO$depositRecordVersion(
489+
record = zenodo_metadata,
490+
delete_latest_files = deleteOldFiles,
491+
files = files_to_upload,
492+
publish = publish
493+
)
494+
}
428495
)
429496
}
430497
)

inst/extdata/workflows/config_metadata_gsheets_zenodo_full.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
"logos": [
88
"https://via.placeholder.com/300x150.png/09f/fff?text=geoflow"
99
],
10-
"mode": "entity"
10+
"mode": "entity",
11+
"options": {
12+
"skipEnrichWithDatatypes": true
13+
}
1114
},
1215
"metadata": {
1316
"entities": [

tests/testthat/test_config_metadata_gsheets_zenodo_full.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ require(testthat)
6161
setwd(wd)
6262

6363
zenodo_published_records <- as.data.frame(readr::read_csv(file.path(EXEC2, "zenodo", "zenodo_pids.csv")))
64-
expect_equal(zenodo_published_records$Identifier, c("my-geoflow-zenodo-record-testnew1", "my-geoflow-zenodo-record-testnew2"))
64+
expect_equal(zenodo_published_records$Identifier, c("my-geoflow-zenodo-record-test1", "my-geoflow-zenodo-record-test2"))
6565
expect_equal(zenodo_published_records$Status, c("published", "published"))
6666
for(id in zenodo_published_records$Identifier){
6767
conceptdoi <- zenodo_published_records[zenodo_published_records$Identifier == id, "DOI_for_allversions"]

0 commit comments

Comments
 (0)