Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
17 changes: 12 additions & 5 deletions R/z_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#' viz,
#' github_repo = "my_animint2_plots",
#' commit_message = "New animint",
#' private = TRUE)
#' private = FALSE)
#' }
#'
#' @export
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -129,12 +129,19 @@ 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) {
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
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-compiler-ghpages.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,19 @@ 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"]]
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")
Comment thread
Faye-yufan marked this conversation as resolved.
Outdated
head_content <- readLines(head_file)
if (startsWith(head_content, "ref: refs/heads/")) {
default_branch <- sub("ref: refs/heads/", "", head_content)
} else {
default_branch <- NA
Comment thread
tdhock marked this conversation as resolved.
Outdated
}
expect_equal(default_branch, "gh-pages")
Comment on lines +88 to +91
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

can this test be added inside the "animint2pages() returns owner/repo string" block above?

})