Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set gh-pages as default branch of new repo created by animint2pages #138

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
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")
Faye-yufan marked this conversation as resolved.
Show resolved Hide resolved
head_content <- readLines(head_file)
if (startsWith(head_content, "ref: refs/heads/")) {
default_branch <- sub("ref: refs/heads/", "", head_content)
} else {
default_branch <- NA
tdhock marked this conversation as resolved.
Show resolved Hide resolved
}
expect_equal(default_branch, "gh-pages")
})
Loading