This repository was archived by the owner on Jun 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgithub.R
79 lines (75 loc) · 2.71 KB
/
github.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# Vars ----
gh_api_url <- 'https://api.github.com'
gh_search_repo_url <- paste0(gh_api_url, '/search/repositories')
api_request <- function(url, warn = TRUE) {
tkn <- Sys.getenv("GITHUB_PAT")
h <- curl::new_handle()
if (nchar(tkn) > 0)
{
# From Oct 2020, GitHub API requires token to be provided via curl header
curl::handle_setheaders(h, "Authorization-token" = tkn)
}
req <- try(curl::curl_fetch_memory(url, handle = h), silent = TRUE)
if (warn && inherits(req, 'try-error') || req$status_code != 200) {
warning("Unable to make URL request for ", char(url))
}
req
}
# Functions ----
#' @name github_repo_search
#' @title Search for repository
#' @description Return GitHub API item for specific repository.
#' @param repo GitHub repo
#' @return data.frame
github_repo_search <- function(repo) {
search_args <- paste0('?q=', repo, '&', 'Type=Repositories')
req <- api_request(paste0(gh_search_repo_url, search_args))
github_res <- jsonlite::fromJSON(rawToChar(req$content))
if (github_res[['total_count']] == 0) {
warning('No ', char(repo), ' found.', call. = FALSE)
return(data.frame())
}
if (github_res[['total_count']] > 1) {
warning('Too many possible matching repos for ', char(repo), '.',
call. = FALSE)
return(data.frame())
}
github_res[['items']]
}
#' @name github_search
#' @title Search for outsider modules in GitHub
#' @description Returns GitHub API item results for outsider module search.
#' @return data.frame
github_search <- function() {
search_args <- paste0('?q=om..+in:name+outsider-module+in:description',
'&', 'Type=Repositories')
req <- api_request(paste0(gh_search_repo_url, search_args))
github_res <- jsonlite::fromJSON(rawToChar(req$content))
if (github_res[['incomplete_results']]) {
warning('Not all repos discovered.')
}
github_res[['items']]
}
#' @name github_tags
#' @title Module tags from GitHub
#' @description Return tbl_df of module tags for a list of outsider
#' modules hosted on GitHub.
#' @param repos Character vector of outsider module repositories.
#' @return tbl_df
github_tags <- function(repos) {
fetch <- function(repo) {
api_url <- paste0(gh_api_url, '/repos/', repo, '/contents/inst/dockerfiles')
req <- api_request(url = api_url, warn = FALSE)
if (!inherits(req, 'try-error') && req$status_code == 200) {
raw_df <- jsonlite::fromJSON(rawToChar(req$content))
tag <- raw_df[ ,'name']
} else {
warning('Unable to fetch data from GitHub for ', char(repo))
download_url <- tag <- ''
}
data.frame(repo = repo, tag = tag, stringsAsFactors = FALSE)
}
res <- lapply(X = repos, FUN = fetch)
res <- do.call(what = rbind, args = res)
tibble::as_tibble(x = res)
}