scripts/fetch_release_urls.R

#!/usr/bin/Rscript

# Updates vignettes/RELEASE_URLS.csv file.
# Should be done manually when a new version of shinycannon is released.
# vignettes/RELEASE_URLS.csv is read by vignettes/shinycannon.Rmd
# To run:
# source(rprojroot::find_package_root_file("scripts/fetch_release_urls.R"))


library(httr)
library(readr)
library(gh)

asset_platform <- function(asset_name) {
  if (grepl("\\.deb$", asset_name)) {
    "deb"
  } else if (grepl("-suse-", asset_name)) {
    "rpm_suse"
  } else if (grepl("\\.rpm$", asset_name)) {
    "rpm_rh"
  } else {
    tools::file_ext(asset_name)
  }
}

asset_df <- function(query_result) {
  recent_release <- query_result$data$repository$releases$nodes[[1]]
  assets <- recent_release$releaseAssets$nodes
  do.call(rbind, lapply(assets, function(asset) {
    data.frame(
      platform = asset_platform(asset$name),
      file = asset$name,
      url = asset$downloadUrl
    )
  }))
}

## Not authenticated. Failes on GHA
# url <- 'https://api.github.com/repos/rstudio/shinycannon/releases'
# json <- jsonlite::parse_json(httr::GET(url))
# latest <- json[[1]]
# str(latest)
# df <- asset_df(latest)

# Test out at https://docs.github.com/en/graphql/overview/explorer
query <- '
query {
  repository(owner:"rstudio", name:"shinycannon") {
    releases(first:1) {
      nodes {
        tagName
        releaseAssets(first:100) {
          nodes {
            name
            downloadUrl
            # release {
            #   id
            # }
            # url
          }
        }
      }
    }
  }
}
'
# Performs authenticated GraphQL queries
query_result <- gh::gh_gql(query)
str(query_result[1]) # Subset to first entry (only entry) to quickly remove attributes
df <- asset_df(query_result)


save_file <- rprojroot::find_package_root_file("vignettes/RELEASE_URLS.csv")
cat(
  sep = "",
  file = save_file,
  "## Generated by scripts/fetch_release_urls.R: do not edit by hand\n",
  "## Please edit GHA in .github/workflows/rituals.yaml\n",
  readr::format_csv(df)
)
message("Updated: ", save_file)
rstudio/shinyloadtest documentation built on Jan. 21, 2023, 11:14 p.m.