Skip to content

Commit

Permalink
Provide "shortcuts" to kwb.utils' functions
Browse files Browse the repository at this point in the history
and use new function get_github_token()
  • Loading branch information
hsonne committed May 2, 2024
1 parent 7c30822 commit 2363a6a
Show file tree
Hide file tree
Showing 23 changed files with 85 additions and 59 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions R/create_pkg_codemeta.R
Original file line number Diff line number Diff line change
Expand Up @@ -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, {

Expand All @@ -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)))
)
Expand Down
2 changes: 1 addition & 1 deletion R/create_universe_pkgs_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 3 additions & 3 deletions R/download_github.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down
8 changes: 4 additions & 4 deletions R/get_gh_ratelimit.R
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#' 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
#' @importFrom gh gh
#' @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")
}
4 changes: 2 additions & 2 deletions R/get_github_packages.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion R/get_pkg_dependencies.R
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down
15 changes: 8 additions & 7 deletions R/github_commits.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down Expand Up @@ -77,27 +77,28 @@ 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
#' @importFrom dplyr bind_rows if_else mutate
#' @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)
#' head(pkgs_commits)
#' }
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))
)
Expand All @@ -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",
Expand Down
14 changes: 7 additions & 7 deletions R/github_package_versions.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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" %>%
Expand All @@ -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")
)
}
Expand All @@ -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
Expand All @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion R/plot_commits_github.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#' }
Expand Down
20 changes: 20 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions R/write_github_repos_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,20 @@ 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")
#' @importFrom gh gh
#' @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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion R/write_pkg_codemeta_json.R
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions man/create_universe_pkgs_json.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/download_github.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions man/get_gh_ratelimit.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/get_github_commits.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2363a6a

Please sign in to comment.