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

API List 2 DF: Part 1 - Google Analytics #16

Merged
merged 10 commits into from
Nov 21, 2023
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
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ Imports:
gh,
dplyr,
lubridate,
purrr
purrr,
tidyr
Encoding: UTF-8
RoxygenNote: 7.2.3
LazyData: true
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(all_ga_metrics)
export(auth_from_secret)
export(authorize)
export(calendly_get)
export(clean_metric_data)
export(clean_repo_metrics)
export(delete_creds)
export(get_calendly_user)
Expand All @@ -26,6 +27,8 @@ importFrom(assertthat,is.string)
importFrom(dplyr,"%>%")
importFrom(dplyr,bind_rows)
importFrom(dplyr,distinct)
importFrom(dplyr,mutate_all)
importFrom(dplyr,mutate_at)
importFrom(gh,gh)
importFrom(httr,accept_json)
importFrom(httr,config)
Expand All @@ -36,6 +39,7 @@ importFrom(httr,oauth_endpoints)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,today)
importFrom(purrr,map)
importFrom(tidyr,separate)
importFrom(utils,browseURL)
importFrom(utils,installed.packages)
importFrom(utils,menu)
1 change: 0 additions & 1 deletion R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ delete_creds <- function(app_name = "all") {
#' }
#'
auth_from_secret <- function(app_name, token, access_token, refresh_token, cache = FALSE) {

if (app_name %in% c("github", "calendly") && is.null(token)) {
stop("For GitHub and Calendly, token cannot be NULL")
}
Expand Down
3 changes: 0 additions & 3 deletions R/calendly.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ calendly_get <- function(url, token = NULL, user = NULL, count = NULL, page_toke
#' get_calendly_user()
#' }
get_calendly_user <- function(token = NULL) {

if (is.null(token)) {
# Get auth token
token <- get_token(app_name = "calendly")
Expand Down Expand Up @@ -84,11 +83,9 @@ get_calendly_user <- function(token = NULL) {
#' authorize("calendly")
#' user <- get_calendly_user()
#' list_calendly_events(user = user$resource$uri)
#'
#' }
#'
list_calendly_events <- function(token = NULL, user, count = 100) {

if (is.null(token)) {
# Get auth token
token <- get_token(app_name = "calendly")
Expand Down
60 changes: 29 additions & 31 deletions R/github.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ get_github <- function(token = NULL, url) {
token <- get_token(app_name = "github")
}

# Github api get
# Github api get
result <- httr::GET(
url,
httr::add_headers(Authorization = paste0("Bearer ", token)),
Expand Down Expand Up @@ -68,7 +68,6 @@ get_github_user <- function(token = NULL) {
#' }
#'
get_repo_list <- function(owner, count = "all", token = NULL) {

if (count == "all") count <- "Inf"

if (is.null(token)) {
Expand All @@ -78,9 +77,9 @@ get_repo_list <- function(owner, count = "all", token = NULL) {
}

repo_list <- gh::gh("GET /orgs/{owner}/repos",
owner = owner,
.token = token,
.limit = count
owner = owner,
.token = token,
.limit = count
)

return(repo_list)
Expand All @@ -102,7 +101,6 @@ get_repo_list <- function(owner, count = "all", token = NULL) {
#' metrics <- get_github_metrics(repo = "fhdsl/metricminer")
#' }
get_github_metrics <- function(repo, token = NULL, count = "all", data_format = "dataframe") {

if (count == "all") count <- Inf

# Split it up
Expand All @@ -121,13 +119,14 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format =
)
# Put gh_repo_wrapper inside function
gh_repo_wrapper_fn <- function(api_call) {
gh_repo_wrapper(api_call = api_call,
owner = owner,
repo = repo,
token = token,
count = count,
data_format = data_format
)
gh_repo_wrapper(
api_call = api_call,
owner = owner,
repo = repo,
token = token,
count = count,
data_format = data_format
)
}
# Run gh_repo_wrapper_fn() on api_calls
# when error occurs, set value to "Not Found"
Expand Down Expand Up @@ -165,11 +164,9 @@ get_github_metrics <- function(repo, token = NULL, count = "all", data_format =
#'
#' repo_names <- c("fhdsl/metricminer", "jhudsl/OTTR_Template")
#' some_repos_metrics <- get_repos_metrics(repo_names = repo_names)
#'
#' }
#'
get_repos_metrics <- function(owner = NULL, repo_names = NULL, token = NULL, data_format = "dataframe") {

if (is.null(token)) {
# Get auth token
token <- get_token(app_name = "github", try = TRUE)
Expand All @@ -182,15 +179,17 @@ get_repos_metrics <- function(owner = NULL, repo_names = NULL, token = NULL, dat
count = "all"
)

# Extra repo names from the repo list
repo_names <- unlist(purrr::map(repo_list, "full_name"))
# Extra repo names from the repo list
repo_names <- unlist(purrr::map(repo_list, "full_name"))
}

# Now run get_github_metrics on all repos
repo_metrics <- lapply(repo_names, function(repo) {
get_github_metrics(token = token,
repo = repo,
data_format = data_format)
get_github_metrics(
token = token,
repo = repo,
data_format = data_format
)
})

# Keep names
Expand All @@ -216,7 +215,6 @@ get_repos_metrics <- function(owner = NULL, repo_names = NULL, token = NULL, dat
#' @export
#'
gh_repo_wrapper <- function(api_call, owner, repo, token = NULL, count = Inf, data_format = "dataframe") {

message(paste("Trying", api_call, "for", repo))

if (is.null(token)) {
Expand All @@ -234,9 +232,9 @@ gh_repo_wrapper <- function(api_call, owner, repo, token = NULL, count = Inf, da
), silent = TRUE)

# Some handlers because not all repos have all stats
if (length(result) == 0) result <- "No results"
if (grepl("404", result[1])) result <- "No results"
if (grepl("Error", result[1])) result <- "No results"
if (length(result) == 0) result <- "No results"
if (grepl("404", result[1])) result <- "No results"
if (grepl("Error", result[1])) result <- "No results"

return(result)
}
Expand All @@ -253,14 +251,14 @@ gh_repo_wrapper <- function(api_call, owner, repo, token = NULL, count = Inf, da
#' @export
#'
clean_repo_metrics <- function(repo_name, repo_metric_list) {

if (repo_metric_list$contributors[1] != "No results") {
contributors <-
lapply(repo_metric_list$contributors, function(contributor) {
data.frame(
contributor = contributor$login,
num_contributors = contributor$contributions)
}) %>%
lapply(repo_metric_list$contributors, function(contributor) {
data.frame(
contributor = contributor$login,
num_contributors = contributor$contributions
)
}) %>%
dplyr::bind_rows() %>%
dplyr::distinct()

Expand All @@ -280,7 +278,7 @@ clean_repo_metrics <- function(repo_name, repo_metric_list) {
metrics <- data.frame(
repo_name,
num_forks = num_forks,
num_contributors = num_contributors,
num_contributors = num_contributors,
total_contributions = total_contributors,
num_stars = length(unlist(purrr::map(repo_metric_list$stars, "login"))),
health_percentage = ifelse(repo_metric_list$community[1] != "No results", as.numeric(repo_metric_list$community$health_percentage), NA),
Expand Down
Loading
Loading