Skip to content

Commit

Permalink
feat: Automatically extract Posit versions (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
csgillespie authored Jun 25, 2024
1 parent 9b0478f commit 84e23b5
Show file tree
Hide file tree
Showing 16 changed files with 359 additions and 133 deletions.
3 changes: 2 additions & 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.16
Version: 0.6.17
Authors@R:
person("Jumping", "Rivers", , "[email protected]", role = c("aut", "cre"))
Description: Base package for sharing classes between posit audit
Expand All @@ -25,6 +25,7 @@ Imports:
yaml
Suggests:
jsonlite,
rvest,
testthat (>= 3.0.0)
Remotes:
jumpingrivers/serverHeaders
Expand Down
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export(init_r6_check)
export(init_r6_checks)
export(logger)
export(print_colour_versions)
export(update_software_csv)
export(update_all_versions)
import(R6)
importFrom(dplyr,"%>%")
importFrom(rlang,.data)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# audit.base 0.6.17 _2024-06-24_
- chore: Software bump
- feat: Automatically extract connect/workbench versions

# audit.base 0.6.16 _2024-06-24_
- chore: Use jrStyling

Expand Down
11 changes: 0 additions & 11 deletions R/create_software_tibble.R
Original file line number Diff line number Diff line change
@@ -1,14 +1,3 @@
#' Helper function for updating R/Py/Quarto versions
#'
#' Currently this function would only be called by the package author, as it
#' updates inst/extdata/versions/software.csv
#' @export
update_software_csv = function() {
software = create_software_tibble()
readr::write_csv(software, file = "inst/extdata/versions/software.csv")
return(invisible(software))
}

# https://gitlab.com/jumpingrivers/services/de/spd/infrastructure-template/-/blob/5c584fced32a6fc8fd7b25b3ea78f6fb7a8bd7ca/template/ansible/scripts/versions.sh
create_software_tibble = function() {
r = get_latest_versions_from_posit("r")
Expand Down
40 changes: 40 additions & 0 deletions R/get_posit_cves.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
get_posit_remote_versions = function(type = c("connect", "workbench")) {
type = match.arg(type)
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) |>
dplyr::filter(!.data$version %in% v_tibbles$version) %>%
dplyr::bind_rows(v_tibbles) %>%
dplyr::arrange(dplyr::desc(.data$version))
all_v
}

extract_posit_cves = function(section) {
posit_name = rvest::html_attrs(section)
posit_name = as.vector(posit_name["id"])
posit_version = stringr::str_extract(posit_name, "[0-9]{4}\\.[0-9]{1,2}\\.[0-9]{1,2}")

li = section %>%
rvest::html_elements("li") %>%
rvest::html_text2()

cves = stringr::str_extract(li, "^CVE-[0-9]{4}-[0-9]*")
cves = cves[!is.na(cves)]

if (length(cves) == 0L || is.na(posit_version)) {
tibble::tibble(version = character(0), cve = character(0))
} else {
tibble::tibble(version = posit_version, cve = cves)
}
}

get_all_remote_versions = function(page) {
versions = page %>%
rvest::html_nodes("h2") %>%
rvest::html_text()
v = stringr::str_extract(versions, "202[0-9]\\.[0-9]{2}\\.[0-9]{1,2}")
tibble::tibble(version = v[!is.na(v)], cve = "")
}
12 changes: 8 additions & 4 deletions R/posit_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ get_posit_versions = function(type = c("connect", "workbench", "drivers")) {
fname = system.file("extdata", "versions", paste0(type, ".csv"),
mustWork = TRUE, package = "audit.base"
)
versions = readr::read_csv(fname, comment = "#", col_types = c("c", "D", "c"))
versions = dplyr::arrange(versions, dplyr::desc(date))
versions = readr::read_csv(fname, comment = "#", col_types = c("c", "c"))
versions = dplyr::arrange(versions, dplyr::desc(.data$version))
return(versions)
}

Expand Down Expand Up @@ -45,13 +45,17 @@ audit_posit_version = function(posit_version, type = c("connect", "workbench", "
return(invisible(NULL))
}



lookup_version = function(posit_version, type) {
versions = get_posit_versions(type = type)
version_as_date = version_to_date(posit_version)
if (is.na(version_as_date) || version_as_date < min(versions$date)) {
all_dates = unlist(purrr::map(versions$version, version_to_date))

if (is.na(version_as_date) || version_as_date < min(all_dates)) {
# Older than DB
row_number = NA_integer_
} else if (version_as_date > max(versions$date)) {
} else if (version_as_date > max(all_dates)) {
# Newer than DB
row_number = 1L
} else {
Expand Down
20 changes: 20 additions & 0 deletions R/update_all_versions.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#' Helper functions for updating R/Py/Quarto/Posit versions
#'
#' Currently this function would only be called by the package author, as it
#' updates inst/extdata/versions/*.csv
#' @export
update_all_versions = function() {
readr::write_csv(
get_posit_remote_versions(type = "connect"),
file = "inst/extdata/versions/connect.csv"
)

readr::write_csv(
get_posit_remote_versions(type = "workbench"),
file = "inst/extdata/versions/workbench.csv"
)

software = create_software_tibble()
readr::write_csv(software, file = "inst/extdata/versions/software.csv")
return(invisible(NULL))
}
Loading

0 comments on commit 84e23b5

Please sign in to comment.