-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathutils-gh.R
65 lines (57 loc) · 1.79 KB
/
utils-gh.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Functions that are in a grey area between usethis and gh
gh_tr <- function(tr) {
force(tr)
function(endpoint, ...) {
gh::gh(
endpoint,
...,
owner = tr$repo_owner, repo = tr$repo_name, .api_url = tr$api_url
)
}
}
# Functions inlined from gh ----
get_baseurl <- function(url) { # https://github.uni.edu/api/v3/
if (!any(grepl("^https?://", url))) {
stop("Only works with HTTP(S) protocols")
}
prot <- sub("^(https?://).*$", "\\1", url) # https://
rest <- sub("^https?://(.*)$", "\\1", url) # github.uni.edu/api/v3/
host <- sub("/.*$", "", rest) # github.uni.edu
paste0(prot, host) # https://github.uni.edu
}
# https://api.github.com --> https://github.com
# api.github.com --> github.com
normalize_host <- function(x) {
sub("api[.]github[.]com", "github.com", x)
}
get_hosturl <- function(url) {
url <- get_baseurl(url)
normalize_host(url)
}
# (almost) the inverse of get_hosturl()
# https://github.com --> https://api.github.com
# https://github.uni.edu --> https://github.uni.edu/api/v3
get_apiurl <- function(url) {
host_url <- get_hosturl(url)
prot_host <- strsplit(host_url, "://", fixed = TRUE)[[1]]
if (is_github_dot_com(host_url)) {
paste0(prot_host[[1]], "://api.github.com")
} else if(is_github_enterprise(host_url)) {
paste0(prot_host[[1]], "://api.", prot_host[[2]])
} else {
paste0(host_url, "/api/v3")
}
}
is_github_enterprise <- function(url) {
url <- get_baseurl(url)
url <- normalize_host(url)
grepl("ghe.com$", url)
}
is_github_dot_com <- function(url) {
url <- get_baseurl(url)
url <- normalize_host(url)
grepl("^https?://github.com", url)
}
default_api_url <- function() {
Sys.getenv("GITHUB_API_URL", unset = "https://api.github.com")
}