Skip to content

Commit

Permalink
Pass up .limit in all functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bschilder committed Mar 19, 2023
1 parent c21ab73 commit 1e3594d
Show file tree
Hide file tree
Showing 33 changed files with 556 additions and 35 deletions.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(add_owner_repo)
export(description_extract)
export(github_branches)
export(github_code)
export(github_commits)
export(github_dependencies)
export(github_dependents)
Expand All @@ -13,12 +14,15 @@ export(github_pages)
export(github_pages_files)
export(github_pages_vignettes)
export(github_permissions)
export(github_repositories)
export(github_traffic)
export(github_user_events)
export(github_workflows)
export(r_repos)
export(r_repos_data)
export(r_repos_downloads)
export(r_repos_opts)
export(r_repos_upset_queries)
importFrom(RCurl,url.exists)
importFrom(data.table,":=")
importFrom(data.table,.SD)
Expand All @@ -31,6 +35,7 @@ importFrom(data.table,merge.data.table)
importFrom(data.table,rbindlist)
importFrom(data.table,setnafill)
importFrom(data.table,setnames)
importFrom(data.table,setorderv)
importFrom(gh,gh)
importFrom(gh,gh_token)
importFrom(methods,is)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
- `echogithub::readme_header` --> `rworkflows::use_badges`
* `r_repos_data`
- Add new arg `add_hex` to find hex URL with `rworkflows::get_hex`.
* `r_repos`
- New arg `queries`
* New functions:
- `github_user_events`
* Expose `.limit` arg wherever relevant.

## Bug fixes

Expand Down
40 changes: 40 additions & 0 deletions R/github_code.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#' GitHub code
#'
#' Search for GitHub code using specific queries.
#' @source
#' \href{https://docs.github.com/en/search-github/searching-on-github/searching-code}{
#' GitHub Docs: Searching code}
#' @inheritParams github_files
#' @inheritParams gh::gh
#'
#' @export
#' @importFrom gh gh gh_token
#' @importFrom data.table :=
#' @importFrom methods is
#' @examples
#' \dontrun{
#' ## easily exceeds API limit
#' repos <- github_code(query="Package path:DESCRIPTION", .limit=5)
#' }
github_code <- function(query,
token = gh::gh_token(),
.limit = Inf,
verbose = TRUE){
owner_repo <- repo <- NULL;

endpoint <- "https://api.github.com/search/code"
res <- gh::gh(endpoint,
.token = token,
.limit = .limit,
q = query,
#### only beta version supports full-on regex ####
# q = "/(?-i)Package/ path:/(?-i)^DESCRIPTION$/",
per_page = 100)
dt <- gh_to_dt(gh_response = res$items,
verbose = verbose)
dt[,owner_repo:=mapply(repo, FUN=function(x){x$name})]
messager("Returning",formatC(nrow(dt),big.mark = ","),
"GitHub code files.",v=verbose)
return(dt)
}

