Skip to content

Instantly share code, notes, and snippets.

@zkamvar
Created November 7, 2024 14:19
Show Gist options
  • Save zkamvar/e2728e225d073f27e42a597c626eb60b to your computer and use it in GitHub Desktop.
Save zkamvar/e2728e225d073f27e42a597c626eb60b to your computer and use it in GitHub Desktop.
2024-11-05-update-contributing.R
library(gert)
library(tinkr) # pak::pak("ropensci/tinkr#119")
library(xml2)
here::i_am("2024-11-05-update-contributing.R")
#' Update the CONTRIBUTING.md for a given hubverse package
#'
#' This function will update some of the steps in the CONTRIBUTING to add a link to the
#' PR checklist, update the usethis code example, and clarify that the development version
#' will have "development version" as the header for the NEWS
#'
#' NOTE: this will checkout a new git branch called znk/update-contributing, commit, and
#' push the changes up to the remote. There is one command `git pushb` that is an alias:
#' ```
#' pushb = !"git push --set-upstream $(git remote | head -n 1) $(git symbolic-ref HEAD --short)"
#' ```
#' @param pkg the path to a locally cloned hubverse package
#' @return nothing. this is used for the side effect
update_contributing <- function(pkg) {
cli::cli_alert_info("Processing {.file {pkg}}")
# checkout a new branch
git_fetch(repo = pkg)
if (git_branch_exists("origin/znk/update-contributing", local = FALSE, repo = pkg)) {
message("nothing to be done")
return(NULL)
}
if (!git_branch_exists("znk/update-contributing", local = TRUE, repo = pkg)){
git_branch_checkout("main", repo = pkg)
git_pull(repo = pkg)
git_branch_create("znk/update-contributing", repo = pkg)
}
git_branch_checkout("znk/update-contributing", repo = pkg)
withr::with_dir(pkg, system("git pushb"))
ctb <- yarn$new(file.path(pkg, ".github/CONTRIBUTING.md"))
# Add checklist link ----------------------------------------------------
xpath <- ".//md:item/md:paragraph[md:code[contains(text(), 'pr_init')]]"
txt_xpath <- paste0(xpath, "/md:text")
node <- xml_find_first(ctb$body, txt_xpath, ns = ctb$ns)
txt <- xml_text(node)
txt <- sub("Create", "create", txt)
xml_set_text(node, txt)
ctb$prepend_md("Follow [the pull request checklist](https://hubverse-org.github.io/hubDevs/articles/release-checklists.html#subsequent-pr-checklist) to ", node, space = FALSE)
# fix for dang bug
node <- xml_find_first(ctb$body, paste0(txt_xpath, "[text()=' to']"), ns = ctb$ns)
txt <- xml_text(node)
xml_set_text(node, paste0(txt, " "))
# Update code to match checklist expectations ---------------------------
code <- xml_find_first(ctb$body, xpath, ns = ctb$ns) |>
xml_find_first("./md:code", ns = ctb$ns)
xml2::xml_set_text(code, r"{usethis::pr_init("name/brief-description/issue")}")
# Update the NEWS text
news <- xml_find_first(ctb$body, ".//md:text[contains(text(), 'first header')]", ns = ctb$ns)
ntxt <- xml_text(news)
new <- sub("header", r"{heading---usually labelled "development version"}", ntxt)
xml_set_text(news, new)
ctb$write(ctb$path)
git_add(".github/CONTRIBUTING.md", repo = pkg)
withr::with_dir(pkg, system("git commit -m 'update CONTRIBUTING.md'"))
git_push(repo = pkg)
withr::with_dir(pkg, system("gh pr create --fill"))
}
pkgs <- list.dirs(path = here::here(), recursive = FALSE)
purrr::walk(pkgs, ~tryCatch(update_contributing(.x),
error = function(e) {
cli::cli_alert_info(e$call)
cli::cli_alert_danger(e$message)
})
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment