From 2363a6abaf5681482866b11dee99a0d214826c83 Mon Sep 17 00:00:00 2001 From: hsonne Date: Thu, 2 May 2024 13:58:54 +0200 Subject: [PATCH] Provide "shortcuts" to kwb.utils' functions and use new function get_github_token() --- NAMESPACE | 2 ++ R/create_pkg_codemeta.R | 12 ++++++------ R/create_universe_pkgs_json.R | 2 +- R/download_github.R | 6 +++--- R/get_gh_ratelimit.R | 8 ++++---- R/get_github_packages.R | 4 ++-- R/get_pkg_dependencies.R | 2 +- R/github_commits.R | 15 ++++++++------- R/github_package_versions.R | 14 +++++++------- R/plot_commits_github.R | 3 ++- R/utils.R | 20 ++++++++++++++++++++ R/write_github_repos_json.R | 8 ++++---- R/write_pkg_codemeta_json.R | 2 +- man/create_universe_pkgs_json.Rd | 4 ++-- man/download_github.Rd | 6 +++--- man/get_gh_ratelimit.Rd | 6 +++--- man/get_github_commits.Rd | 4 ++-- man/get_github_commits_repos.Rd | 7 ++++--- man/get_github_packages.Rd | 4 ++-- man/get_github_repos.Rd | 4 ++-- man/github_package_versions.Rd | 4 ++-- man/github_packages_versions.Rd | 4 ++-- man/plot_commits_github.Rd | 3 ++- 23 files changed, 85 insertions(+), 59 deletions(-) create mode 100644 R/utils.R diff --git a/NAMESPACE b/NAMESPACE index 3fb7a32..2f37473 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -50,7 +50,9 @@ importFrom(kwb.pkgstatus,get_github_repos) importFrom(kwb.pkgstatus,get_non_r_packages) importFrom(kwb.utils,catAndRun) importFrom(kwb.utils,multiSubstitute) +importFrom(kwb.utils,selectColumns) importFrom(kwb.utils,selectElements) +importFrom(kwb.utils,stringList) importFrom(lubridate,as_datetime) importFrom(magrittr,"%>%") importFrom(plotly,ggplotly) diff --git a/R/create_pkg_codemeta.R b/R/create_pkg_codemeta.R index d02251b..7b028d0 100644 --- a/R/create_pkg_codemeta.R +++ b/R/create_pkg_codemeta.R @@ -15,14 +15,14 @@ create_pkg_codemeta <- function( dbg = TRUE ) { - cat_and_run <- function(msg, expr) { - kwb.utils::catAndRun(msg, expr, dbg = dbg, newLine = 3L) + run <- function(msg, expr) { + cat_and_run(msg, expr, dbg = dbg, newLine = 3L) } # Get package names from input data frame - packages <- kwb.utils::selectColumns(pkgs, "name") + packages <- select_columns(pkgs, "name") - cat_and_run("Creating codemeta object", { + run("Creating codemeta object", { withr::with_libpaths(libpath, { @@ -37,12 +37,12 @@ create_pkg_codemeta <- function( n, ifelse(n > 1L, "packages are", "package is"), libpath, - kwb.utils::stringList(sort(packages[!is_installed])) + string_list(sort(packages[!is_installed])) )) } lapply(packages[is_installed], function(package) { - cat_and_run( + run( sprintf("Writing codemeta for R package %s", package), try(codemetar::create_codemeta(file.path(libpath, package))) ) diff --git a/R/create_universe_pkgs_json.R b/R/create_universe_pkgs_json.R index 55b980c..a82b386 100644 --- a/R/create_universe_pkgs_json.R +++ b/R/create_universe_pkgs_json.R @@ -16,7 +16,7 @@ create_universe_pkgs_json <- function( group = "KWB-R", ignore_pkgs = NULL, non_r_packages = kwb.pkgstatus::get_non_r_packages(), - github_token = Sys.getenv("GITHUB_PAT") + github_token = get_github_token() ) { get_github_packages( diff --git a/R/download_github.R b/R/download_github.R index e173966..04fe600 100644 --- a/R/download_github.R +++ b/R/download_github.R @@ -9,8 +9,8 @@ #' @param use_zip should files be downloades as .tar.gz (use_zip=FALSE) or as #' .zip files (use_zip=TRUE); default: FALSE #' @param quiet print messages (default: TRUE) -#' @param auth_token auth_token (needed to download "private" repos), default: -#' Sys.getenv("GITHUB_PAT") +#' @param auth_token auth_token (needed to download "private" repos). +#' Default: pkgmeta:::get_github_token() #' @return downloaded repo in dest. folder #' @export #' @import remotes @@ -22,7 +22,7 @@ download_github <- function( dest_dir = tempdir(), use_zip = FALSE, quiet = FALSE, - auth_token = Sys.getenv("GITHUB_PAT") + auth_token = get_github_token() ) { repo_sep <- as.vector( diff --git a/R/get_gh_ratelimit.R b/R/get_gh_ratelimit.R index d14bab4..4cc509e 100644 --- a/R/get_gh_ratelimit.R +++ b/R/get_gh_ratelimit.R @@ -1,7 +1,7 @@ #' Get GitHub rate limit #' -#' @param github_token token passed to \link[gh]{gh} (default: -#' Sys.getenv("GITHUB_PAT") +#' @param github_token token passed to \link[gh]{gh}. +#' Default: pkgmeta:::get_github_token() #' @return overview of rate limit #' @export #' @importFrom dplyr bind_rows @@ -9,10 +9,10 @@ #' @importFrom kwb.utils selectElements #' @examples #' get_gh_ratelimit() -get_gh_ratelimit <- function(github_token = Sys.getenv("GITHUB_PAT")) +get_gh_ratelimit <- function(github_token = get_github_token()) { "https://api.github.com/rate_limit" %>% gh::gh(.token = github_token) %>% - kwb.utils::selectElements("resources") %>% + select_elements("resources") %>% dplyr::bind_rows(.id = "id") } diff --git a/R/get_github_packages.R b/R/get_github_packages.R index 0a14ec4..7c11b5c 100644 --- a/R/get_github_packages.R +++ b/R/get_github_packages.R @@ -6,7 +6,7 @@ #' @param non_r_packages vector with repos that are not R packages (default: #' kwb.pkgstatus::get_non_r_packages()) #' @param github_token optionally a Github token in order to access private -#' repositories (default: Sys.getenv("GITHUB_PAT")) +#' repositories. Default: pkgmeta:::get_github_token() #' @return data frame with R packages on Github #' @export #' @importFrom kwb.pkgstatus get_github_repos get_non_r_packages @@ -20,7 +20,7 @@ get_github_packages <- function( group = "KWB-R", ignore_pkgs = NULL, non_r_packages = kwb.pkgstatus::get_non_r_packages(), - github_token = Sys.getenv("GITHUB_PAT") + github_token = get_github_token() ) { repos <- kwb.pkgstatus::get_github_repos(group, github_token) diff --git a/R/get_pkg_dependencies.R b/R/get_pkg_dependencies.R index de35d1e..b04b841 100644 --- a/R/get_pkg_dependencies.R +++ b/R/get_pkg_dependencies.R @@ -86,7 +86,7 @@ get_pkg_dependencies_impl <- function( pkgs_installed %>% lapply(function(pkg) { - kwb.utils::catAndRun( + cat_and_run( sprintf( "Getting %s dependencies for '%s'", ifelse(recursive, "recursive", "non-recursive"), diff --git a/R/github_commits.R b/R/github_commits.R index 29d2d31..e7d26fb 100644 --- a/R/github_commits.R +++ b/R/github_commits.R @@ -2,7 +2,7 @@ #' Get Github Commits for One Repo #' #' @param repo full name of Github repo ("owner/repo_name", e.g. "kwb-r/kwb.utils") -#' @param github_token default: Sys.getenv("GITHUB_PAT") +#' @param github_token Default: pkgmeta:::get_github_token() #' #' @return data frame #' @export @@ -17,7 +17,7 @@ #'} get_github_commits <- function( repo, - github_token = Sys.getenv("GITHUB_PAT") + github_token = get_github_token() ) { get_commits <- function(repo, per_page = 100L) { @@ -77,7 +77,7 @@ get_github_commits <- function( #' #' @param repos vector with full names of Github repos ("owner/repo_name", #' e.g. c("kwb-r/kwb.utils", "kwb-r/kwb.ml", "kwb-r/aquanes.report")) -#' @param github_token default: Sys.getenv("GITHUB_PAT") +#' @param github_token Default: pkgmeta:::get_github_token() #' @return data frame for all repos with releases #' @export #' @importFrom kwb.utils catAndRun multiSubstitute @@ -85,7 +85,8 @@ get_github_commits <- function( #' @importFrom tidyr separate #' @examples #' \dontrun{ -#' #repos <- kwb.pkgstatus::get_github_repos(github_token = Sys.getenv("GITHUB_PAT")) +#' #token <- Sys.getenv("GITHUB_PAT") +#' #repos <- kwb.pkgstatus::get_github_repos(github_token = token) #' #repos <- repos$full_name #' repos <- paste0("kwb-r/", c("aquanes.report", "kwb.ml", "kwb.utils")) #' pkgs_commits <- pkgmeta::get_github_commits_repos(repos) @@ -93,11 +94,11 @@ get_github_commits <- function( #' } get_github_commits_repos <- function( repos, - github_token = Sys.getenv("GITHUB_PAT") + github_token = get_github_token() ) { pkg_commit_list <- lapply(repos, function(repo) { - kwb.utils::catAndRun( + cat_and_run( sprintf("Repo: %s", repo), try(get_github_commits(repo, github_token = github_token)) ) @@ -115,7 +116,7 @@ get_github_commits_repos <- function( ) ) %>% dplyr::mutate( - author_login = kwb.utils::multiSubstitute(.data$author_login, list( + author_login = multi_substitute(.data$author_login, list( "Andreas Matzinger" = "amatzi", "Hauke Sonnenberg" = "hsonne", "Mathias Riechel" = "mriech", diff --git a/R/github_package_versions.R b/R/github_package_versions.R index 4c9dc12..8828b8b 100644 --- a/R/github_package_versions.R +++ b/R/github_package_versions.R @@ -2,7 +2,7 @@ #' Get Github Versions for One Repo #' #' @param repo full name of Github repo ("owner/repo_name", e.g. "kwb-r/kwb.utils") -#' @param github_token default: Sys.getenv("GITHUB_PAT") +#' @param github_token Default: pkgmeta:::get_github_token() #' #' @return data frame #' @export @@ -17,7 +17,7 @@ #'} github_package_versions <- function( repo, - github_token = Sys.getenv("GITHUB_PAT") + github_token = get_github_token() ) { releases <- "https://api.github.com/repos/%s/releases" %>% @@ -31,8 +31,8 @@ github_package_versions <- function( data.frame( owner = owner_repo[1L], repo = owner_repo[2L], - tag = sapply(releases, kwb.utils::selectElements, "tag_name"), - date = as.Date(sapply(releases, kwb.utils::selectElements, "published_at")), + tag = sapply(releases, select_elements, "tag_name"), + date = as.Date(sapply(releases, select_elements, "published_at")), author_id = purrr::map_chr(purrr::map(releases, "author"), "login") ) } @@ -41,7 +41,7 @@ github_package_versions <- function( #' #' @param repos vector with full names of Github repos ("owner/repo_name", #' e.g. c("kwb-r/kwb.utils", "kwb-r/kwb.ml", "kwb-r/aquanes.report")) -#' @param github_token default: Sys.getenv("GITHUB_PAT") +#' @param github_token Default: pkgmeta:::get_github_token() #' @return data frame for all repos with releases #' @export #' @importFrom kwb.utils catAndRun @@ -54,11 +54,11 @@ github_package_versions <- function( #' } github_packages_versions <- function( repos, - github_token = Sys.getenv("GITHUB_PAT") + github_token = pkgmeta:::get_github_token() ) { versions <- lapply(repos, function(repo) { - kwb.utils::catAndRun( + cat_and_run( sprintf("Repo: %s", repo), expr = try( github_package_versions(repo, github_token = github_token), diff --git a/R/plot_commits_github.R b/R/plot_commits_github.R index afd843b..cd281c5 100644 --- a/R/plot_commits_github.R +++ b/R/plot_commits_github.R @@ -12,7 +12,8 @@ #' @importFrom magrittr %>% #' @examples #' \dontrun{ -#' repos <- kwb.pkgstatus::get_github_repos(github_token = Sys.getenv("GITHUB_PAT") +#' token <- Sys.getenv("GITHUB_PAT") +#' repos <- kwb.pkgstatus::get_github_repos(github_token = token) #' repos_commits <- pkgmeta::github_commits_repos(repos$full_name) #' pkgmeta::plot_commits_github(repos_commits) #' } diff --git a/R/utils.R b/R/utils.R new file mode 100644 index 0000000..5cc0746 --- /dev/null +++ b/R/utils.R @@ -0,0 +1,20 @@ +#' @importFrom kwb.utils catAndRun +cat_and_run <- kwb.utils::catAndRun + +# get_github_token ------------------------------------------------------------- +get_github_token <- function() +{ + Sys.getenv("GITHUB_PAT") +} + +#' @importFrom kwb.utils multiSubstitute +multi_substitute <- kwb.utils::multiSubstitute + +#' @importFrom kwb.utils selectColumns +select_columns <- kwb.utils::selectColumns + +#' @importFrom kwb.utils selectElements +select_elements <- kwb.utils::selectElements + +#' @importFrom kwb.utils stringList +string_list <- kwb.utils::stringList diff --git a/R/write_github_repos_json.R b/R/write_github_repos_json.R index 69eac0a..89be366 100644 --- a/R/write_github_repos_json.R +++ b/R/write_github_repos_json.R @@ -52,7 +52,7 @@ get_github_full_names <- function(github_repos = get_github_repos()) #' Helper function: Get Github Metadata For Repos #' #' @param group organisation (default: "KWB-R") -#' @param github_token default: Sys.getenv("GITHUB_PAT") +#' @param github_token Default: pkgmeta:::get_github_token() #' @param dbg should debug messages be printed? (default: TRUE) #' @param ... additional arguments passed to gh:::gh_build_request(), see #' https://developer.github.com/v3/ (e.g. type = "public") @@ -60,12 +60,12 @@ get_github_full_names <- function(github_repos = get_github_repos()) #' @export get_github_repos <- function ( group = "KWB-R", - github_token = Sys.getenv("GITHUB_PAT"), + github_token = get_github_token(), dbg = TRUE, ... ) { - kwb.utils::catAndRun( + cat_and_run( messageText = sprintf( "\nFetching Github metadata for repos of organisation '%s' at '%s'", group, @@ -104,7 +104,7 @@ write_github_repos_json <- function( repo_html_url <- fetch_sorted(what = "html_url") repo_names <- fetch_sorted(what = "name") - kwb.utils::catAndRun( + cat_and_run( sprintf( "Writting '%s' file for %d repos:\n%s", file, diff --git a/R/write_pkg_codemeta_json.R b/R/write_pkg_codemeta_json.R index 7d6986e..e22f25f 100644 --- a/R/write_pkg_codemeta_json.R +++ b/R/write_pkg_codemeta_json.R @@ -15,7 +15,7 @@ write_pkg_codemeta_json <- function( dbg = TRUE ) { - kwb.utils::catAndRun( + cat_and_run( sprintf("Writting codemeta to '%s'", file), dbg = dbg, expr = jsonlite::write_json( diff --git a/man/create_universe_pkgs_json.Rd b/man/create_universe_pkgs_json.Rd index a4da4db..77cc1cc 100644 --- a/man/create_universe_pkgs_json.Rd +++ b/man/create_universe_pkgs_json.Rd @@ -8,7 +8,7 @@ create_universe_pkgs_json( group = "KWB-R", ignore_pkgs = NULL, non_r_packages = kwb.pkgstatus::get_non_r_packages(), - github_token = Sys.getenv("GITHUB_PAT") + github_token = get_github_token() ) } \arguments{ @@ -22,7 +22,7 @@ metadata on the license!)} kwb.pkgstatus::get_non_r_packages())} \item{github_token}{optionally a Github token in order to access private -repositories (default: Sys.getenv("GITHUB_PAT"))} +repositories. Default: pkgmeta:::get_github_token()} } \value{ JSON structure as required for packages.json diff --git a/man/download_github.Rd b/man/download_github.Rd index 405bfe9..64408d2 100644 --- a/man/download_github.Rd +++ b/man/download_github.Rd @@ -10,7 +10,7 @@ download_github( dest_dir = tempdir(), use_zip = FALSE, quiet = FALSE, - auth_token = Sys.getenv("GITHUB_PAT") + auth_token = get_github_token() ) } \arguments{ @@ -25,8 +25,8 @@ download_github( \item{quiet}{print messages (default: TRUE)} -\item{auth_token}{auth_token (needed to download "private" repos), default: -Sys.getenv("GITHUB_PAT")} +\item{auth_token}{auth_token (needed to download "private" repos). +Default: pkgmeta:::get_github_token()} } \value{ downloaded repo in dest. folder diff --git a/man/get_gh_ratelimit.Rd b/man/get_gh_ratelimit.Rd index 84607c9..defd0e2 100644 --- a/man/get_gh_ratelimit.Rd +++ b/man/get_gh_ratelimit.Rd @@ -4,11 +4,11 @@ \alias{get_gh_ratelimit} \title{Get GitHub rate limit} \usage{ -get_gh_ratelimit(github_token = Sys.getenv("GITHUB_PAT")) +get_gh_ratelimit(github_token = get_github_token()) } \arguments{ -\item{github_token}{token passed to \link[gh]{gh} (default: -Sys.getenv("GITHUB_PAT")} +\item{github_token}{token passed to \link[gh]{gh}. +Default: pkgmeta:::get_github_token()} } \value{ overview of rate limit diff --git a/man/get_github_commits.Rd b/man/get_github_commits.Rd index 6f16544..0978f67 100644 --- a/man/get_github_commits.Rd +++ b/man/get_github_commits.Rd @@ -4,12 +4,12 @@ \alias{get_github_commits} \title{Get Github Commits for One Repo} \usage{ -get_github_commits(repo, github_token = Sys.getenv("GITHUB_PAT")) +get_github_commits(repo, github_token = get_github_token()) } \arguments{ \item{repo}{full name of Github repo ("owner/repo_name", e.g. "kwb-r/kwb.utils")} -\item{github_token}{default: Sys.getenv("GITHUB_PAT")} +\item{github_token}{Default: pkgmeta:::get_github_token()} } \value{ data frame diff --git a/man/get_github_commits_repos.Rd b/man/get_github_commits_repos.Rd index abe216b..e426f2c 100644 --- a/man/get_github_commits_repos.Rd +++ b/man/get_github_commits_repos.Rd @@ -4,13 +4,13 @@ \alias{get_github_commits_repos} \title{Get Github Commits for Multiple Repos} \usage{ -get_github_commits_repos(repos, github_token = Sys.getenv("GITHUB_PAT")) +get_github_commits_repos(repos, github_token = get_github_token()) } \arguments{ \item{repos}{vector with full names of Github repos ("owner/repo_name", e.g. c("kwb-r/kwb.utils", "kwb-r/kwb.ml", "kwb-r/aquanes.report"))} -\item{github_token}{default: Sys.getenv("GITHUB_PAT")} +\item{github_token}{Default: pkgmeta:::get_github_token()} } \value{ data frame for all repos with releases @@ -20,7 +20,8 @@ Get Github Commits for Multiple Repos } \examples{ \dontrun{ -#repos <- kwb.pkgstatus::get_github_repos(github_token = Sys.getenv("GITHUB_PAT")) +#token <- Sys.getenv("GITHUB_PAT") +#repos <- kwb.pkgstatus::get_github_repos(github_token = token) #repos <- repos$full_name repos <- paste0("kwb-r/", c("aquanes.report", "kwb.ml", "kwb.utils")) pkgs_commits <- pkgmeta::get_github_commits_repos(repos) diff --git a/man/get_github_packages.Rd b/man/get_github_packages.Rd index 2be37b9..a369cc4 100644 --- a/man/get_github_packages.Rd +++ b/man/get_github_packages.Rd @@ -8,7 +8,7 @@ get_github_packages( group = "KWB-R", ignore_pkgs = NULL, non_r_packages = kwb.pkgstatus::get_non_r_packages(), - github_token = Sys.getenv("GITHUB_PAT") + github_token = get_github_token() ) } \arguments{ @@ -22,7 +22,7 @@ metadata on the license!)} kwb.pkgstatus::get_non_r_packages())} \item{github_token}{optionally a Github token in order to access private -repositories (default: Sys.getenv("GITHUB_PAT"))} +repositories. Default: pkgmeta:::get_github_token()} } \value{ data frame with R packages on Github diff --git a/man/get_github_repos.Rd b/man/get_github_repos.Rd index 4012e2d..dc4476f 100644 --- a/man/get_github_repos.Rd +++ b/man/get_github_repos.Rd @@ -6,7 +6,7 @@ \usage{ get_github_repos( group = "KWB-R", - github_token = Sys.getenv("GITHUB_PAT"), + github_token = get_github_token(), dbg = TRUE, ... ) @@ -14,7 +14,7 @@ get_github_repos( \arguments{ \item{group}{organisation (default: "KWB-R")} -\item{github_token}{default: Sys.getenv("GITHUB_PAT")} +\item{github_token}{Default: pkgmeta:::get_github_token()} \item{dbg}{should debug messages be printed? (default: TRUE)} diff --git a/man/github_package_versions.Rd b/man/github_package_versions.Rd index ff673ba..4924165 100644 --- a/man/github_package_versions.Rd +++ b/man/github_package_versions.Rd @@ -4,12 +4,12 @@ \alias{github_package_versions} \title{Get Github Versions for One Repo} \usage{ -github_package_versions(repo, github_token = Sys.getenv("GITHUB_PAT")) +github_package_versions(repo, github_token = get_github_token()) } \arguments{ \item{repo}{full name of Github repo ("owner/repo_name", e.g. "kwb-r/kwb.utils")} -\item{github_token}{default: Sys.getenv("GITHUB_PAT")} +\item{github_token}{Default: pkgmeta:::get_github_token()} } \value{ data frame diff --git a/man/github_packages_versions.Rd b/man/github_packages_versions.Rd index e8444bb..6d6ee56 100644 --- a/man/github_packages_versions.Rd +++ b/man/github_packages_versions.Rd @@ -4,13 +4,13 @@ \alias{github_packages_versions} \title{Get Github Versions for Multiple Repos} \usage{ -github_packages_versions(repos, github_token = Sys.getenv("GITHUB_PAT")) +github_packages_versions(repos, github_token = pkgmeta:::get_github_token()) } \arguments{ \item{repos}{vector with full names of Github repos ("owner/repo_name", e.g. c("kwb-r/kwb.utils", "kwb-r/kwb.ml", "kwb-r/aquanes.report"))} -\item{github_token}{default: Sys.getenv("GITHUB_PAT")} +\item{github_token}{Default: pkgmeta:::get_github_token()} } \value{ data frame for all repos with releases diff --git a/man/plot_commits_github.Rd b/man/plot_commits_github.Rd index e89b271..3137a86 100644 --- a/man/plot_commits_github.Rd +++ b/man/plot_commits_github.Rd @@ -18,7 +18,8 @@ Plot Commits GitHub } \examples{ \dontrun{ -repos <- kwb.pkgstatus::get_github_repos(github_token = Sys.getenv("GITHUB_PAT") +token <- Sys.getenv("GITHUB_PAT") +repos <- kwb.pkgstatus::get_github_repos(github_token = token) repos_commits <- pkgmeta::github_commits_repos(repos$full_name) pkgmeta::plot_commits_github(repos_commits) }