cynkra / fledge

Wings for your R packages: Streamline the process of versioning R packages and updating NEWS
https://fledge.cynkra.com
187 stars 11 forks source link

CRAN watching automation #648

Closed maelle closed 1 year ago

maelle commented 1 year ago

@krlmlr code that could run every... 4 hours?

pkg <- "tibble"

temp_file <- withr::local_tempfile()

curl::curl_download(
  sprintf("https://cran.r-project.org/package=%s", pkg),
  temp_file
)

pkg_cran_page <- xml2::read_html(temp_file)
pkg_version_pre <- xml2::xml_find_first(pkg_cran_page, ".//td[text()='Version:']")
pkg_version <- xml2::xml_siblings(pkg_version_pre)[[1]] |>
  xml2::xml_text()

# treat binaries link
tibblify_binary_link <- function(link, os) {
  binary_version <- sub(sprintf("%s_", pkg), "", fs::path_ext_remove(fs::path_file(link)))
  r_version = fs::path_file(fs::path_dir(link))

  tibble::tibble(
    binary_version = binary_version,
    r_version = r_version,
    os = os
  )
}

# windows binaries
windows_binaries <- xml2::xml_find_all(
  pkg_cran_page, 
  ".//a[starts-with(@href, '../../../bin/windows/contrib/')]"
) |>
  xml2::xml_attr("href") |>
  purrr::map_df(tibblify_binary_link, os = "windows")

# macOS binaries
mac_binaries <- xml2::xml_find_all(
  pkg_cran_page, 
  ".//a[starts-with(@href, '../../../bin/macosx/')]"
) |>
  xml2::xml_attr("href") |>
  purrr::map_df(tibblify_binary_link, os = "mac") 

# put it together
binaries <- rbind(windows_binaries, mac_binaries)
binaries[["up_to_date"]] <- binaries[["binary_version"]] == pkg_version

all_ok <- all(binaries[["up_to_date"]])

binaries
#> # A tibble: 7 × 4
#>   binary_version r_version os      up_to_date
#>   <chr>          <chr>     <chr>   <lgl>     
#> 1 3.2.0          4.3       windows TRUE      
#> 2 3.1.8          4.2       windows FALSE     
#> 3 3.1.8          4.1       windows FALSE     
#> 4 3.2.0          4.2       mac     TRUE      
#> 5 3.2.0          4.1       mac     TRUE      
#> 6 3.1.8          4.2       mac     FALSE     
#> 7 3.1.8          4.1       mac     FALSE
all_ok
#> [1] FALSE

Created on 2023-03-10 with reprex v2.0.2

maelle commented 1 year ago

probably needs a tweak to not count the same R version twice (see macOS)

krlmlr commented 1 year ago

Thanks! There are two flavors of macOS, and I see another use case for rematch2:

pkg <- "tibble"

temp_file <- withr::local_tempfile()

curl::curl_download(
  sprintf("https://cran.r-project.org/package=%s", pkg),
  temp_file
)

pkg_cran_page <- xml2::read_html(temp_file)
pkg_version_pre <- xml2::xml_find_first(pkg_cran_page, ".//td[text()='Version:']")
pkg_version <- xml2::xml_siblings(pkg_version_pre)[[1]] |>
  xml2::xml_text()

# treat binaries link
tibblify_binary_link <- function(link) {
  rematch2::re_match(link, "/bin/(?<flavor>.+)/contrib/(?<r_version>[^/]+)/[^_]+_(?<binary_version>[-0-9.]+)[.][a-z]+$")
}

# binaries
binaries <- xml2::xml_find_all(
  pkg_cran_page,
  ".//a[starts-with(@href, '../../../bin/')]"
) |>
  xml2::xml_attr("href") |>
  purrr::map_df(tibblify_binary_link)

# put it together
binaries[["up_to_date"]] <- (binaries[["binary_version"]] == pkg_version)

all_ok <- all(binaries[["up_to_date"]])

binaries
#> # A tibble: 7 × 6
#>   flavor               r_version binary_version .text             .match up_to…¹
#>   <chr>                <chr>     <chr>          <chr>             <chr>  <lgl>  
#> 1 windows              4.3       3.2.0          ../../../bin/win… /bin/… TRUE   
#> 2 windows              4.2       3.2.0          ../../../bin/win… /bin/… TRUE   
#> 3 windows              4.1       3.2.0          ../../../bin/win… /bin/… TRUE   
#> 4 macosx/big-sur-arm64 4.2       3.2.0          ../../../bin/mac… /bin/… TRUE   
#> 5 macosx/big-sur-arm64 4.1       3.2.0          ../../../bin/mac… /bin/… TRUE   
#> 6 macosx               4.2       3.2.0          ../../../bin/mac… /bin/… TRUE   
#> 7 macosx               4.1       3.2.0          ../../../bin/mac… /bin/… TRUE   
#> # … with abbreviated variable name ¹​up_to_date
all_ok
#> [1] TRUE

Created on 2023-03-11 with reprex v2.0.2

krlmlr commented 1 year ago

How do I detect if a release branch/PR is currently open? How do I get the branch name? How do I switch a clone to that branch?