Skip to content

Instantly share code, notes, and snippets.

@jakubsob
Last active December 21, 2024 19:45
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(glue::glue("GET /repos/{owner}/{repo}/actions/artifacts"))
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)
system(glue::glue(
'gh api \\
-H "Accept: application/vnd.github+json" \\
-H "X-GitHub-Api-Version: 2022-11-28" \\
/repos/{owner}/{repo}/actions/artifacts/{id}/zip > _snaps.zip'
))
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)
}
.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