|
| 1 | +#' GitHub repositories |
| 2 | +#' |
| 3 | +#' Search for GitHub repositories using specific queries. |
| 4 | +#' @source |
| 5 | +#' \href{https://docs.github.com/en/rest/search#search-repositories}{ |
| 6 | +#' GitHub Docs: Search repositories} |
| 7 | +#' @source |
| 8 | +#' \href{https://docs.github.com/en/rest/search#constructing-a-search-query}{ |
| 9 | +#' GitHub Docs: Constructing a search query} |
| 10 | +#' @source |
| 11 | +#' \href{https://docs.github.com/en/search-github/searching-on-github/searching-for-repositories}{ |
| 12 | +#' Searching GitHub repos} |
| 13 | +#' @source |
| 14 | +#' \href{https://github.com/orgs/community/discussions/9759}{ |
| 15 | +#' Case-sensitive GitHub searches} |
| 16 | +#' @source \href{https://github.com/r-lib/gh/pull/136}{ |
| 17 | +#' Examples of using gh to search repositories} |
| 18 | +#' @inheritParams github_files |
| 19 | +#' @inheritParams gh::gh |
| 20 | +#' |
| 21 | +#' @export |
| 22 | +#' @importFrom gh gh gh_token |
| 23 | +#' @importFrom data.table := |
| 24 | +#' @importFrom methods is |
| 25 | +#' @examples |
| 26 | +#' repos <- github_repositories(query="language:r", .limit=100) |
| 27 | +github_repositories <- function(query, |
| 28 | + token = gh::gh_token(), |
| 29 | + .limit = Inf, |
| 30 | + verbose = TRUE){ |
| 31 | + owner_repo <- repo <- NULL; |
| 32 | + |
| 33 | + endpoint <- "https://api.github.com/search/repositories" |
| 34 | + res <- gh::gh(endpoint, |
| 35 | + .token = token, |
| 36 | + .limit = .limit, |
| 37 | + per_page = 100, |
| 38 | + q = paste(query, collapse = " ") |
| 39 | + ) |
| 40 | + dt <- gh_to_dt(gh_response = res$items, |
| 41 | + verbose = verbose) |
| 42 | + dt[,owner_repo:=mapply(repo, FUN=function(x){x$name})] |
| 43 | + messager("Returning",formatC(nrow(dt),big.mark = ","), |
| 44 | + "GitHub repositories.",v=verbose) |
| 45 | + return(dt) |
| 46 | +} |
| 47 | + |
| 48 | +# make_query <- function(query, |
| 49 | +# sep = ":", |
| 50 | +# collapse = " ", |
| 51 | +# encode=FALSE) { |
| 52 | +# txt <- paste(names(query), |
| 53 | +# query, |
| 54 | +# sep = sep, collapse = collapse) |
| 55 | +# if(encode) utils::URLencode(txt, reserved=TRUE) else {txt} |
| 56 | +# } |
| 57 | +# endpoint <- paste0("https://api.github.com/", |
| 58 | +# "search?q=", make_query(query, encode = TRUE)) |
| 59 | +# url <- httr::modify_url("https://api.github.com", |
| 60 | +# path = "search/", |
| 61 | +# query = list(q = make_query(query))) |
| 62 | +# req <- httr::GET(url = url) |
| 63 | +# cont <- httr::content(req) |
| 64 | +# dt <- gh_to_dt(gh_response = cont$items, |
| 65 | +# verbose = verbose) |
0 commit comments