Skip to content

Commit

Permalink
animint2pages test uses gh DELETE POST in animint-test/animint2pages_…
Browse files Browse the repository at this point in the history
…test_repo
  • Loading branch information
Toby Dylan Hocking committed Aug 29, 2024
1 parent 416c19c commit 132b723
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: animint2
Title: Animated Interactive Grammar of Graphics
Version: 2024.8.27
Version: 2024.8.29
URL: https://animint.github.io/animint2/
BugReports: https://github.com/animint/animint2/issues
Authors@R: c(
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Changes in version 2024.8.29 (PR#147)

- animint2pages initial commit README.md uses viz title.
- animint2pages gains owner argument, with default taken from `gh::gh_whoami` (same as before, always returns user, even when token has permissions on an org); this allows user to specify an org where the user/token has write permissions. This is used in a new test-compiler-ghpages.R, which now assumes `PAT_GITHUB` has Administration and Contents permissions for all repos in `animint-test` org, so our test code can delete the `animint2pages_test_repo`, create a new one, and then use animint2pages twice, to test the results of creation/update.

# Changes in version 2024.8.27 (PR#144)

- Remove selenium, combine renderer tests into single CI job.
Expand Down
17 changes: 11 additions & 6 deletions R/z_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#' @param plot.list A named list of ggplots and option lists.
#' @param github_repo The name of the GitHub repository to which the
#' files will be pushed.
#' @param owner The user/org under which the repo will be created, default comes from \code{gh::gh_whoami}.
#' @param commit_message A string specifying the commit message for
#' the pushed files.
#' @param private A logical flag indicating whether the GitHub
Expand Down Expand Up @@ -35,7 +36,7 @@
#' }
#'
#' @export
animint2pages <- function(plot.list, github_repo, commit_message = "Commit from animint2pages", private = FALSE, required_opts = c("title","source"), ...) {
animint2pages <- function(plot.list, github_repo, owner=NULL, commit_message = "Commit from animint2pages", private = FALSE, required_opts = c("title","source"), ...) {
for(opt in required_opts){
if(!opt %in% names(plot.list)){
stop(sprintf("plot.list does not contain option named %s, which is required by animint2pages", opt))
Expand All @@ -61,8 +62,10 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from
stop("The github_repo argument should not contain '/'.")
}
# Check for existing repository
whoami <- suppressMessages(gh::gh_whoami())
owner <- whoami[["login"]]
if(is.null(owner)){
whoami <- suppressMessages(gh::gh_whoami())
owner <- whoami[["login"]]
}
viz_owner_repo <- paste0(owner, "/", github_repo)
local_clone <- tempfile()
if (!check_no_github_repo(owner, github_repo)) {
Expand All @@ -86,7 +89,9 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from
silent = TRUE
)
if (!has_commits) {
initial_commit(local_clone, repo, viz_url)
title <- plot.list[["title"]]
if(!is.character(title))title <- "New animint visualization"
initial_commit(local_clone, repo, viz_url, title)
}
# Handle gh-pages branch
manage_gh_pages(repo, to_post, local_clone, commit_message)
Expand All @@ -95,9 +100,9 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from
list(owner_repo=viz_owner_repo, local_clone=local_clone, viz_url=viz_url, gh_pages_url=sprintf("https://github.com/%s/tree/gh-pages", viz_owner_repo))
}

initial_commit <- function(local_clone, repo, viz_url) {
initial_commit <- function(local_clone, repo, viz_url, title) {
readme_file_path <- file.path(local_clone, "README.md")
header <- "## New animint visualization\n"
header <- sprintf("## %s\n", title)
url_hyperlink <- sprintf("[%s](%s)\n", viz_url, viz_url)
full_content <- paste0(header, url_hyperlink)
writeLines(full_content, readme_file_path)
Expand Down
3 changes: 3 additions & 0 deletions man/animint2pages.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion tests/testthat/test-compiler-ghpages.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,41 @@ test_that("error for viz with no source", {
}, "plot.list does not contain option named source, which is required by animint2pages")
})

## This test requires a fine-grained PAT
## - go to https://github.com/settings/personal-access-tokens/new
## - Resource owner: animint-test
## - Repository access: All repositories
## - Repository permissions: Administration and Contents: read and write.
## - Generate token
## - copy token and paste into PAT_GITHUB on https://github.com/animint/animint2/settings/secrets/actions
## Note that it is animint2pages_test_repo under owner=animint-test (not animint), because we want to limit the damage that a malicious user could do with this token: there are no repos with important data in the animint-test org.
test_that("animint2pages() returns list of meta-data", {
result_list <- animint2pages(viz, github_repo = "animint2pages_test_repo")
## https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#delete-a-repository says The fine-grained token must have the following permission set: "Administration" repository permissions (write) gh api --method DELETE -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" /repos/OWNER/REPO
gh::gh("DELETE /repos/animint-test/animint2pages_test_repo")
## https://docs.github.com/en/rest/repos/repos?apiVersion=2022-11-28#create-an-organization-repository says The fine-grained token must have the following permission set: "Administration" repository permissions (write)
gh::gh("POST /orgs/animint-test/repos", name="animint2pages_test_repo")
## first run of animint2pages creates new data viz.
result_list <- animint2pages(viz, "animint2pages_test_repo", owner="animint-test")
result_list
expect_match(result_list$owner_repo, "animint2pages_test_repo")
expect_match(result_list$viz_url, "github.io/animint2pages_test_repo")
expect_match(result_list$gh_pages_url, "animint2pages_test_repo/tree/gh-pages")
README.md <- file.path(result_list$local_clone, "README.md")
README.lines <- readLines(README.md)
expected.line <- paste("##", viz$title)
expect_identical(README.lines[1], expected.line)
get_tsv <- function(L)Sys.glob(file.path(L$local_clone, "*tsv"))
tsv_files_created <- get_tsv(result_list)
expect_equal(length(tsv_files_created), 1)
## second run of animint2pages updates data viz.
viz.more <- viz
viz.more$five <- ggplot()+
geom_point(aes(
x, x),
data=data.frame(x=1:5))
update_list <- animint2pages(viz.more, "animint2pages_test_repo", owner="animint-test")
tsv_files_updated <- get_tsv(update_list)
expect_equal(length(tsv_files_updated), 2)
})

test_that("animint2pages raises an error if no GitHub token is present", {
Expand Down

0 comments on commit 132b723

Please sign in to comment.