From 47e45c3b4803b7885a7c8663e3cea3e1c151f269 Mon Sep 17 00:00:00 2001 From: Colin Gillespie Date: Tue, 25 Jun 2024 21:07:05 +0100 Subject: [PATCH] feat: Dynamically obtain software versions, but fall but to static --- DESCRIPTION | 2 +- NEWS.md | 3 +++ ..._tibble.R => get_latest_versions_remote.R} | 2 +- ...sit_cves.R => get_posit_remote_versions.R} | 13 +++++++++--- R/posit_versions.R | 21 ++++++++++++++----- R/quarto-helpers.R | 2 +- R/software-versions.R | 20 ++++++++++++------ R/update_all_versions.R | 2 +- man/get_posit_versions.Rd | 4 +++- tests/testthat/test-posit_versions.R | 17 ++++++++------- tests/testthat/test-software-versions.R | 7 ++++--- 11 files changed, 63 insertions(+), 30 deletions(-) rename R/{create_software_tibble.R => get_latest_versions_remote.R} (97%) rename R/{get_posit_cves.R => get_posit_remote_versions.R} (80%) diff --git a/DESCRIPTION b/DESCRIPTION index 5f660a9..efd1b8f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: audit.base Title: Base package for Posit Checks -Version: 0.6.17 +Version: 0.6.18 Authors@R: person("Jumping", "Rivers", , "info@jumpingrivers.com", role = c("aut", "cre")) Description: Base package for sharing classes between posit audit diff --git a/NEWS.md b/NEWS.md index a820388..34fff7d 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,6 @@ +# audit.base 0.6.18 _2024-06-25_ +- feat: Dynamically obtain software versions, but fall but to static + # audit.base 0.6.17 _2024-06-24_ - chore: Software bump - feat: Automatically extract connect/workbench versions diff --git a/R/create_software_tibble.R b/R/get_latest_versions_remote.R similarity index 97% rename from R/create_software_tibble.R rename to R/get_latest_versions_remote.R index 1af76e4..acf8377 100644 --- a/R/create_software_tibble.R +++ b/R/get_latest_versions_remote.R @@ -1,5 +1,5 @@ # https://gitlab.com/jumpingrivers/services/de/spd/infrastructure-template/-/blob/5c584fced32a6fc8fd7b25b3ea78f6fb7a8bd7ca/template/ansible/scripts/versions.sh -create_software_tibble = function() { +get_latest_versions_remote = function() { r = get_latest_versions_from_posit("r") py = get_latest_versions_from_posit("python") # Drop latest to get all releases diff --git a/R/get_posit_cves.R b/R/get_posit_remote_versions.R similarity index 80% rename from R/get_posit_cves.R rename to R/get_posit_remote_versions.R index df8ea82..3857914 100644 --- a/R/get_posit_cves.R +++ b/R/get_posit_remote_versions.R @@ -1,11 +1,18 @@ get_posit_remote_versions = function(type = c("connect", "workbench")) { + if (!requireNamespace("rvest")) { + cli::cli_alert_info("Missing package - rvest") + return(invisible(NULL)) + } type = match.arg(type) - url = if (type == "connect") "https://docs.posit.co/connect/news/" - else "https://docs.posit.co/ide/news/" + url = if (type == "connect") { + "https://docs.posit.co/connect/news/" + } else { + "https://docs.posit.co/ide/news/" + } page = rvest::read_html(url) sections = rvest::html_elements(page, "section") v_tibbles = purrr::map_df(sections, extract_posit_cves) - all_v = get_all_remote_versions(page) |> + all_v = get_all_remote_versions(page) %>% dplyr::filter(!.data$version %in% v_tibbles$version) %>% dplyr::bind_rows(v_tibbles) %>% dplyr::arrange(dplyr::desc(.data$version)) diff --git a/R/posit_versions.R b/R/posit_versions.R index 845d452..b9251da 100644 --- a/R/posit_versions.R +++ b/R/posit_versions.R @@ -3,16 +3,27 @@ #' Returns a tibble containing the Posit version, date of release, #' and any associated CVEs #' @param type Posit product of interest +#' @param remote Try scrapping NEWS first, but fall back to local if that fails #' @export #' @examples #' get_posit_versions(type = "connect") #' -get_posit_versions = function(type = c("connect", "workbench", "drivers")) { +get_posit_versions = function( + type = c("connect", "workbench", "drivers"), + remote = TRUE) { type = match.arg(type) - fname = system.file("extdata", "versions", paste0(type, ".csv"), - mustWork = TRUE, package = "audit.base" - ) - versions = readr::read_csv(fname, comment = "#", col_types = c("c", "c")) + versions = if (isTRUE(remote)) { + try(get_posit_remote_versions(type), silent = TRUE) + } else { + NULL + } + if ("try-error" %in% class(versions) || is.null(versions)) { + cli::cli_alert_warning("Unable to scrape NEWS page for {type}. Falling back to cache") + fname = system.file("extdata", "versions", paste0(type, ".csv"), + mustWork = TRUE, package = "audit.base" + ) + versions = readr::read_csv(fname, comment = "#", col_types = c("c", "c")) + } versions = dplyr::arrange(versions, dplyr::desc(.data$version)) return(versions) } diff --git a/R/quarto-helpers.R b/R/quarto-helpers.R index 6b570e4..478d76f 100644 --- a/R/quarto-helpers.R +++ b/R/quarto-helpers.R @@ -15,7 +15,7 @@ get_quarto_server_header = function(out) { ~ gt::html(paste(.x, as.character(.y))) ) ) %>% - dplyr::mutate(value = ifelse(is.na(.data$value), "-", .data$value)) |> + dplyr::mutate(value = ifelse(is.na(.data$value), "-", .data$value)) %>% dplyr::distinct() dplyr::select(headers, -"documentation", -"header_docs", -"primary_header") } diff --git a/R/software-versions.R b/R/software-versions.R index bfdfd78..9ae5b6a 100644 --- a/R/software-versions.R +++ b/R/software-versions.R @@ -97,12 +97,20 @@ versions_to_display = function(installed) { dplyr::select(l, -"version_num", -"installed_version_num") } -get_latest_versions = function() { - versions_fname = system.file("extdata", "versions", "software.csv", - package = "audit.base", - mustWork = TRUE - ) - versions = readr::read_csv(versions_fname, comment = "#", col_types = "fc") +get_latest_versions = function(remote = TRUE) { + versions = if (isTRUE(remote)) { + get_latest_versions_remote() + } else { + NULL + } + if ("try-error" %in% class(versions) || is.null(versions)) { + cli::cli_alert_warning("Using cached version for software versions") + versions_fname = system.file("extdata", "versions", "software.csv", + package = "audit.base", + mustWork = TRUE + ) + versions = readr::read_csv(versions_fname, comment = "#", col_types = "fc") + } versions$major = get_major(versions$version) versions$patch = get_patch(versions$version) # Add a version number. diff --git a/R/update_all_versions.R b/R/update_all_versions.R index 04efb3f..1957b74 100644 --- a/R/update_all_versions.R +++ b/R/update_all_versions.R @@ -14,7 +14,7 @@ update_all_versions = function() { file = "inst/extdata/versions/workbench.csv" ) - software = create_software_tibble() + software = get_latest_versions_remote() readr::write_csv(software, file = "inst/extdata/versions/software.csv") return(invisible(NULL)) } diff --git a/man/get_posit_versions.Rd b/man/get_posit_versions.Rd index 2f1ce18..7f412cc 100644 --- a/man/get_posit_versions.Rd +++ b/man/get_posit_versions.Rd @@ -4,10 +4,12 @@ \alias{get_posit_versions} \title{Posit Versions} \usage{ -get_posit_versions(type = c("connect", "workbench", "drivers")) +get_posit_versions(type = c("connect", "workbench", "drivers"), remote = TRUE) } \arguments{ \item{type}{Posit product of interest} + +\item{remote}{Try scrapping NEWS first, but fall back to local if that fails} } \description{ Returns a tibble containing the Posit version, date of release, diff --git a/tests/testthat/test-posit_versions.R b/tests/testthat/test-posit_versions.R index 6382307..6716529 100644 --- a/tests/testthat/test-posit_versions.R +++ b/tests/testthat/test-posit_versions.R @@ -43,13 +43,14 @@ test_that("Testing check server", { test_that("Testing check Posit versions", { type = "connect" remote = get_posit_remote_versions(type) - local = get_posit_versions(type) - expect_true(all(remote$version %in% local$version), - info = "Try running update_all_versions()") + local = get_posit_versions(type, remote = FALSE) + expect_true(all(remote$version %in% local$version), + info = "Try running update_all_versions()" + ) type = "workbench" remote = get_posit_remote_versions(type) - local = get_posit_versions(type) - expect_true(all(remote$version %in% local$version), - info = "Try running update_all_versions()") -} -) \ No newline at end of file + local = get_posit_versions(type, remote = FALSE) + expect_true(all(remote$version %in% local$version), + info = "Try running update_all_versions()" + ) +}) diff --git a/tests/testthat/test-software-versions.R b/tests/testthat/test-software-versions.R index ec2f7c6..1beb741 100644 --- a/tests/testthat/test-software-versions.R +++ b/tests/testthat/test-software-versions.R @@ -46,8 +46,9 @@ test_that("Testing software versions quarto output", { test_that("Ensure that software versions are up to date", { testthat::skip_on_ci() versions = get_latest_versions() - latest = create_software_tibble() + latest = get_latest_versions_remote() # If this test files, try running update_software_csv() first - expect_true(all(latest$version %in% versions$version), - info = "Try running update_all_versions()") + expect_true(all(latest$version %in% versions$version), + info = "Try running update_all_versions()" + ) })