Skip to content

Commit

Permalink
external: add some basic validation of inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleam committed Aug 5, 2024
1 parent 70813da commit c6d6233
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
1 change: 1 addition & 0 deletions R/aaa.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ RISK_LEVELS <- c("NA - unexpected", "High Risk", "Medium Risk", "Low Risk")

utils::globalVariables(c("."))

METRIC_CATEGORIES <- c("testing", "documentation", "maintenance", "transparency")

DOCUMENTATION_METRICS <- c("has_vignettes", "has_website", "has_news") #, export_help)
MAINTENANCE_METRICS <- c("has_maintainer", "news_current")#, "last_30_bugs_status")
Expand Down
44 changes: 44 additions & 0 deletions R/external.R
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,26 @@ build_pkg_scores <- function(results_dir) {
metadata = list(jsonlite::read_json(meta_json))
)

to_drop <- c(
"mpn_scorecard_version", "out_dir", "pkg_tar_path", "md5sum_check",
"category_scores"
)
extkeys <- SCORECARD_JSON_KEYS[!SCORECARD_JSON_KEYS %in% to_drop]
assert_json_keys(res, extkeys)
assert_json_keys(res[["metadata"]], c("date", "executor"))

scores <- res[["scores"]]
# In general, these score names aren't are hard-coded set, but there should at
# least be one per category...
assert_json_keys(scores, METRIC_CATEGORIES)
nscores_per_cat <- purrr::map_int(scores[METRIC_CATEGORIES], length)
if (any(nscores_per_cat < 1)) {
no_scores <- names(scores[METRIC_CATEGORIES])[nscores_per_cat < 1]
abort(c("The following categories do not have any scores:", no_scores))
}
# ... and check and coverage scores should be present.
assert_json_keys(scores[["testing"]], c("check", "coverage"))

return(calc_overall_scores(res))
}

Expand All @@ -211,6 +231,10 @@ read_traceability_matrix <- function(results_dir) {

entries <- yaml::read_yaml(fname)
entries <- purrr::discard(entries, function(e) isTRUE(e[["skip"]]))
for (e in entries) {
assert_keys(e, c("entrypoint", "code", "doc", "tests"), yaml::as.yaml)
}

tibble::tibble(
entrypoint = purrr::map_chr(entries, "entrypoint"),
code_file = purrr::map_chr(entries, "code"),
Expand All @@ -233,8 +257,28 @@ read_coverage_results <- function(results_dir) {

data <- jsonlite::read_json(fname)
filecov <- data[["files"]]
for (e in filecov) {
assert_json_keys(e, c("file", "coverage"))
}

tibble::tibble(
code_file = purrr::map_chr(filecov, "file"),
test_coverage = purrr::map_dbl(filecov, "coverage")
)
}

assert_json_keys <- function(entry, keys) {
assert_keys(
entry,
keys,
function(...) jsonlite::toJSON(..., auto_unbox = TRUE, pretty = TRUE)
)
}

assert_keys <- function(entry, keys, render_fn) {
for (k in keys) {
if (is.null(entry[[k]])) {
abort(paste0("entry is missing key: ", k, "\n", render_fn(entry)))
}
}
}

0 comments on commit c6d6233

Please sign in to comment.