Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- uses: actions/checkout@v3
- name: install and update texlive
run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt update -y -qq
- run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install texlive texlive-fonts-extra -y
- run: /usr/bin/sudo DEBIAN_FRONTEND=noninteractive apt install tidy texlive texlive-fonts-extra -y
- uses: r-lib/actions/setup-r@v2
- uses: r-lib/actions/setup-r-dependencies@v2

Expand Down
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
2 changes: 1 addition & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
#' Housing sales in TX.
#'
#' Information about the housing market in Texas provided by the TAMU
#' real estate center, \url{https://www.recenter.tamu.edu/}.
#' real estate center.
#'
#' @format A data frame with 8602 observations and 9 variables:
#' \describe{
Expand Down
3 changes: 0 additions & 3 deletions R/stat-unique.r
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#' Remove duplicates.
#'
#' @section Aesthetics:
#' \Sexpr[results=rd,stage=build]{animint2:::rd_aesthetics("stat", "unique")}
#'
#' @export
#' @inheritParams layer
#' @inheritParams geom_point
Expand Down
19 changes: 12 additions & 7 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,18 +89,20 @@ 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)
message(sprintf(
"Visualization will be available at %s\nDeployment via GitHub Pages may take a few minutes...", viz_url))
viz_owner_repo
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.

5 changes: 0 additions & 5 deletions man/stat_unique.Rd

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

2 changes: 1 addition & 1 deletion man/txhousing.Rd

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

39 changes: 36 additions & 3 deletions tests/testthat/test-compiler-ghpages.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
acontext("GitHub Pages")

library(animint2)
viz <- animint(
title="one to ten",
source="https://github.com/animint/animint2/tree/master/tests/testthat/test-compiler-ghpages.R",
Expand All @@ -22,9 +23,41 @@ test_that("error for viz with no source", {
}, "plot.list does not contain option named source, which is required by animint2pages")
})

test_that("animint2pages() returns owner/repo string", {
viz_owner_repo <- animint2pages(viz, github_repo = "animint2pages_test_repo")
expect_is(viz_owner_repo, "character")
## 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw that it has already been set, so I don't need to set the PAT again, correct?
Will the PAT expire?

Copy link
Collaborator Author

@tdhock tdhock Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set the PAT to expire in 1 year (that is the max on github)
If you try to run the test locally, it should work, as long as your github token has the right permissions. (it worked for me locally when I used the same token as is currently used on github actions)
Since I added you to the animint-test org as member (can create new repositories), you should be able to run the test code locally with your regular github credentials (no special token required).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually I just tried that myself and I got

> gh::gh("DELETE /repos/animint-test/animint2pages_test_repo")
Error in `gh::gh()`:
! GitHub API error (403): Must have admin rights to Repository.Read more at <https://docs.github.com/rest/repos/repos#delete-a-repository>
Run `rlang::last_trace()` to see where the error occurred.

but I am owner of animint-test org???

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to change base permissions of animint-test org from Read to Admin
image
https://github.com/organizations/animint-test/settings/member_privileges?enable_tip=#base-permissions

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so I got it to work for local testing by doing the following.
first I used gitcreds::gitcreds_delete() to remove my previous git crendential/token, which I believe only has repo read/write permission (not delete).
then I go to https://github.com/settings/tokens/new and check repo and delete_repo then generate token, as shown below.
image
Then I open up git bash on windows, type git push, then a window pops up, click token, paste the token, confirm.
Then running the tests locally should work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some LOCAL TESTING docs in the comments next to this test, does that make sense to you?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, thank you! I will try on my local tonight.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just tested it locally and got it to work!
It seems gitcreds::gitcreds_delete() wasn't deleting my old token, so I used credentials::set_github_pat("my-pat") to reset it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

great thanks, I moved the docs to the wiki https://github.com/animint/animint2/wiki/Testing#installing-a-github-token-for-use-locally and I mentioned your credentials::set_github_pat("my-pat") trick there.
hopefully this technique will make it easier to implement tests in #138

## 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", {
## 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