Skip to content

Commit

Permalink
feat: Dynamically obtain software versions, but fall but to static
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie committed Jun 25, 2024
1 parent 84e23b5 commit 47e45c3
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 30 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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", , "[email protected]", role = c("aut", "cre"))
Description: Base package for sharing classes between posit audit
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
13 changes: 10 additions & 3 deletions R/get_posit_cves.R → R/get_posit_remote_versions.R
Original file line number Diff line number Diff line change
@@ -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))
Expand Down
21 changes: 16 additions & 5 deletions R/posit_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion R/quarto-helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
20 changes: 14 additions & 6 deletions R/software-versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion R/update_all_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
4 changes: 3 additions & 1 deletion man/get_posit_versions.Rd

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

17 changes: 9 additions & 8 deletions tests/testthat/test-posit_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()")
}
)
local = get_posit_versions(type, remote = FALSE)
expect_true(all(remote$version %in% local$version),
info = "Try running update_all_versions()"
)
})
7 changes: 4 additions & 3 deletions tests/testthat/test-software-versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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()"
)
})

0 comments on commit 47e45c3

Please sign in to comment.