|
| 1 | +#' Configure a GitHub Pages site |
| 2 | +#' |
| 3 | +#' Activates or reconfigures a GitHub Pages site for a project hosted on GitHub. |
| 4 | +#' This function anticipates two specific usage modes: |
| 5 | +#' * Publish from the root directory of a `gh-pages` branch, which is assumed to |
| 6 | +#' be only (or at least primarily) a remote branch. Typically the `gh-pages` |
| 7 | +#' branch is managed by an automatic "build and deploy" job, such as the one |
| 8 | +#' configured by [`use_github_action("pkgdown")`][use_github_action()]. |
| 9 | +#' * Publish from the `"/docs"` directory of a "regular" branch, probably the |
| 10 | +#' repo's default branch. The user is assumed to have a plan for how they will |
| 11 | +#' manage the content below `"/docs"`. |
| 12 | +#' |
| 13 | + |
| 14 | +#' @param branch,path Branch and path for the site source. The default of |
| 15 | +#' `branch = "gh-pages"` and `path = "/"` reflects strong GitHub support for |
| 16 | +#' this configuration: when a `gh-pages` branch is first created, it is |
| 17 | +#' *automatically* published to Pages, using the source found in `"/"`. If a |
| 18 | +#' `gh-pages` branch does not yet exist on the host, `use_github_pages()` |
| 19 | +#' creates an empty, orphan remote branch. |
| 20 | +#' |
| 21 | +#' The most common alternative is to use the repo's default branch, coupled |
| 22 | +#' with `path = "/docs"`. It is the user's responsibility to ensure that this |
| 23 | +#' `branch` pre-exists on the host. |
| 24 | +#' |
| 25 | +#' Note that GitHub does not support an arbitrary `path` and, at the time of |
| 26 | +#' writing, only `"/"` or `"/docs"` are accepted. |
| 27 | + |
| 28 | +#' @param cname Optional, custom domain name. The `NA` default means "don't set |
| 29 | +#' or change this", whereas a value of `NULL` removes any previously |
| 30 | +#' configured custom domain. |
| 31 | +#' |
| 32 | +#' Note that this *can* add or modify a CNAME file in your repository. If you |
| 33 | +#' are using Pages to host a pkgdown site, it is better to specify its URL in |
| 34 | +#' the pkgdown config file and let pkgdown manage CNAME. |
| 35 | +#' |
| 36 | + |
| 37 | +#' @seealso |
| 38 | +#' * [use_tidy_pkgdown()] combines `use_github_pages()` with other functions to |
| 39 | +#' fully configure a pkgdown site |
| 40 | +#' * <https://docs.github.com/en/free-pro-team@latest/github/working-with-github-pages> |
| 41 | +#' * <https://docs.github.com/en/free-pro-team@latest/rest/reference/repos#pages> |
| 42 | + |
| 43 | +#' @return Site metadata returned by the GitHub API, invisibly |
| 44 | +#' @export |
| 45 | +#' |
| 46 | +#' @examples |
| 47 | +#' \dontrun{ |
| 48 | +#' use_github_pages() |
| 49 | +#' use_github_pages(branch = git_branch_default(), path = "/docs") |
| 50 | +#' } |
| 51 | +use_github_pages <- function(branch = "gh-pages", path = "/", cname = NA) { |
| 52 | + stopifnot(is_string(branch), is_string(path)) |
| 53 | + stopifnot(is.na(cname) || is.null(cname) || is_string(cname)) |
| 54 | + tr <- target_repo(github_get = TRUE) |
| 55 | + if (!isTRUE(tr$can_push)) { |
| 56 | + ui_stop(" |
| 57 | + You don't seem to have push access for {ui_value(tr$repo_spec)}, which \\ |
| 58 | + is required to turn on GitHub Pages.") |
| 59 | + } |
| 60 | + |
| 61 | + gh <- function(endpoint, ...) { |
| 62 | + gh::gh( |
| 63 | + endpoint, |
| 64 | + ..., |
| 65 | + owner = tr$repo_owner, repo = tr$repo_name, .api_url = tr$api_url |
| 66 | + ) |
| 67 | + } |
| 68 | + safe_gh <- purrr::safely(gh) |
| 69 | + |
| 70 | + if (branch == "gh-pages") { |
| 71 | + new_branch <- create_gh_pages_branch(tr, branch = "gh-pages") |
| 72 | + if (new_branch) { |
| 73 | + # merely creating gh-pages branch automatically activates publishing |
| 74 | + # BUT we need to give the servers time to sync up before a new GET |
| 75 | + # retrieves accurate info... ask me how I know |
| 76 | + Sys.sleep(2) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + site <- safe_gh("GET /repos/{owner}/{repo}/pages")[["result"]] |
| 81 | + |
| 82 | + if (is.null(site)) { |
| 83 | + ui_done("Activating GitHub Pages for {ui_value(tr$repo_spec)}") |
| 84 | + site <- gh( |
| 85 | + "POST /repos/{owner}/{repo}/pages", |
| 86 | + source = list(branch = branch, path = path), |
| 87 | + .accept = "application/vnd.github.switcheroo-preview+json" |
| 88 | + ) |
| 89 | + } |
| 90 | + |
| 91 | + need_update <- |
| 92 | + site$source$branch != branch || |
| 93 | + site$source$path != path || |
| 94 | + (is.null(cname) && !is.null(site$cname)) || |
| 95 | + (is_string(cname) && (is.null(site$cname) || cname != site$cname)) |
| 96 | + |
| 97 | + if (need_update) { |
| 98 | + args <- list( |
| 99 | + endpoint = "PUT /repos/{owner}/{repo}/pages", |
| 100 | + source = list(branch = branch, path = path) |
| 101 | + ) |
| 102 | + if (is.null(cname) && !is.null(site$cname)) { |
| 103 | + # this goes out as a JSON `null`, which is necessary to clear cname |
| 104 | + args$cname <- NA |
| 105 | + } |
| 106 | + if (is_string(cname) && (is.null(site$cname) || cname != site$cname)) { |
| 107 | + args$cname <- cname |
| 108 | + } |
| 109 | + Sys.sleep(2) |
| 110 | + exec(gh, !!!args) |
| 111 | + Sys.sleep(2) |
| 112 | + site <- safe_gh("GET /repos/{owner}/{repo}/pages")[["result"]] |
| 113 | + } |
| 114 | + |
| 115 | + ui_done("GitHub Pages is publishing from:") |
| 116 | + if (!is.null(site$cname)) { |
| 117 | + kv_line("Custom domain", site$cname) |
| 118 | + } |
| 119 | + kv_line("URL", site$html_url) |
| 120 | + kv_line("Branch", site$source$branch) |
| 121 | + kv_line("Path", site$source$path) |
| 122 | + |
| 123 | + invisible(site) |
| 124 | +} |
| 125 | + |
| 126 | +# returns FALSE if it does NOT create the branch (because it already exists) |
| 127 | +# returns TRUE if it does create the branch |
| 128 | +create_gh_pages_branch <- function(tr, branch = "gh-pages") { |
| 129 | + gh <- function(endpoint, ...) { |
| 130 | + gh::gh( |
| 131 | + endpoint, |
| 132 | + ..., |
| 133 | + owner = tr$repo_owner, repo = tr$repo_name, .api_url = tr$api_url |
| 134 | + ) |
| 135 | + } |
| 136 | + safe_gh <- purrr::safely(gh) |
| 137 | + |
| 138 | + branch_GET <- safe_gh( |
| 139 | + "GET /repos/{owner}/{repo}/branches/{branch}", |
| 140 | + branch = branch |
| 141 | + ) |
| 142 | + |
| 143 | + if (!inherits(branch_GET$error, "http_error_404")) { |
| 144 | + return(FALSE) |
| 145 | + } |
| 146 | + |
| 147 | + ui_done(" |
| 148 | + Initializing empty, orphan {ui_value(branch)} branch in GitHub repo \\ |
| 149 | + {ui_value(tr$repo_spec)}") |
| 150 | + |
| 151 | + # git hash-object -t tree /dev/null |
| 152 | + sha_empty_tree <- "4b825dc642cb6eb9a060e54bf8d69288fbee4904" |
| 153 | + |
| 154 | + # Create commit with empty tree |
| 155 | + res <- gh( |
| 156 | + "POST /repos/{owner}/{repo}/git/commits", |
| 157 | + message = "first commit", |
| 158 | + tree = sha_empty_tree |
| 159 | + ) |
| 160 | + |
| 161 | + # Assign ref to above commit |
| 162 | + gh( |
| 163 | + "POST /repos/{owner}/{repo}/git/refs", |
| 164 | + ref = "refs/heads/gh-pages", |
| 165 | + sha = res$sha |
| 166 | + ) |
| 167 | + |
| 168 | + TRUE |
| 169 | +} |
0 commit comments