Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle more than 100 results from repo branch query #4

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: echogithub
Type: Package
Title: echoverse module: Extract data and metadata from GitHub
Version: 0.99.2
Version: 0.99.3
Authors@R:
c(person(given = "Brian",
family = "Schilder",
Expand All @@ -17,7 +17,12 @@ Authors@R:
family = "Raj",
role = c("aut"),
email = "[email protected]",
comment = c(ORCID = "0000-0002-9355-5704"))
comment = c(ORCID = "0000-0002-9355-5704")),
person(given = "Hiranyamaya",
family = "Dash",
role = c("ctb"),
email = "[email protected]",
comment = c(ORCID = "0009-0005-5514-505X"))
)
Description: echoverse module: Extract data and metadata from GitHub.
URL: https://github.com/RajLabMSSM/echogithub
Expand Down Expand Up @@ -56,7 +61,7 @@ Suggests:
Remotes:
github::neurogenomics/cranlogs,
github::neurogenomics/rworkflows
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
VignetteBuilder: knitr
License: GPL-3
Config/testthat/edition: 3
10 changes: 9 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
# echogithub 0.99.3

## Bug fixes

* `github_branches`
- Handle repos with more than 100 branches.


# echogithub 0.99.2

## New features
Expand Down Expand Up @@ -68,4 +76,4 @@
* Switched to using `gh` instead of `httr` to avoid API limits imposed by GitHub.
- Kept `httr` as alternative method.
* `is_url`:
- Add `RCurl::url.exists` check.
- Add `RCurl::url.exists` check.
67 changes: 38 additions & 29 deletions R/github_branches.R
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#' GitHub branches
#'
#'
#' List all branches for a given GitHub repository.
#' @param owner Owner of the GitHub repository.
#' @param owner Owner of the GitHub repository.
#' If \code{NULL}, will automatically try to infer the owner
#' name from the \emph{DESCRIPTION file}
#' (assuming you're working directory is a local R package repo).
#' @param repo GitHub repository name.
#' @param repo GitHub repository name.
#' If \code{NULL}, will automatically try to infer the repo name
#' name from the \emph{DESCRIPTION file}
#' (assuming you're working directory is a local R package repo).
#' @param branch [Optional] If \code{branch} is supplied
#' (as a character vector of one or more branch names),
#' @param branch [Optional] If \code{branch} is supplied
#' (as a character vector of one or more branch names),
#' will check to see if that branch exists. If it does, only that branch will
#' be returned. If it doesn't, an error will be thrown.
#' @param master_or_main If \code{branch} is supplied and
#' is either \code{"master"} or \code{"main"},
#' automatically interpret "master" and "main" as synonymous and return
#' whichever branch exists.
#' automatically interpret "master" and "main" as synonymous and return
#' whichever branch exists.
#' @param as_datatable Return the results as a \link[data.table]{data.table}
#' (\code{TRUE}), or as a character vector of branch names
#' (default: \code{FALSE}).
#' @param error Throw an error when no matching branches are fond.
#' @inheritParams github_files
#' @inheritParams description_extract
#' @returns Character vector or \link[data.table]{data.table} of branches.
#'
#'
#' @export
#' @importFrom gh gh_token gh
#' @examples
#' @examples
#' branches <- github_branches(owner="RajLabMSSM", repo="echolocatoR")
github_branches <- function(owner = NULL,
repo = NULL,
Expand All @@ -37,36 +37,45 @@ github_branches <- function(owner = NULL,
token = gh::gh_token(),
desc_file = NULL,
error = FALSE,
verbose = TRUE){
verbose = TRUE){
name <- NULL;

out <- infer_owner_repo(owner = owner,
repo = repo,
desc_file = desc_file,
repo = repo,
desc_file = desc_file,
verbose = verbose)
owner <- out$owner
repo <- out$repo
repo <- out$repo
#### Search branches ####
messager("Searching for all branches in:",paste(owner,repo,sep="/"),
v=verbose)
endpoint <- paste(
"https://api.github.com/repos",owner,repo,"branches",
sep="/"
)
gh_response <- gh::gh(endpoint = endpoint,
.token = token,
per_page = 100)
dt <- gh_to_dt(gh_response)
dt <- cbind(owner=owner, repo=repo, dt)
#### Filter branches ####
if(!is.null(branch)){
#### Detect synonymous branches ####
if(isTRUE(master_or_main) &&
any(c("master","main") %in% branch)){
branch <- unique(c("master","main",branch))
}
dt <- dt[name %in% branch,]
page <- 1
repeat {
# Keep iterating pages until we find the branch or run out of pages
gh_response <- gh::gh(endpoint = endpoint,
.token = token,
per_page = 100,
page = page)
if(length(gh_response) == 0) break
dt <- gh_to_dt(gh_response)
dt <- cbind(owner=owner, repo=repo, dt)
#### Filter branches ####
if(!is.null(branch)){
#### Detect synonymous branches ####
if(isTRUE(master_or_main) &&
any(c("master","main") %in% branch)){
branch <- unique(c("master","main",branch))
}
dt <- dt[name %in% branch,]
}
if(nrow(dt)>0) break
page <- page + 1
}

#### Report ####
if(nrow(dt)>0){
messager(paste0(
Expand All @@ -78,9 +87,9 @@ github_branches <- function(owner = NULL,
if(isTRUE(error)) {
stop(stp)
} else {
messager("WARNING:",stp,"Returning NULL.",v=verbose)
messager("WARNING:",stp,"Returning NULL.",v=verbose)
return(NULL)
}
}
}
#### Return ####
if(isTRUE(as_datatable)){
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
<img src='https://github.com/RajLabMSSM/echogithub/raw/master/inst/hex/hex.png' title='Hex sticker for echogithub' height='300'><br>
[![License:
GPL-3](https://img.shields.io/badge/license-GPL--3-blue.svg)](https://cran.r-project.org/web/licenses/GPL-3)
[![](https://img.shields.io/badge/devel%20version-0.99.2-black.svg)](https://github.com/RajLabMSSM/echogithub)
[![](https://img.shields.io/badge/devel%20version-0.99.3-black.svg)](https://github.com/RajLabMSSM/echogithub)
[![](https://img.shields.io/github/languages/code-size/RajLabMSSM/echogithub.svg)](https://github.com/RajLabMSSM/echogithub)
[![](https://img.shields.io/github/last-commit/RajLabMSSM/echogithub.svg)](https://github.com/RajLabMSSM/echogithub/commits/master)
<br> [![R build
status](https://github.com/RajLabMSSM/echogithub/workflows/rworkflows/badge.svg)](https://github.com/RajLabMSSM/echogithub/actions)
[![](https://codecov.io/gh/RajLabMSSM/echogithub/branch/master/graph/badge.svg)](https://codecov.io/gh/RajLabMSSM/echogithub)
[![](https://codecov.io/gh/RajLabMSSM/echogithub/branch/master/graph/badge.svg)](https://app.codecov.io/gh/RajLabMSSM/echogithub)
<br>
<a href='https://app.codecov.io/gh/RajLabMSSM/echogithub/tree/master' target='_blank'><img src='https://codecov.io/gh/RajLabMSSM/echogithub/branch/master/graphs/icicle.svg' title='Codecov icicle graph' width='200' height='50' style='vertical-align: top;'></a>
<h4>
Authors: <i>Brian Schilder, Jack Humphrey, Towfique Raj</i>
Authors: <i>Brian Schilder, Jack Humphrey, Towfique Raj, Hiranyamaya
Dash</i>
</h4>
<h5>
README updated: <i>Mar-10-2023</i>
README updated: <i>Dec-03-2024</i>
</h5>

## `echogithub`: Extract data and metadata from GitHub.
Expand Down
6 changes: 6 additions & 0 deletions man/description_extract.Rd

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

10 changes: 5 additions & 5 deletions man/github_branches.Rd

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

14 changes: 7 additions & 7 deletions tests/testthat/test-github_files.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
test_that("github_files works", {
test_that("github_files works", {

# files <- github_files(owner = "RajLabMSSM",
# repo = "Fine_Mapping_Shiny",
# query = ".md$",
# download = TRUE)
# testthat::expect_true(methods::is(files, "data.table"))
# testthat::expect_true(nrow(files)>=1)
files <- github_files(owner = "RajLabMSSM",
repo = "Fine_Mapping_Shiny",
query = ".md$",
download = TRUE)
testthat::expect_true(methods::is(files, "data.table"))
testthat::expect_true(nrow(files)>=1)
})
Loading