7 changes: 5 additions & 2 deletions R/github_commits.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
#'
#' Get metadata for all commits to a given GitHub repo.
#' @inheritParams github_files
#' @inheritParams gh::gh
#' @return A nested list of metadata
#'
#' @export
#' @importFrom gh gh gh_token
#' @examples
#' commits <- github_commits(owner="RajLabMSSM",
#' repo="echolocatoR")
#' repo="echolocatoR",
#' limit=100)
github_commits <- function(owner,
repo,
token = gh::gh_token(),
.limit = Inf,
verbose = TRUE) {

#devoptera::args2vars(github_commits)
Expand All @@ -20,7 +23,7 @@ github_commits <- function(owner,
owner,repo,"commits",sep="/")
gh_response <- gh::gh(endpoint = endpoint,
.token = token,
.limit = Inf,
.limit = .limit,
per_page = 100)
dt <- gh_to_dt(gh_response)
return(dt)
Expand Down
3 changes: 3 additions & 0 deletions R/github_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#' @param verbose Print messages.
#' @inheritParams github_files_download
#' @inheritParams base::grepl
#' @inheritParams gh::gh
#'
#' @return A list of paths.
#'
Expand All @@ -47,6 +48,7 @@ github_files <- function(owner,
ignore.case = FALSE,
method = "gh",
token = gh::gh_token(),
.limit = Inf,
download = FALSE,
download_dir = tempdir(),
overwrite = FALSE,
Expand Down Expand Up @@ -77,6 +79,7 @@ github_files <- function(owner,
repo = repo,
branch = branch,
token = token,
.limit = .limit,
verbose = verbose)
}
#### Return NULL early ####
Expand Down
5 changes: 4 additions & 1 deletion R/github_files_gh.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#' List files in GitHub repo: via \pkg{gh}
#'
#' Search for files within a public GitHub repository and return their paths.
#' @inheritParams github_files
#' @source \href{https://docs.github.com/en/rest/overview/endpoints-available-for-github-apps}{
#' GitHub endpoints}
#' @source \href{https://docs.github.com/en/rest/git/trees#get-a-tree-recursively}{
#' GitHub trees API}
#' @inheritParams github_files
#' @inheritParams gh::gh
#' @returns A \link[data.table]{data.table} fo file paths.
#'
#' @keywords internal
Expand All @@ -15,6 +16,7 @@ github_files_gh <- function(owner,
repo,
branch = c("master","main"),
token = gh::gh_token(),
.limit = Inf,
verbose = TRUE) {
path <- link <- NULL;

Expand All @@ -29,6 +31,7 @@ github_files_gh <- function(owner,
)
gh_response <- gh::gh(endpoint = endpoint,
.token = token,
.limit = .limit,
per_page = 100)
dt <- gh_to_dt(gh_response$tree)
dt[,link:=paste("https://github.com",owner,repo,
Expand Down
3 changes: 3 additions & 0 deletions R/github_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#' @param add_traffic Add traffic metadata
#' with \link[echogithub]{github_traffic}.
#' @inheritParams github_files
#' @inheritParams gh::gh
#' @return A \link[data.table]{data.table}.
#'
#' @export
Expand All @@ -16,6 +17,7 @@ github_metadata <- function(owner,
repo,
add_traffic = FALSE,
token = gh::gh_token(),
.limit = Inf,
verbose = TRUE) {

#devoptera::args2vars(github_metadata)
Expand All @@ -37,6 +39,7 @@ github_metadata <- function(owner,
owner,repo,sep="/")
gh_response <- gh::gh(endpoint = endpoint,
.token = token,
.limit = .limit,
per_page = 100)
dt <- cbind(
owner=owner,
Expand Down
3 changes: 3 additions & 0 deletions R/github_pages.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @param error Throw an error when the GitHub repository does not exist
#' (default: \code{TRUE}).
#' @inheritParams github_files
#' @inheritParams gh::gh
#'
#' @export
#' @importFrom gh gh gh_token
Expand All @@ -15,13 +16,15 @@ github_pages <- function(owner,
repo,
error = TRUE,
token = gh::gh_token(),
.limit = Inf,
verbose = TRUE){

endpoint <- paste("https://api.github.com","repos",
owner,repo,"pages",sep="/")
res <- tryCatch({
gh::gh(endpoint = endpoint,
.token = token,
.limit = .limit,
per_page = 100)
}, error = function(e){return(e)})
#### Stop if the repo doesn't exist #####
Expand Down
9 changes: 7 additions & 2 deletions R/github_pages_files.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@
#' @param local_repo Search a cloned local folder of a git repo instead of
#' the remote GitHub repo. If not \code{NULL}, expects a path to the local repo.
#' @inheritParams github_files
#' @inheritParams gh::gh
#'
#' @export
#' @importFrom data.table := fwrite data.table
#' @examples
#' dt <- github_pages_files(owner="RajLabMSSM", repo="echolocatoR")
#' @examples
#' dt <- github_pages_files(owner="RajLabMSSM",
#' repo="echolocatoR",
#' .limit=5)
github_pages_files <- function(owner,
repo,
branch = "gh-pages",
Expand All @@ -20,6 +23,7 @@ github_pages_files <- function(owner,
local_repo = NULL,
save_path = NULL,
token = gh::gh_token(),
.limit = Inf,
verbose = TRUE) {

link_ghpages_index <- link_ghpages <- link_html <- path <- NULL;
Expand All @@ -35,6 +39,7 @@ github_pages_files <- function(owner,
query = query,
branch = branch,
token = token,
.limit = .limit,
verbose = verbose)
} else {
dt <- data.table::data.table(
Expand Down
65 changes: 65 additions & 0 deletions R/github_repositories.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#' GitHub repositories
#'
#' Search for GitHub repositories using specific queries.
#' @source
#' \href{https://docs.github.com/en/rest/search#search-repositories}{
#' GitHub Docs: Search repositories}
#' @source
#' \href{https://docs.github.com/en/rest/search#constructing-a-search-query}{
#' GitHub Docs: Constructing a search query}
#' @source
#' \href{https://docs.github.com/en/search-github/searching-on-github/searching-for-repositories}{
#' Searching GitHub repos}
#' @source
#' \href{https://github.com/orgs/community/discussions/9759}{
#' Case-sensitive GitHub searches}
#' @source \href{https://github.com/r-lib/gh/pull/136}{
#' Examples of using gh to search repositories}
#' @inheritParams github_files
#' @inheritParams gh::gh
#'
#' @export
#' @importFrom gh gh gh_token
#' @importFrom data.table :=
#' @importFrom methods is
#' @examples
#' repos <- github_repositories(query="language:r", .limit=100)
github_repositories <- function(query,
token = gh::gh_token(),
.limit = Inf,
verbose = TRUE){
owner_repo <- repo <- NULL;

endpoint <- "https://api.github.com/search/repositories"
res <- gh::gh(endpoint,
.token = token,
.limit = .limit,
per_page = 100,
q = paste(query, collapse = " ")
)
dt <- gh_to_dt(gh_response = res$items,
verbose = verbose)
dt[,owner_repo:=mapply(repo, FUN=function(x){x$name})]
messager("Returning",formatC(nrow(dt),big.mark = ","),
"GitHub repositories.",v=verbose)
return(dt)
}

# make_query <- function(query,
# sep = ":",
# collapse = " ",
# encode=FALSE) {
# txt <- paste(names(query),
# query,
# sep = sep, collapse = collapse)
# if(encode) utils::URLencode(txt, reserved=TRUE) else {txt}
# }
# endpoint <- paste0("https://api.github.com/",
# "search?q=", make_query(query, encode = TRUE))
# url <- httr::modify_url("https://api.github.com",
# path = "search/",
# query = list(q = make_query(query)))
# req <- httr::GET(url = url)
# cont <- httr::content(req)
# dt <- gh_to_dt(gh_response = cont$items,
# verbose = verbose)
4 changes: 3 additions & 1 deletion R/github_traffic.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' Get traffic info on a target GitHub repository.
#' @param na_fill Value to fill NAs with.
#' @inheritParams github_files
#' @inheritParams gh::gh
#' @return A \link[data.table]{data.table}.
#'
#' @export
Expand All @@ -14,6 +15,7 @@
github_traffic <- function(owner,
repo,
token = gh::gh_token(),
.limit = Inf,
na_fill = NULL,
verbose = TRUE) {

Expand All @@ -38,7 +40,7 @@ github_traffic <- function(owner,
owner,repo,"traffic","clones",sep="/")
ghr_clones <- gh::gh(endpoint = endpoint,
.token = token,
.limit = Inf,
.limit = .limit,
per_page = 100)
clones <- data.table::data.table(
clones_count=ghr_clones$count,
Expand Down
Loading

0 comments on commit 1e3594d

Please sign in to comment.