Skip to content

Commit

Permalink
Add GITHUB_API_URL envvar handling (#123)
Browse files Browse the repository at this point in the history
Closes #122 

Adds .gh_api_url() to read GITHUB_API_URL envvar, which should support
GH Enterprise usage.
  • Loading branch information
tanho63 authored Jun 11, 2024
1 parent c8f0ffe commit da84484
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: piggyback
Version: 0.1.5.9004
Version: 0.1.5.9005
Title: Managing Larger Data on a GitHub Repository
Description: Helps store files as GitHub release assets, which is a convenient
way for large/binary data files to piggyback onto public and private GitHub
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ limits for pb_download. [#109]
* `pb_download_url()` now can return choice of "browser" or "api" download URLs [#116]
* Add new functions `pb_read()` and `pb_write()` as convenience wrappers around
pattern of downloading to `tempfile()` and then reading into memory. [#97]
* Support customizing GitHub base URL via GITHUB_API_URL environment variable, which should help support GH Enterprise [#122]

# piggyback 0.1.5

Expand Down
2 changes: 1 addition & 1 deletion R/pb_info.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ get_release_assets <- function(releases, r, .token) {
upload_url = releases$upload_url[i],
browser_download_url = .extract_chr(a, "browser_download_url"),
api_download_url = glue::glue(
"https://api.github.com/repos/{r[[1]]}/{r[[2]]}/releases/assets/{.extract_int(a, 'id')}"
"{.gh_api_url()}/repos/{r[[1]]}/{r[[2]]}/releases/assets/{.extract_int(a, 'id')}"
),
id = .extract_int(a, "id"),
state = .extract_chr(a, "state"),
Expand Down
2 changes: 1 addition & 1 deletion R/pb_release_create.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ pb_release_create <- function(repo = guess_repo(),

resp <- httr::RETRY(
verb = "POST",
url = glue::glue("https://api.github.com/repos/{r[[1]]}/{r[[2]]}/releases"),
url = glue::glue("{.gh_api_url()}/repos/{r[[1]]}/{r[[2]]}/releases"),
httr::add_headers(Authorization = paste("token",.token)),
body = jsonlite::toJSON(payload, auto_unbox = TRUE),
terminate_on = c(400, 401, 403, 404, 422)
Expand Down
4 changes: 2 additions & 2 deletions R/pb_release_delete.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pb_release_delete <- function(repo = guess_repo(), tag, .token = gh::gh_token())
resp <- httr::RETRY(
verb = "DELETE",
glue::glue(
"https://api.github.com/repos/{owner}/{repo}/releases/{release_id}",
"{.gh_api_url()}/repos/{owner}/{repo}/releases/{release_id}",
owner = r[1],
repo = r[2],
release_id = release_id
Expand All @@ -48,7 +48,7 @@ pb_release_delete <- function(repo = guess_repo(), tag, .token = gh::gh_token())
resp2 <- httr::RETRY(
verb = "DELETE",
glue::glue(
"https://api.github.com/repos/{owner}/{repo}/git/refs/tags/{tag}",
"{.gh_api_url()}/repos/{owner}/{repo}/git/refs/tags/{tag}",
owner = r[1],
repo = r[2],
tag = tag
Expand Down
11 changes: 11 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,14 @@ guess_repo <- function(path = ".") {
.as_date <- function(x) {
as.Date(x, tz = "UTC")
}

#' GitHub API URL
#'
#' Reads environment variable GITHUB_API_URL to determine base URL of API. Same
#' as gh package. Defaults to `https://api.github.com`.
#'
#' @seealso <https://gh.r-lib.org/#environment-variables>
#' @return string: API base url
.gh_api_url <- function(){
Sys.getenv("GITHUB_API_URL", unset = "https://api.github.com")
}
5 changes: 1 addition & 4 deletions R/utils_testing.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#' - tanho63/piggyback-tests
#' - tanho63/piggyback-private (a private repository)
#'
#' @param test_token github token, typically stored at TAN_GH_TOKEN
#' @param test_repos repos used in testing
#'
#' @keywords internal
#' @return named vector of TRUE or FALSE as to whether the token is configured
#' and can access the test repos.
Expand All @@ -34,7 +31,7 @@
test_repos,
function(repo) {
r <- httr::GET(
paste0("https://api.github.com/repos/",repo),
paste0(.gh_api_url(),"/repos/",repo),
httr::add_headers(Authorization = paste("token", test_token))
)

Expand Down
5 changes: 0 additions & 5 deletions man/dot-check_test_token.Rd

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

18 changes: 18 additions & 0 deletions man/dot-gh_api_url.Rd

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

1 change: 1 addition & 0 deletions man/piggyback-package.Rd

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

1 change: 1 addition & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ reference:
- title: Misc
contents:
- "piggyback-package"
- ".gh_api_url"

0 comments on commit da84484

Please sign in to comment.