Last active
March 14, 2024 14:30
-
-
Save gadenbuie/cb42370be619c54741ad5df2ae406866 to your computer and use it in GitHub Desktop.
usethis pr command line helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env Rscript | |
if (!requireNamespace("rlang", quietly = TRUE)) { | |
stop("`utpr` requires `{usethis}`: install.packages('usethis')") | |
} | |
rlang::check_installed(c("usethis", "cli", "docopt")) | |
' | |
A command-line wrapper for `pr_*` helper functions from {usethis}. | |
<https://usethis.r-lib.org/articles/pr-functions.html> | |
usage: | |
pr init <branch> | |
pr pause | |
pr resume [<branch>] | |
pr fetch [<pr>] | |
pr pull | |
pr merge-main | |
pr forget | |
pr finish [<pr>] | |
pr view [<pr>] | |
options: | |
<branch> A local branch name | |
<pr> A GitHub PR number | |
-h --help Show this screen' -> doc | |
opts <- docopt::docopt(doc) | |
# Pull request wrapper functions --------------------------------------- | |
pr_resume <- function(branch = NULL) { | |
if (is.null(branch)) { | |
pr_list_branches() | |
return(invisible()) | |
} | |
usethis::pr_resume(branch) | |
} | |
pr_pull <- function() { | |
if (on_default_branch()) { | |
gert::git_pull() | |
} else { | |
usethis::pr_pull() | |
} | |
} | |
pr_fetch <- function(pr) { | |
if (is.null(pr)) { | |
pr_list_open_pulls("fetch") | |
return(invisible()) | |
} | |
usethis::pr_fetch(pr) | |
} | |
pr_finish <- function(pr) { | |
if (is.null(pr) && on_default_branch()) { | |
pr_list_closed_pulls("finish") | |
return(invisible()) | |
} | |
usethis::pr_finish(pr) | |
} | |
pr_list_branches <- function() { | |
branches <- gert::git_branch_list(local = TRUE) | |
branches <- branches[rev(order(branches$updated)), ] | |
cli::cli_alert_info("Available branches:") | |
cli::cli_ul(branches$name) | |
} | |
pr_view <- function(pr) { | |
if (is.null(pr) && on_default_branch()) { | |
pr_list_open_pulls("view") | |
return(invisible()) | |
} | |
usethis::pr_view(pr) | |
} | |
# Helper functions --------------------------------------- | |
on_default_branch <- function() { | |
db <- usethis::git_default_branch() | |
cb <- gert::git_info()$shorthand | |
identical(db, cb) | |
} | |
check_has_gh_cli <- function() { | |
if (!nzchar(Sys.which("gh"))) { | |
cli::cli_abort(c( | |
"The {.code gh} cli is not installed.", | |
i = "{.url https://cli.github.com/}" | |
)) | |
} | |
} | |
pr_list_open_pulls <- function(command) { | |
check_has_gh_cli() | |
cli::cli_alert_info("{.field utpr {command}} requires a PR number") | |
cli::cli_h1("Recent open PRs") | |
system2("gh", c("pr", "list", "--limit", "10")) | |
} | |
pr_list_closed_pulls <- function(command) { | |
check_has_gh_cli() | |
cli::cli_alert_info("{.field utpr {command}} requires a PR number") | |
cli::cli_h1("Recent closed PRs") | |
system2("gh", c("pr", "list", "--limit", "10", "--state", "closed")) | |
} | |
# Run the selected command --------------------------------------- | |
commands <- c("init", "pause", "resume", "fetch", "pull", "merge_main", "forget", "finish", "view") | |
which_command <- which(vapply(opts[commands], isTRUE, logical(1))) | |
command <- names(opts[commands])[which_command] | |
switch( | |
command, | |
init = usethis::pr_init(opts$branch), | |
pause = usethis::pr_pause(), | |
resume = pr_resume(opts$branch), | |
fetch = pr_fetch(opts$pr), | |
pull = pr_pull(), | |
merge_main = usethis::pr_merge_main(), | |
forget = usethis::pr_forget(), | |
finish = pr_finish(opts$pr), | |
view = pr_view(opts$pr) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Installation
Download this file and move it into
~/.local/bin
. Then make it executable. Enjoy!You might need to
mkdir ~/.local/bin
first to create the folder. You might also not have~/.local/bin
in your$PATH
. I have this line in my~/.zshrc
:After that, call
utpr
to help your with your PR workflows!Requirements
This script uses
usethis
,cli
anddocopt
and R, which you'll need to have installed.It also uses the gh command-line utility from GitHub to query recently opened or closed PRs.
Read more about the usethis PR worfklow.