Skip to content

Commit

Permalink
refactor: use {gh} in lieu of {httr2} for calling the REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle committed Oct 11, 2024
1 parent b4f7be8 commit 49943f2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 76 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ Imports:
gert,
gh,
gitcreds,
httr2,
magrittr
Suggests:
httptest2,
Expand Down
96 changes: 21 additions & 75 deletions R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -118,71 +118,30 @@ get_contributors <- function (org, repo,
#' @family github
#' @export
get_gh_code_contributors <- function (org, repo, alphabetical = FALSE) {

tok <- get_gh_token ()
if (length (tok) == 0) {
tok <- ""
}

# This query can not be done via GraphQL, so have to use v3 REST API
u <- paste0 (
"https://api.github.com/repos/",
endpoint <- paste0 (
"/repos/",
org,
"/",
repo,
"/contributors"
)

make_req <- function (u, tok, per_page = 100L, page_num = 1L) {

req <- httr2::request (u)
if (nchar (tok) > 0L) {
headers <- list (Authorization = paste0 ("Bearer ", tok))
req <- httr2::req_headers (req, "Authorization" = headers)
}
req <- httr2::req_url_query (req, per_page = per_page, page_num = page_num)
httr2::req_method (req, "GET")
}

per_page <- 100L
page_num <- 1L
req <- make_req (u, tok, per_page = per_page, page_num = page_num)
res <- gh::gh (endpoint, .limit = Inf)

resp <- httr2::req_perform (req)
httr2::resp_check_status (resp)

x <- httr2::resp_body_json (resp, simplifyVector = TRUE)

res <- x
while (nrow (x) == per_page) {
page_num <- page_num + 1L
req <- make_req (u, tok, per_page = per_page, page_num = page_num)
resp <- httr2::req_perform (req)
httr2::resp_check_status (resp)

x <- httr2::resp_body_json (resp, simplifyVector = TRUE)
res <- rbind (res, x)
}
out <- data.frame (
logins = map_chr(res, "login"),
contributions = map_chr(res, "contributions"),
avatar = map_chr(res, "avatar_url"),
stringsAsFactors = FALSE
)

index <- seq_len (nrow (res))
if (alphabetical) {
index <- order (res$login)
out [ order (res$login), ]
} else {
out
}

data.frame (
logins = res$login,
contributions = res$contributions,
avatar = res$avatar_url,
stringsAsFactors = FALSE
) [index, ]
}

get_gh_token <- function (token = "") {

tryCatch (
gitcreds::gitcreds_get ()$password,
error = function (e) ""
)
}

get_git_user <- function () {
Expand Down Expand Up @@ -496,39 +455,22 @@ get_gh_contrib_issue <- function (org, repo) {
return (NULL)
}

tok <- get_gh_token ()

u <- paste0 (
"https://api.github.com/repos/",
endpoint <- paste0 (
"/repos/",
org,
"/",
repo,
"/issues/",
issue_num
)

req <- httr2::request (u)

if (nchar (tok) > 0) {
headers <- list (Authorization = paste0 ("Bearer ", tok))
req <- httr2::req_headers (req, "Authorization" = headers)
}
params <- list (state = "all", per_page = 100, page = 1)
req <- httr2::req_body_json (req, params)
req <- httr2::req_method (req, "GET")

resp <- httr2::req_perform (req)
httr2::resp_check_status (resp)

x <- httr2::resp_body_json (resp, simplifyVector = TRUE)
x <- gh::gh(endpoint, .limit = Inf)
cmts <- x$body

# That's just the body of the opening comment; the following lines extract
# all subsequent comments:
req <- httr2::req_url_path_append (req, "comments")
resp <- httr2::req_perform (req)
httr2::resp_check_status (resp)
x <- httr2::resp_body_json (resp, simplifyVector = TRUE)
endpoint <- paste0 (endoint, "/comments")
x <- gh::gh(endpoint, .limit = Inf)
cmts <- c (cmts, x$body)

pings <- lapply (cmts, function (i) {
Expand Down Expand Up @@ -568,3 +510,7 @@ check_rate_limit <- function () {
))
}
}

map_chr <- function (x, field) {
unname (unlist (lapply (x, "[", field)))
}
8 changes: 8 additions & 0 deletions R/urlcheck.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,11 @@ check_github_urls <- function (ctbs, quiet = FALSE) {

return (ctbs [index, ])
}

get_gh_token <- function (token = "") {

tryCatch (
gitcreds::gitcreds_get ()$password,
error = function (e) ""
)
}

0 comments on commit 49943f2

Please sign in to comment.