Skip to content

Commit

Permalink
Merge pull request #15 from matt-dray/md-cli
Browse files Browse the repository at this point in the history
Add {cli}, R-CMD check GitHub Action, allow for generic branch-name removal
  • Loading branch information
matt-dray committed Jun 21, 2023
2 parents 6b117a7 + 88fa1aa commit 3139701
Show file tree
Hide file tree
Showing 14 changed files with 206 additions and 76 deletions.
3 changes: 2 additions & 1 deletion .Rbuildignore
@@ -1,4 +1,5 @@
^allghrepos\.Rproj$
^ghdump\.Rproj$
^\.Rproj\.user$
^LICENSE\.md$
^CODE_OF_CONDUCT\.md$
^\.github$
1 change: 1 addition & 0 deletions .github/.gitignore
@@ -0,0 +1 @@
*.html
49 changes: 49 additions & 0 deletions .github/workflows/R-CMD-check.yaml
@@ -0,0 +1,49 @@
# Workflow derived from https://github.com/r-lib/actions/tree/v2/examples
# Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help
on:
push:
branches: [main, master]
pull_request:
branches: [main, master]

name: R-CMD-check

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}

name: ${{ matrix.config.os }} (${{ matrix.config.r }})

strategy:
fail-fast: false
matrix:
config:
- {os: macos-latest, r: 'release'}
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3

- uses: r-lib/actions/setup-pandoc@v2

- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
http-user-agent: ${{ matrix.config.http-user-agent }}
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck
needs: check

- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
5 changes: 3 additions & 2 deletions DESCRIPTION
@@ -1,6 +1,6 @@
Package: ghdump
Title: Clone Or Download All Repos For A GitHub User
Version: 0.0.0.9006
Version: 0.1.0
Authors@R:
person(given = "Matt",
family = "Dray",
Expand All @@ -15,7 +15,8 @@ License: MIT + file LICENSE
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.0
RoxygenNote: 7.2.3
Imports:
cli,
gh (>= 1.1.0),
purrr (>= 0.3.4)
12 changes: 10 additions & 2 deletions NEWS.md
@@ -1,3 +1,11 @@
# ghdump 0.1.0

* Added {cli} for a prettier user interface and added new messages.
* Fixed bug where providing a protocol borked downloads (#14).
* Attempted to remove branch name from unzipped file folders by simple regex (`-.*$`) rather than to remove the hard-coded '-master'.
* Added R-CMD check GitHub Action.
* Bump v0.1.0.

# ghdump 0.0.0.9006

* Added protocol argument for user to choose HTTPS or SSH (#10)
Expand All @@ -12,9 +20,9 @@
* Introduced cloning functionality for (at least) Mac (#6)
* Updated readme given new functionality
* Separated scripts in `r/` into different files
* Minor improvements to code comments and user prompt text
* Made minor improvements to code comments and user prompt text
* Preferred ?readlines() example for 'y'/'n' user input
* Add license
* Added license

# ghdump 0.0.0.9003

Expand Down
17 changes: 8 additions & 9 deletions R/01-repo-names.R
Expand Up @@ -5,18 +5,16 @@
#' Uses \code{\link[gh]{gh}} to access the GitHub API and get the details for
#' all of a named user's repos. To use this function you need to have created a
#' GitHub account and to have put a GitHub Personal Access Token (PAT) in your
#' .Renviron. See \href{https://happygitwithr.com/github-pat.html}{Happy Git and GitHub for the UseR}
#' .Renviron. See
#' \href{https://happygitwithr.com/github-pat.html}{Happy Git and GitHub for the UseR}
#' for more information.
#'
#' @param gh_user Character string. A GitHub username.
#'
#' @return A gh_response object.
#' @return A gh_response (list) object.
ghd_get_repos <- function(gh_user) {

cat(
"Fetching GitHub repos for user ", gh_user, "... ",
sep = ""
)
cli::cli_alert_info("Fetching GitHub repos for user '{gh_user}'. ")

# Get repos for username
user_repos <-
Expand All @@ -26,6 +24,8 @@ ghd_get_repos <- function(gh_user) {
.limit = Inf # get all repos
)

cli::cli_alert_success("Found {length(user_repos)} repos.")

return(user_repos)

}
Expand All @@ -35,7 +35,8 @@ ghd_get_repos <- function(gh_user) {
#' Extract all the 'name' elements from a gh_response object. These are the
#' names of all the GitHub repos.
#'
#' @param repo_object A gh_response object, as returned by \code{\link{ghd_get_repos}}.
#' @param repo_object A gh_response object, as returned by
#' \code{\link{ghd_get_repos}}.
#'
#' @return A character vector of GitHub repo names.
ghd_extract_names <- function(repo_object) {
Expand All @@ -48,8 +49,6 @@ ghd_extract_names <- function(repo_object) {

repo_names_vec <- unlist(repo_names)

cat(length(repo_names), "repos found\n")

return(repo_names_vec)

}
56 changes: 38 additions & 18 deletions R/02-download-repos.R
Expand Up @@ -40,20 +40,27 @@ ghd_download_zips <- function(repo_urls, dest_dir) {
# Ask if all the repos should be downloaded
q_download_all <- readline(
prompt = paste0(
"Definitely download all ", nrow(repo_urls), " repos? y/n: ")
"Definitely download all ", nrow(repo_urls), " repos? y/n: "
)
)

if (substr(tolower(q_download_all), 1, 1) == "y") {
is_yes <- substr(tolower(q_download_all), 1, 1) == "y"
is_no <- substr(tolower(q_download_all), 1, 1) == "n"

if (is_yes) {

cli::cli_alert_info("Downloading zipped repositories to {dest_dir}")
cli::cli_alert_info("Hit ESC at any time to abort.")

cat("Downloading zipped repositories to", dest_dir, "\n")
} else if (is_no) {

} else if (substr(tolower(q_download_all), 1, 1) == "n") {
cli::cli_abort("Aborted by user choice.")

stop("Aborted by user choice.\n")

} else {

stop("Aborted. Input not understood.\n")
cli::cli_abort("Aborted. Input not understand.")

}

Expand Down Expand Up @@ -99,7 +106,7 @@ ghd_unzip <- function(dir) {
)

# Unzip the files
cat("Unzipping repositories\n")
cli::cli_alert_info("Unzipping repositories.")
purrr::walk(
.x = zip_files,
.f = ~ utils::unzip(zipfile = .x, exdir = dir)
Expand All @@ -110,34 +117,44 @@ ghd_unzip <- function(dir) {
prompt = paste0("Retain the zip files? y/n: ")
)

is_yes <- substr(tolower(q_keep_zip), 1, 1) == "y"
is_no <- substr(tolower(q_keep_zip), 1, 1) == "n"

# React to user input
if (substr(tolower(q_keep_zip), 1, 1) == "y") {
if (is_yes) {

cat("Keeping zipped folders.")
cli::cli_alert_info("Keeping zipped folders.")

} else if (substr(tolower(q_keep_zip), 1, 1) == "n") {
} else if (is_no) {

cat("Removing zipped folders\n")
cli::cli_alert_info("Removing zipped folders.")

purrr::walk(
.x = zip_files,
.f = file.remove
)

cli::cli_alert_success("Removed zipped folders.")

} else {
cat("Input not understood. Keeping zipped folders.\n")

cli::cli_alert_warning("Input not understood. Keeping zipped folders.")

}


# Ask if "-master" suffix of unzipped files should be replaced
q_remove_suffix <- readline(
prompt = paste0(
"Remove '-master' suffix from unzipped directory names? y/n: "
"Remove branch suffix (e.g. '-main') from unzipped directory names? y/n: "
)
)

is_yes <- substr(tolower(q_remove_suffix), 1, 1) == "y"
is_no <- substr(tolower(q_remove_suffix), 1, 1) == "n"

# React to user input
if (substr(tolower(q_remove_suffix), 1, 1) == "y") {
if (is_yes) {

# Get paths of each unzipped file
unzipped_dirs <-
Expand All @@ -151,25 +168,28 @@ ghd_unzip <- function(dir) {
rename_df <-
data.frame(
from = unzipped_dirs,
to = gsub(pattern = "-master", replacement = "", x = unzipped_dirs),
to = gsub(pattern = "-.*$", replacement = "", x = unzipped_dirs),
stringsAsFactors = FALSE
)

# Rename each file to remove "-master"
cat("Renaming files to remove '-master' suffix\n")
cli::cli_alert_info("Renaming files to remove branch suffix.")
purrr::walk2(
.x = rename_df$from,
.y = rename_df$to,
.f = file.rename
)
cli::cli_alert_success("Renamed folders.")

} else if (substr(tolower(q_remove_suffix), 1, 1) == "n") {
} else if (is_no) {

cat("Unzipped repository names unchanged.\n")
cli::cli_alert_info("Unzipped repository names unchanged.")

} else {

cat("Input not understood. Leaving unzipped repository names unchanged.\n")
cli::cli_alert_warning(
"Input not understood. Leaving unzipped repository names unchanged."
)

}

Expand Down
18 changes: 13 additions & 5 deletions R/03-clone-repos.R
Expand Up @@ -20,7 +20,10 @@
ghd_clone_one <- function(gh_user, repo, protocol, dest_dir) {

if (!protocol %in% c("https", "ssh")) {
stop("You must provide either 'https' or 'ssh' to the protocol argument.")

cli::cli_abort(
"You must provide either 'https' or 'ssh' to the protocol argument."
)
}

# Pass a system call to clone the repo to the destination
Expand Down Expand Up @@ -70,10 +73,13 @@ ghd_clone_multi <- function(gh_user, names_vec, protocol, dest_dir) {
prompt = paste0("Definitely clone all ", length(names_vec), " repos? y/n: ")
)

is_yes <- substr(tolower(q_clone_all), 1, 1) == "y"
is_no <- substr(tolower(q_clone_all), 1, 1) == "n"

# React to user input
if (substr(tolower(q_clone_all), 1, 1) == "y") {
if (is_yes) {

cat("Cloning repositories to", dest_dir, "\n")
cli::cli_alert_info("Cloning repositories to {dest_dir}.")

# Prepare safe file clone (passes over failures)
clone_safely <-
Expand All @@ -92,13 +98,15 @@ ghd_clone_multi <- function(gh_user, names_vec, protocol, dest_dir) {
.f = clone_safely
)

} else if (substr(tolower(q_clone_all), 1, 1) == "n") {
} else if (is_no) {

cli::cli_abort("Aborted by user choice.")

stop("Aborted by user choice.\n")

} else {

stop("Aborted. Input not understood.\n")
cli::cli_abort("Aborted. Input not understand.")

}

Expand Down

0 comments on commit 3139701

Please sign in to comment.