Skip to content

Instantly share code, notes, and snippets.

@jakubsob
Last active July 24, 2025 07:08
Show Gist options
  • Save jakubsob/83a88e62cdb01ba49f0c292399c5a77d to your computer and use it in GitHub Desktop.
Save jakubsob/83a88e62cdb01ba49f0c292399c5a77d to your computer and use it in GitHub Desktop.
Download test snapshots generated on CI
#' Download the latest snapshot artifacts from the CI and update local snapshots
#'
#' @details
#'
#' This function uses Github API to download the latest snapshot artifacts from the CI and update the local snapshots.
#'
#' In order for the API to work it needs to be authenticated.
#'
#' Run the following command in the terminal to authenticate the API:
#' ```
#' gh auth login
#' ```
#'
#' @param branch The name of the branch
#' @param repo The name of the repository
#' @param owner The name of the owner of the repository
#' @param name The name of the artifact
#' @return NULL, called for side effects
.download_ci_snaps <- function(
branch = .get_active_branch(),
repo,
owner,
name) {
artifacts <- gh::gh(
"GET /repos/{owner}/{repo}/actions/artifacts",
owner = owner,
repo = repo,
.token = Sys.getenv("GITHUB_PAT")
)
id <- artifacts |>
purrr::pluck(2) |>
purrr::map(\(x) {
x$branch <- x$workflow_run$head_branch
x
}) |>
dplyr::bind_rows() |>
dplyr::filter(branch == !!branch) |>
dplyr::filter(name == !!name) |>
dplyr::filter(updated_at == max(updated_at)) |>
dplyr::filter(!expired) |>
dplyr::slice_head(n = 1) |>
dplyr::pull(id)
gh::gh(
"/repos/{owner}/{repo}/actions/artifacts/{id}/zip",
owner = owner,
repo = repo,
id = id,
.destfile = "_snaps.zip",
.token = Sys.getenv("GITHUB_PAT")
)
# It takes a moment for fille to be renamed to _snaps.zip, wait for it to happen
while (TRUE) {
if (fs::file_exists("_snaps.zip")) {
break
}
}
unzip("_snaps.zip", exdir = "_snaps")
fs::file_delete("_snaps.zip")
dir_variant_new <- fs::dir_ls("_snaps")[1]
dir_variant_old <- fs::path("tests", "testthat", dir_variant_new)
fs::file_delete(dir_variant_old)
fs::file_move(dir_variant_new, dir_variant_old)
invisible(NULL)
}
.get_active_branch <- function() {
res <- system("git status", intern = TRUE)
stringr::str_remove(res[1], "On branch ")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment