1
1
# ' GitHub branches
2
- # '
2
+ # '
3
3
# ' List all branches for a given GitHub repository.
4
- # ' @param owner Owner of the GitHub repository.
4
+ # ' @param owner Owner of the GitHub repository.
5
5
# ' If \code{NULL}, will automatically try to infer the owner
6
6
# ' name from the \emph{DESCRIPTION file}
7
7
# ' (assuming you're working directory is a local R package repo).
8
- # ' @param repo GitHub repository name.
8
+ # ' @param repo GitHub repository name.
9
9
# ' If \code{NULL}, will automatically try to infer the repo name
10
10
# ' name from the \emph{DESCRIPTION file}
11
11
# ' (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),
14
14
# ' will check to see if that branch exists. If it does, only that branch will
15
15
# ' be returned. If it doesn't, an error will be thrown.
16
16
# ' @param master_or_main If \code{branch} is supplied and
17
17
# ' 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.
20
20
# ' @param as_datatable Return the results as a \link[data.table]{data.table}
21
21
# ' (\code{TRUE}), or as a character vector of branch names
22
22
# ' (default: \code{FALSE}).
23
23
# ' @param error Throw an error when no matching branches are fond.
24
24
# ' @inheritParams github_files
25
25
# ' @inheritParams description_extract
26
26
# ' @returns Character vector or \link[data.table]{data.table} of branches.
27
- # '
27
+ # '
28
28
# ' @export
29
29
# ' @importFrom gh gh_token gh
30
- # ' @examples
30
+ # ' @examples
31
31
# ' branches <- github_branches(owner="RajLabMSSM", repo="echolocatoR")
32
32
github_branches <- function (owner = NULL ,
33
33
repo = NULL ,
@@ -37,36 +37,45 @@ github_branches <- function(owner = NULL,
37
37
token = gh :: gh_token(),
38
38
desc_file = NULL ,
39
39
error = FALSE ,
40
- verbose = TRUE ){
40
+ verbose = TRUE ){
41
41
name <- NULL ;
42
-
42
+
43
43
out <- infer_owner_repo(owner = owner ,
44
- repo = repo ,
45
- desc_file = desc_file ,
44
+ repo = repo ,
45
+ desc_file = desc_file ,
46
46
verbose = verbose )
47
47
owner <- out $ owner
48
- repo <- out $ repo
48
+ repo <- out $ repo
49
49
# ### Search branches ####
50
50
messager(" Searching for all branches in:" ,paste(owner ,repo ,sep = " /" ),
51
51
v = verbose )
52
52
endpoint <- paste(
53
53
" https://api.github.com/repos" ,owner ,repo ," branches" ,
54
54
sep = " /"
55
55
)
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
69
77
}
78
+
70
79
# ### Report ####
71
80
if (nrow(dt )> 0 ){
72
81
messager(paste0(
@@ -78,9 +87,9 @@ github_branches <- function(owner = NULL,
78
87
if (isTRUE(error )) {
79
88
stop(stp )
80
89
} else {
81
- messager(" WARNING:" ,stp ," Returning NULL." ,v = verbose )
90
+ messager(" WARNING:" ,stp ," Returning NULL." ,v = verbose )
82
91
return (NULL )
83
- }
92
+ }
84
93
}
85
94
# ### Return ####
86
95
if (isTRUE(as_datatable )){
0 commit comments