Skip to content

feature(use_release_issue()): Add support for CRAN policy diff #2140

@schloerke

Description

@schloerke

I always panic when I see "Have you read the CRAN policies?" because I don't know what has changed since package release. I would like a way to view a diff of the policy changes since my last package release.

I found the repo https://github.com/eddelbuettel/crp which pulls the docs for CRAN policies, specifically the texi/CRAN_policies.texi file.

Using the most recent package release date, I'm proposing we (conditionally) add a release bullet entry of the policy changes since last release.


While instructions in https://www.r-bloggers.com/2019/05/how-to-keep-up-with-cran-policies-and-processes/ would have worked, the R Journal source stopped updating in 2021.


First pass of a method that is partially using internal {usethis} methods:

#' Retrieve TODOs for CRAN Policy changes since previous package publication
#'
#' This function determines the number of days since the latest CRAN release and
#' constructs a URL to view CRAN policy changes on GitHub since that date.
#'
#' @return Character string containing a markdown todo item for reviewing CRAN policy changes. If a previous
#' release can not be determined, it returns `NULL` (implying the user should
#' review all policies manually).
#'
#' @examples
#' \dontrun{
#' todo_item <- todo_cran_policy()
#' todo_item
#' #> [1] "* [ ] Review [CRAN policy changes](https://github.com/eddelbuettel/crp/compare/master%40%7B519day%7D...master#diff-786702b479ac7d4d5230138b5d63fb321de4df8af956e8fe792532acd26b326a) since `GGally`'s previous release"
#'
#' todo_item |> markdown::markdownToHTML(fragment.only = TRUE) |> cat()
#' #> <ul>
#' #> <li><input type="checkbox" /> Review <a href="https://github.com/eddelbuettel/crp/compare/master%40%7B519day%7D...master#diff-786702b479ac7d4d5230138b5d63fb321de4df8af956e8fe792532acd26b326a">CRAN policy changes</a> since <code>GGally</code>’s previous release</li>
#' #> </ul>
#' }
#' @noRd
todo_cran_policy <- function() {
  proj <- usethis:::proj_desc()
  pkg_name <- proj$get_field("Package")

  is_available <- pkg_name %in% rownames(available.packages())
  if (!is_available) {
    # Package is not on CRAN, so the policy check is not applicable.
    return(invisible(NULL))
  }

  tryCatch(
    {
      last_pkg_desc_text <- readLines(glue::glue(
        "https://cran.r-project.org/web/packages/{pkg_name}/DESCRIPTION"
      ))
    },
    error = function(e) {
      # If the DESCRIPTION file cannot be read, then the the CRAN policies may not apply.
      return(invisible(NULL))
    }
  )
  last_pkg_pub_date <- as.Date(desc::desc(
    text = last_pkg_desc_text
  )$get_field("Date/Publication"))

  days_since_publication <- difftime(
    Sys.Date(),
    last_pkg_pub_date,
    units = "days"
  )

  if (is.na(days_since_publication) || days_since_publication == 0) {
    # If the last package publication date has trouble computing or is today,
    # we don't need to check for policy changes.
    return(invisible(NULL))
  }

  # Check for commits in the last `days`
  commits_since <- gh::gh(
    "/repos/eddelbuettel/crp/commits",
    since = format(Sys.Date() - days_since_publication, format = "%Y-%m-%d")
  )

  if (length(commits_since) == 0) {
    # No new policy changes!
    return(invisible(NULL))
  }

  browser_url <- paste0(
    # Ex: https://github.com/eddelbuettel/crp/compare/master%40%7B519day%7D...master#diff-786702b479ac7d4d5230138b5d63fb321de4df8af956e8fe792532acd26b326a
    "https://github.com/eddelbuettel/crp/compare/master%40%7B",
    as.numeric(days_since_publication),
    "day%7D...master#diff-786702b479ac7d4d5230138b5d63fb321de4df8af956e8fe792532acd26b326a"
  )
  usethis:::todo(glue::glue(
    "Review [CRAN policy changes]({browser_url}) since `{pkg_name}`'s previous release"
  ))
}

If this is of interest, I'm happy to make a PR!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions