11# ' GitHub branches
2- # '
2+ # '
33# ' List all branches for a given GitHub repository.
4- # ' @param owner Owner of the GitHub repository.
4+ # ' @param owner Owner of the GitHub repository.
55# ' If \code{NULL}, will automatically try to infer the owner
66# ' name from the \emph{DESCRIPTION file}
77# ' (assuming you're working directory is a local R package repo).
8- # ' @param repo GitHub repository name.
8+ # ' @param repo GitHub repository name.
99# ' If \code{NULL}, will automatically try to infer the repo name
1010# ' name from the \emph{DESCRIPTION file}
1111# ' (assuming you're working directory is a local R package repo).
12- # ' @param branch [Optional] If \code{branch} is supplied
13- # ' (as a character vector of one or more branch names),
12+ # ' @param branch [Optional] If \code{branch} is supplied
13+ # ' (as a character vector of one or more branch names),
1414# ' will check to see if that branch exists. If it does, only that branch will
1515# ' be returned. If it doesn't, an error will be thrown.
1616# ' @param master_or_main If \code{branch} is supplied and
1717# ' is either \code{"master"} or \code{"main"},
18- # ' automatically interpret "master" and "main" as synonymous and return
19- # ' whichever branch exists.
18+ # ' automatically interpret "master" and "main" as synonymous and return
19+ # ' whichever branch exists.
2020# ' @param as_datatable Return the results as a \link[data.table]{data.table}
2121# ' (\code{TRUE}), or as a character vector of branch names
2222# ' (default: \code{FALSE}).
2323# ' @param error Throw an error when no matching branches are fond.
2424# ' @inheritParams github_files
2525# ' @inheritParams description_extract
2626# ' @returns Character vector or \link[data.table]{data.table} of branches.
27- # '
27+ # '
2828# ' @export
2929# ' @importFrom gh gh_token gh
30- # ' @examples
30+ # ' @examples
3131# ' branches <- github_branches(owner="RajLabMSSM", repo="echolocatoR")
3232github_branches <- function (owner = NULL ,
3333 repo = NULL ,
@@ -37,36 +37,45 @@ github_branches <- function(owner = NULL,
3737 token = gh :: gh_token(),
3838 desc_file = NULL ,
3939 error = FALSE ,
40- verbose = TRUE ){
40+ verbose = TRUE ){
4141 name <- NULL ;
42-
42+
4343 out <- infer_owner_repo(owner = owner ,
44- repo = repo ,
45- desc_file = desc_file ,
44+ repo = repo ,
45+ desc_file = desc_file ,
4646 verbose = verbose )
4747 owner <- out $ owner
48- repo <- out $ repo
48+ repo <- out $ repo
4949 # ### Search branches ####
5050 messager(" Searching for all branches in:" ,paste(owner ,repo ,sep = " /" ),
5151 v = verbose )
5252 endpoint <- paste(
5353 " https://api.github.com/repos" ,owner ,repo ," branches" ,
5454 sep = " /"
5555 )
56- gh_response <- gh :: gh(endpoint = endpoint ,
57- .token = token ,
58- per_page = 100 )
59- dt <- gh_to_dt(gh_response )
60- dt <- cbind(owner = owner , repo = repo , dt )
61- # ### Filter branches ####
62- if (! is.null(branch )){
63- # ### Detect synonymous branches ####
64- if (isTRUE(master_or_main ) &&
65- any(c(" master" ," main" ) %in% branch )){
66- branch <- unique(c(" master" ," main" ,branch ))
67- }
68- dt <- dt [name %in% branch ,]
56+ page <- 1
57+ repeat {
58+ # Keep iterating pages until we find the branch or run out of pages
59+ gh_response <- gh :: gh(endpoint = endpoint ,
60+ .token = token ,
61+ per_page = 100 ,
62+ page = page )
63+ if (length(gh_response ) == 0 ) break
64+ dt <- gh_to_dt(gh_response )
65+ dt <- cbind(owner = owner , repo = repo , dt )
66+ # ### Filter branches ####
67+ if (! is.null(branch )){
68+ # ### Detect synonymous branches ####
69+ if (isTRUE(master_or_main ) &&
70+ any(c(" master" ," main" ) %in% branch )){
71+ branch <- unique(c(" master" ," main" ,branch ))
72+ }
73+ dt <- dt [name %in% branch ,]
74+ }
75+ if (nrow(dt )> 0 ) break
76+ page <- page + 1
6977 }
78+
7079 # ### Report ####
7180 if (nrow(dt )> 0 ){
7281 messager(paste0(
@@ -78,9 +87,9 @@ github_branches <- function(owner = NULL,
7887 if (isTRUE(error )) {
7988 stop(stp )
8089 } else {
81- messager(" WARNING:" ,stp ," Returning NULL." ,v = verbose )
90+ messager(" WARNING:" ,stp ," Returning NULL." ,v = verbose )
8291 return (NULL )
83- }
92+ }
8493 }
8594 # ### Return ####
8695 if (isTRUE(as_datatable )){
0 commit comments