From 0372d1c313afca0eeb2ff51d5f71ede08342da2e Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Thu, 27 Jun 2024 21:42:42 -0700 Subject: [PATCH 01/11] Set gh-pages as default branch --- R/z_pages.R | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/R/z_pages.R b/R/z_pages.R index 9945dace6..6925fc272 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -89,7 +89,7 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from initial_commit(local_clone, repo, viz_url) } # Handle gh-pages branch - manage_gh_pages(repo, to_post, local_clone, commit_message) + manage_gh_pages(repo, owner, to_post, local_clone, commit_message, github_repo) message(sprintf( "Visualization will be available at %s\nDeployment via GitHub Pages may take a few minutes...", viz_url)) viz_owner_repo @@ -119,7 +119,7 @@ initial_commit <- function(local_clone, repo, viz_url) { gert::git_push(repo = repo, remote = "origin", set_upstream = TRUE) } -manage_gh_pages <- function(repo, to_post, local_clone, commit_message) { +manage_gh_pages <- function(repo, owner, to_post, local_clone, commit_message, gh_repo_name) { branches <- gert::git_branch_list(local = TRUE, repo = repo) if (!"gh-pages" %in% branches$name) { gert::git_branch_create(repo = repo, branch = "gh-pages") @@ -129,6 +129,13 @@ manage_gh_pages <- function(repo, to_post, local_clone, commit_message) { gert::git_add(files = ".", repo = repo) gert::git_commit(message = commit_message, repo = repo) gert::git_push(remote = "origin", set_upstream = TRUE, repo = repo, force = TRUE) + # Set 'gh-pages' as the default branch on GitHub using GitHub API + gh::gh( + "PATCH /repos/{owner}/{repo}", + owner = owner, + repo = gh_repo_name, + default_branch = "gh-pages" + ) } check_no_github_repo <- function(owner, repo) { From ea183735229789dc6a21d9ab9fd71237a063ddc7 Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Thu, 27 Jun 2024 21:52:47 -0700 Subject: [PATCH 02/11] `repo` = local repo, `gh_repo` = github remote --- R/z_pages.R | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/R/z_pages.R b/R/z_pages.R index 6925fc272..f62b45bb9 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -31,7 +31,7 @@ #' viz, #' github_repo = "my_animint2_plots", #' commit_message = "New animint", -#' private = TRUE) +#' private = FALSE) #' } #' #' @export @@ -138,10 +138,10 @@ manage_gh_pages <- function(repo, owner, to_post, local_clone, commit_message, g ) } -check_no_github_repo <- function(owner, repo) { +check_no_github_repo <- function(owner, gh_repo) { tryCatch( { - gh::gh("/repos/{owner}/{repo}", owner = owner, repo = repo) + gh::gh("/repos/{owner}/{repo}", owner = owner, repo = gh_repo) TRUE }, "http_error_404" = function(err) FALSE From d9782a92203149eaad7957778f636f7f242e6f7f Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Thu, 27 Jun 2024 22:30:09 -0700 Subject: [PATCH 03/11] Add test for default branch --- tests/testthat/test-compiler-ghpages.R | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index abefaa433..f6f0b709a 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -45,3 +45,17 @@ test_that("animint2pages raises an error if no GitHub token is present", { do.call(Sys.setenv, as.list(env.old)) file.copy(config.old, config.file, overwrite = TRUE) }) + +test_that("animint2pages() default branch is gh-pages", { + whoami <- suppressMessages(gh::gh_whoami()) + owner <- whoami[["login"]] + test_default_branch <- function(owner, repo_name) { + repo_info <- gh::gh( + "GET /repos/:owner/:repo", + owner = owner, + repo = repo_name + ) + repo_info$default_branch == "gh-pages" + } + expect_true(test_default_branch(owner, "animint2pages_test_repo")) +}) \ No newline at end of file From 7778b1eeba1751b6c1328ea2045e488631c9ff03 Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Mon, 1 Jul 2024 22:34:59 -0700 Subject: [PATCH 04/11] update test - check head file after clone --- tests/testthat/test-compiler-ghpages.R | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index f6f0b709a..2bf1dead4 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -49,13 +49,15 @@ test_that("animint2pages raises an error if no GitHub token is present", { test_that("animint2pages() default branch is gh-pages", { whoami <- suppressMessages(gh::gh_whoami()) owner <- whoami[["login"]] - test_default_branch <- function(owner, repo_name) { - repo_info <- gh::gh( - "GET /repos/:owner/:repo", - owner = owner, - repo = repo_name - ) - repo_info$default_branch == "gh-pages" + local_repo_path <- tempfile(pattern = "repo_clone_") + gert::git_clone(url = paste0("https://github.com/", owner, "/animint2pages_test_repo.git"), path = local_repo_path) + # Check the default branch after clone + head_file <- file.path(local_repo_path, ".git", "HEAD") + head_content <- readLines(head_file) + if (startsWith(head_content, "ref: refs/heads/")) { + default_branch <- sub("ref: refs/heads/", "", head_content) + } else { + default_branch <- NA } - expect_true(test_default_branch(owner, "animint2pages_test_repo")) + expect_equal(default_branch, "gh-pages") }) \ No newline at end of file From a33e5b0a2363be489f9c0565af6f02d23e7760db Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Fri, 5 Jul 2024 21:15:03 -0700 Subject: [PATCH 05/11] update test, use `gert::git_branch` --- tests/testthat/test-compiler-ghpages.R | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 2bf1dead4..a03461f27 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -52,12 +52,6 @@ test_that("animint2pages() default branch is gh-pages", { local_repo_path <- tempfile(pattern = "repo_clone_") gert::git_clone(url = paste0("https://github.com/", owner, "/animint2pages_test_repo.git"), path = local_repo_path) # Check the default branch after clone - head_file <- file.path(local_repo_path, ".git", "HEAD") - head_content <- readLines(head_file) - if (startsWith(head_content, "ref: refs/heads/")) { - default_branch <- sub("ref: refs/heads/", "", head_content) - } else { - default_branch <- NA - } + default_branch <- gert::git_branch(repo = local_repo_path) expect_equal(default_branch, "gh-pages") }) \ No newline at end of file From f3021bd3cb3ccc094ebe9d7f220875c4ffddc4d5 Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Mon, 8 Jul 2024 23:18:09 -0700 Subject: [PATCH 06/11] Change default branch to gh-pages in inital commit --- R/z_pages.R | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/R/z_pages.R b/R/z_pages.R index f62b45bb9..f14d24720 100644 --- a/R/z_pages.R +++ b/R/z_pages.R @@ -89,7 +89,7 @@ animint2pages <- function(plot.list, github_repo, commit_message = "Commit from initial_commit(local_clone, repo, viz_url) } # Handle gh-pages branch - manage_gh_pages(repo, owner, to_post, local_clone, commit_message, github_repo) + manage_gh_pages(repo, owner, 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 @@ -112,30 +112,23 @@ initial_commit <- function(local_clone, repo, viz_url) { all_branches <- df_or_vec current_master <- df_or_vec } - # do not attempt to rename a branch to "main" when a branch with that name already exists - if (current_master != "main" && !"main" %in% all_branches) { - gert::git_branch_move(branch = current_master, new_branch = "main", repo = repo) + # rename the master/main branch to gh-pages, so that gh-pages become the default branch + if (current_master != "gh-pages" && !"gh-pages" %in% all_branches) { + gert::git_branch_move(branch = current_master, new_branch = "gh-pages", repo = repo) } gert::git_push(repo = repo, remote = "origin", set_upstream = TRUE) } -manage_gh_pages <- function(repo, owner, to_post, local_clone, commit_message, gh_repo_name) { +manage_gh_pages <- function(repo, owner, to_post, local_clone, commit_message) { branches <- gert::git_branch_list(local = TRUE, repo = repo) if (!"gh-pages" %in% branches$name) { - gert::git_branch_create(repo = repo, branch = "gh-pages") + gert::git_branch_create(branch = "gh-pages", repo = repo) } gert::git_branch_checkout("gh-pages", repo = repo) file.copy(to_post, local_clone, recursive = TRUE) gert::git_add(files = ".", repo = repo) gert::git_commit(message = commit_message, repo = repo) gert::git_push(remote = "origin", set_upstream = TRUE, repo = repo, force = TRUE) - # Set 'gh-pages' as the default branch on GitHub using GitHub API - gh::gh( - "PATCH /repos/{owner}/{repo}", - owner = owner, - repo = gh_repo_name, - default_branch = "gh-pages" - ) } check_no_github_repo <- function(owner, gh_repo) { From 23aa2bb58234994189b384af5422f872adfc3294 Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Mon, 8 Jul 2024 23:52:22 -0700 Subject: [PATCH 07/11] just checking the owner name in gh actions --- tests/testthat/test-compiler-ghpages.R | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index a03461f27..668f53859 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -50,6 +50,7 @@ test_that("animint2pages() default branch is gh-pages", { whoami <- suppressMessages(gh::gh_whoami()) owner <- whoami[["login"]] local_repo_path <- tempfile(pattern = "repo_clone_") + print(owner) # just for checking the gh action env gert::git_clone(url = paste0("https://github.com/", owner, "/animint2pages_test_repo.git"), path = local_repo_path) # Check the default branch after clone default_branch <- gert::git_branch(repo = local_repo_path) From 48e61a4b3c4c6827d9e821eedddd162d2e4f1b8f Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Tue, 9 Jul 2024 22:52:48 -0700 Subject: [PATCH 08/11] TO BE REVERTED - test gh env --- tests/testthat/test-compiler-ghpages.R | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 668f53859..737eeb0e4 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -36,6 +36,10 @@ test_that("animint2pages raises an error if no GitHub token is present", { ## be called to set the env vars/token. repo.root <- system("git rev-parse --show-toplevel", intern=TRUE) config.file <- file.path(repo.root, ".git", "config") + # to be reverted, test only + config.content <- readLines(config.file) + print(config.content) + # config.old <- file.path(repo.root, ".git", "config.old") file.copy(config.file, config.old, overwrite = TRUE) cat("[credential]\n\tusername = FOO", file=config.file, append=TRUE) @@ -50,7 +54,10 @@ test_that("animint2pages() default branch is gh-pages", { whoami <- suppressMessages(gh::gh_whoami()) owner <- whoami[["login"]] local_repo_path <- tempfile(pattern = "repo_clone_") - print(owner) # just for checking the gh action env + # to be reverted, test only + print(whoami) + print(Sys.getenv("GITHUB_REPOSITORY")) + # gert::git_clone(url = paste0("https://github.com/", owner, "/animint2pages_test_repo.git"), path = local_repo_path) # Check the default branch after clone default_branch <- gert::git_branch(repo = local_repo_path) From f4b9e55dc6c09f03e1add43a3729ee9675635351 Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Tue, 9 Jul 2024 23:10:02 -0700 Subject: [PATCH 09/11] try clone the test repo under animint org --- tests/testthat/test-compiler-ghpages.R | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 737eeb0e4..0652a8928 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -36,10 +36,6 @@ test_that("animint2pages raises an error if no GitHub token is present", { ## be called to set the env vars/token. repo.root <- system("git rev-parse --show-toplevel", intern=TRUE) config.file <- file.path(repo.root, ".git", "config") - # to be reverted, test only - config.content <- readLines(config.file) - print(config.content) - # config.old <- file.path(repo.root, ".git", "config.old") file.copy(config.file, config.old, overwrite = TRUE) cat("[credential]\n\tusername = FOO", file=config.file, append=TRUE) @@ -52,12 +48,13 @@ test_that("animint2pages raises an error if no GitHub token is present", { test_that("animint2pages() default branch is gh-pages", { whoami <- suppressMessages(gh::gh_whoami()) - owner <- whoami[["login"]] + if (is.null(whoami)) { + repo_full_name <- Sys.getenv("GITHUB_REPOSITORY") + owner <- strsplit(repo_full_name, "/")[[1]][1] + } else { + owner <- whoami[["login"]] + } local_repo_path <- tempfile(pattern = "repo_clone_") - # to be reverted, test only - print(whoami) - print(Sys.getenv("GITHUB_REPOSITORY")) - # gert::git_clone(url = paste0("https://github.com/", owner, "/animint2pages_test_repo.git"), path = local_repo_path) # Check the default branch after clone default_branch <- gert::git_branch(repo = local_repo_path) From 9076ebef92861f7c6e012dcb5760ca26e8424c6f Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Tue, 9 Jul 2024 23:33:17 -0700 Subject: [PATCH 10/11] try hard-coded username --- tests/testthat/test-compiler-ghpages.R | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 0652a8928..43531af0d 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -49,8 +49,7 @@ test_that("animint2pages raises an error if no GitHub token is present", { test_that("animint2pages() default branch is gh-pages", { whoami <- suppressMessages(gh::gh_whoami()) if (is.null(whoami)) { - repo_full_name <- Sys.getenv("GITHUB_REPOSITORY") - owner <- strsplit(repo_full_name, "/")[[1]][1] + owner <- "tdhock" } else { owner <- whoami[["login"]] } From a3516e6dd745eb6cd0d9c557371d10ae67cc64f7 Mon Sep 17 00:00:00 2001 From: Faye-yufan Date: Tue, 9 Jul 2024 23:55:28 -0700 Subject: [PATCH 11/11] Use a new test repo cuz the previous one has `main` as the default branch and cannot be deleted via gert/gh --- tests/testthat/test-compiler-ghpages.R | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/testthat/test-compiler-ghpages.R b/tests/testthat/test-compiler-ghpages.R index 43531af0d..3c5271ca0 100644 --- a/tests/testthat/test-compiler-ghpages.R +++ b/tests/testthat/test-compiler-ghpages.R @@ -54,7 +54,8 @@ test_that("animint2pages() default branch is gh-pages", { owner <- whoami[["login"]] } local_repo_path <- tempfile(pattern = "repo_clone_") - gert::git_clone(url = paste0("https://github.com/", owner, "/animint2pages_test_repo.git"), path = local_repo_path) + test_viz_repo <- animint2pages(viz, github_repo = "a2p_ghp_test") + gert::git_clone(url = paste0("https://github.com/", owner, "/a2p_ghp_test.git"), path = local_repo_path) # Check the default branch after clone default_branch <- gert::git_branch(repo = local_repo_path) expect_equal(default_branch, "gh-pages")