Skip to content

Commit 47a6db3

Browse files
committed
Add new functions. Fix tests/examples
1 parent f620a22 commit 47a6db3

26 files changed

+472
-22
lines changed

DESCRIPTION

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: echogithub
22
Type: Package
33
Title: echoverse module: Extract data and metadata from GitHub
4-
Version: 0.99.0
4+
Version: 0.99.1
55
Authors@R:
66
c(person(given = "Brian",
77
family = "Schilder",
@@ -45,8 +45,9 @@ Suggests:
4545
covr,
4646
badger,
4747
hexSticker,
48-
httr
49-
RoxygenNote: 7.2.1
48+
httr,
49+
rvest
50+
RoxygenNote: 7.2.2
5051
VignetteBuilder: knitr
5152
License: GPL-3
5253
Config/testthat/edition: 3

NAMESPACE

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,25 @@
33
export(description_extract)
44
export(description_find)
55
export(github_branches)
6+
export(github_commits)
7+
export(github_dependents)
68
export(github_files)
79
export(github_files_download)
810
export(github_hex)
11+
export(github_metadata)
912
export(github_pages)
1013
export(github_pages_files)
1114
export(github_pages_vignettes)
15+
export(github_permissions)
16+
export(github_traffic)
1217
export(readme_header)
1318
importFrom(data.table,":=")
1419
importFrom(data.table,.SD)
20+
importFrom(data.table,as.data.table)
1521
importFrom(data.table,data.table)
1622
importFrom(data.table,fwrite)
1723
importFrom(data.table,rbindlist)
24+
importFrom(data.table,setnafill)
1825
importFrom(gh,gh)
1926
importFrom(gh,gh_token)
2027
importFrom(methods,is)

NEWS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# echogithub 0.99.1
2+
3+
## New features
4+
5+
* New functions:
6+
- `github_permissions`
7+
- `github_metadata`
8+
- `github_commits`
9+
- `github_traffic`
10+
111
# echogithub 0.99.0
212

313
## New features

R/description_find.R

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
#' @importFrom methods is
1414
#' @importFrom utils installed.packages packageDescription
1515
#' @importFrom testthat is_testing
16-
#' @examples
17-
#' \dontrun{
18-
#' desc_file <- description_find(repo="data.table")
19-
#' }
16+
#' @examples
17+
#' desc_file <- description_find(repo="data.table")
2018
description_find <- function(desc_file = NULL,
2119
owner = NULL,
2220
repo = NULL,

R/github_commits.R

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#' GitHub commits
2+
#'
3+
#' Get metadata for all commits to a given GitHub repo.
4+
#' @inheritParams github_files
5+
#' @return A nested list of metadata
6+
#'
7+
#' @export
8+
#' @importFrom gh gh gh_token
9+
#' @examples
10+
#' commits <- github_commits(owner="RajLabMSSM",
11+
#' repo="echolocatoR")
12+
github_commits <- function(owner,
13+
repo,
14+
token = gh::gh_token(),
15+
verbose = TRUE) {
16+
# echoverseTemplate:::source_all()
17+
# echoverseTemplate:::args2vars(github_commits)
18+
19+
endpoint <- paste("https://api.github.com","repos",
20+
owner,repo,"commits",sep="/")
21+
gh_response <- gh::gh(endpoint = endpoint,
22+
.token = token,
23+
.limit = Inf,
24+
per_page = 100)
25+
dt <- gh_to_dt(gh_response)
26+
return(dt)
27+
}

R/github_dependents.R

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#' GitHub dependents
2+
#'
3+
#' Get all GitHub repositories that are dependent on the target
4+
#' GitHub repository.
5+
#' @inheritParams github_files
6+
#' @return A \link[data.table]{data.table}.
7+
#'
8+
#' @export
9+
#' @importFrom data.table data.table
10+
#' @importFrom stringr str_split
11+
#' @examples
12+
#' dt <- github_dependents(owner = "neurogenomics",
13+
#' repo = "rworkflows")
14+
github_dependents <- function(owner,
15+
repo,
16+
token = gh::gh_token(),
17+
verbose = TRUE) {
18+
# echoverseTemplate:::source_all()
19+
# echoverseTemplate:::args2vars(github_insights)
20+
21+
requireNamespace("rvest")
22+
## NOTE: GitHub API does not currently support getting deps from repo,
23+
## so need to use webscraping instead.
24+
messager("Searching for dependents of:",paste(owner,repo,sep="/"),
25+
v=verbose)
26+
#### Get latest commit ####
27+
URL <- paste("https://github.com",owner,repo,"network/dependents",
28+
sep = "/")
29+
html <- rvest::read_html(URL)
30+
box_rows <- rvest::html_elements(html,".Box-row")
31+
if(length(box_rows)==0){
32+
messager("WARNING: No dependents could be found. Returning NULL.",
33+
v=verbose)
34+
return(NULL)
35+
}
36+
dt <- (rvest::html_text2(box_rows)) |>
37+
stringr::str_split(" / |\n|[ ]", simplify = TRUE) |>
38+
data.table::data.table() |>
39+
`colnames<-`(c("owner","repo","stargazers_count","forks_count"))
40+
dt <- cbind(target=paste(owner,repo,sep="/"),dt)
41+
return(dt)
42+
}

R/github_files.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#' Search for files within a GitHub repository and filter them
44
#' by a regex \code{query}. Optionally, can download these selected files to
55
#' your local computer using the original folder structure.
6-
#' \emph{NOTET}: To search your private repos, make sure you
6+
#' \emph{NOTE}: To search your private repos, make sure you
77
#' GitHub token is supplied to the \code{token} argument, or is saved as in your
88
#' \emph{~.Renviron} file, e.g.: \code{GITHUB_TOKEN=<token_here>}
99
#' @param owner Repo owner name

R/github_metadata.R

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#' GitHub metadata
2+
#'
3+
#' Get metadata on a GitHub repository.
4+
#' @param add_traffic Add traffic metadata
5+
#' with \link[echogithub]{github_traffic}.
6+
#' @inheritParams github_files
7+
#' @return A \link[data.table]{data.table}.
8+
#'
9+
#' @export
10+
#' @importFrom data.table as.data.table
11+
#' @importFrom gh gh gh_token
12+
#' @examples
13+
#' dt <- github_metadata(owner="RajLabMSSM",
14+
#' repo="echolocatoR")
15+
github_metadata <- function(owner,
16+
repo,
17+
add_traffic = FALSE,
18+
token = gh::gh_token(),
19+
verbose = TRUE) {
20+
# echoverseTemplate:::source_all()
21+
# echoverseTemplate:::args2vars(github_metadata)
22+
23+
messager(paste0(
24+
"Gathering metadata",
25+
if(isTRUE(add_traffic))" (with traffic data)" else NULL
26+
),"for repo:",paste(owner,repo,sep="/"),v=verbose)
27+
endpoint <- paste("https://api.github.com","repos",
28+
owner,repo,sep="/")
29+
gh_response <- gh::gh(endpoint = endpoint,
30+
.token = token,
31+
per_page = 100)
32+
dt <- cbind(
33+
owner=owner,
34+
repo=repo,
35+
data.table::as.data.table(gh_response[!sapply(gh_response, is.list)])
36+
)
37+
for(x in names(gh_response[sapply(gh_response, is.list)])){
38+
if(x %in% c("owner","repo")){
39+
x2 <- paste(x,"info",sep="_")
40+
}else {
41+
x2 <- x
42+
}
43+
dt[[x2]] <- list(gh_response[[x]])
44+
}
45+
#### Add traffic metadata ####
46+
if(isTRUE(add_traffic)){
47+
traffic <- github_traffic(owner = owner,
48+
repo = repo,
49+
token = token,
50+
verbose = FALSE)
51+
if(!is.null(traffic)){
52+
dt <- merge(dt,traffic,by=c("owner","repo"))
53+
}
54+
}
55+
return(dt)
56+
}

R/github_pages.R

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
#'
99
#' @export
1010
#' @importFrom gh gh gh_token
11-
#' @examples
12-
#' link <- github_pages(owner="RajLabMSSM", repo="echolocatoR")
11+
#' @examples
12+
#' link <- github_pages(owner="RajLabMSSM",
13+
#' repo="echolocatoR")
1314
github_pages <- function(owner,
1415
repo,
1516
error = TRUE,

R/github_permissions.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#' GitHub permissions
2+
#'
3+
#' Get permissions metadat on a GitHub repository.
4+
#' @inheritParams github_files
5+
#' @return A \link[data.table]{data.table}.
6+
#'
7+
#' @export
8+
#' @importFrom data.table as.data.table
9+
#' @importFrom gh gh gh_token
10+
#' @examples
11+
#' permissions <- github_permissions(owner="RajLabMSSM",
12+
#' repo="echolocatoR")
13+
github_permissions <- function(owner,
14+
repo,
15+
token = gh::gh_token(),
16+
verbose = TRUE) {
17+
18+
messager("Gathering permissions metadata for repo:",
19+
paste(owner,repo,sep="/"),
20+
v=verbose)
21+
dt <- github_metadata(owner = owner,
22+
repo = repo,
23+
token = token,
24+
verbose = verbose)
25+
return(dt$permissions[[1]])
26+
}

0 commit comments

Comments
 (0)