Skip to content

Commit

Permalink
Merge pull request #10 from fhdsl/cansavvy/github
Browse files Browse the repository at this point in the history
Getting GitHub Metrics
  • Loading branch information
cansavvy authored Nov 21, 2023
2 parents 4ce5028 + 0ff1be3 commit af0bed8
Show file tree
Hide file tree
Showing 20 changed files with 535 additions and 166 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
^_pkgdown\.yml$
^docs$
^pkgdown$
^inst/extdata/cached-secrets/*
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
.secrets/*
.httr-oauth
docs
inst/extdata/cached-secrets/*
9 changes: 8 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@ export(all_ga_metrics)
export(auth_from_secret)
export(authorize)
export(calendly_get)
export(clean_repo_metrics)
export(delete_creds)
export(get_calendly_user)
export(get_ga_metadata)
export(get_ga_properties)
export(get_ga_stats)
export(get_ga_user)
export(get_github)
export(get_github_metrics)
export(get_github_repo)
export(get_github_user)
export(get_google_files)
export(get_repo_list)
export(get_repos_metrics)
export(get_youtube_stats)
export(gh_repo_wrapper)
export(list_calendly_events)
export(request_ga)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.string)
importFrom(dplyr,"%>%")
importFrom(dplyr,bind_rows)
importFrom(dplyr,distinct)
importFrom(gh,gh)
importFrom(httr,accept_json)
importFrom(httr,config)
Expand All @@ -29,6 +35,7 @@ importFrom(httr,oauth_app)
importFrom(httr,oauth_endpoints)
importFrom(jsonlite,fromJSON)
importFrom(lubridate,today)
importFrom(purrr,map)
importFrom(utils,browseURL)
importFrom(utils,installed.packages)
importFrom(utils,menu)
68 changes: 48 additions & 20 deletions R/auth.R
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,13 @@ authorize <- function(app_name = NULL,
# Store api key here
token <- readline(prompt = "Paste token here and press enter: ")

# If they chose to cache it, we'll store it as a global option
if (cache_it == 1) options(calendly_api = token)
options(calendly_api = token)


# If they chose to cache it, we'll store it in rds file format
if (cache_it == 1) {
saveRDS(token, file.path(cache_secrets_folder(), "calendly.RDS"))
}
}

if (app_name == "github") {
Expand All @@ -52,10 +57,13 @@ authorize <- function(app_name = NULL,
message("On the opened page, scroll down and click 'Generate Token'.")

# Store api key here
token <- readline(prompt = "Paste token here and press enter:")
token <- readline(prompt = "Paste token here and press enter: ")

# Check that token
if (!grepl("ghp", token)) stop("This doesn't look like a GitHub Personal Access token. https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens")

# If they chose to cache it, we'll store it as a global option
if (cache_it == 1) options(github_api = token)
# If they chose to cache it, we'll store it in rds file format
if (cache_it == 1) saveRDS(token, file.path(cache_secrets_folder(), "github.RDS"))
}

if (app_name == "google") {
Expand Down Expand Up @@ -87,23 +95,43 @@ delete_creds <- function(app_name = "all") {

if (!(app_name %in% c("all", supported))) stop("That is not a supported app or endpoint")

if (app_name == "all" | app_name == "calendly") {
options(calendly_api = NULL)
remove_token("calendly")
message("Calendly creds deleted from .Rprofile")
}
## Checking for the existence of cached creds
calendly_creds_exist <- !is.null(getOption("calendly_api"))
github_creds_exist <- !is.null(getOption("github_api"))
oauth_file <- list.files(pattern = ".httr-oauth", all.files = TRUE, recursive = TRUE, full.names = TRUE)
google_creds_exist <- length(oauth_file) != 0

if (app_name == "all" | app_name == "github") {
options(github_api = NULL)
remove_token("github")
message("GitHub creds deleted from .Rprofile")
}
# Do any exist?
none_exist <- all(!calendly_creds_exist, !github_creds_exist, !google_creds_exist)

if (none_exist) {
message("No cached creds to delete (from metricminer anyway). Done")
} else {
if (app_name == "all" | app_name == "calendly") {
if (calendly_creds_exist) {
options(calendly_api = NULL)
remove_token("calendly")
remove_cache("calendly")
message("Calendly creds deleted from cache and environment")
}
}

if (app_name == "all" | app_name == "google") {
oauth_file <- list.files(pattern = ".httr-oauth", all.files = TRUE, recursive = TRUE, full.names = TRUE)
file.remove(oauth_file)
remove_token("google")
message("Cached Google .httr-oauth file deleted")
if (app_name == "all" | app_name == "github") {
if (github_creds_exist) {
options(github_api = NULL)
remove_token("github")
remove_cache("github")
message("GitHub creds deleted from cache and environment")
}
}

if (app_name == "all" | app_name == "google") {
if (google_creds_exist) {
file.remove(oauth_file)
remove_token("google")
message("Cached Google .httr-oauth file deleted and token removed from environment")
}
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions R/cran.R
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
# CRAN

# library("ggplot2")
# library("dlstats")

# download_stats <- cran_stats(c("ottrpal", "githubr", "rgoogleclassroom"))

# if (!is.null(download_stats)) {
# print(head(download_stats))
# ggplot(download_stats, aes(end, downloads, group=package, color=package)) +
# geom_line() +
# geom_point() +
# scale_y_log10()
# }
Loading

0 comments on commit af0bed8

Please sign in to comment.