lockedata / starters

R Package 📦 for initializing projects for various R activities :nut_and_bolt:
https://itsalocke.com/starters/
GNU General Public License v3.0
124 stars 16 forks source link

Make repo_exists more specific #114

Open maelle opened 5 years ago

maelle commented 5 years ago

Currently, repo_exists will return FALSE if there's no repo with the same name for the account... but also if there's something wrong with the API.

repo_exists <- function(username, repo) {
  !inherits(
    try(gh::gh("GET /repos/:owner/:repo",
      owner = username,
      repo = repo
    ),
    silent = TRUE
    ),
    "try-error"
  )
}
JaiPizGon commented 4 years ago

Not really sure if this make what you want, I'm relatively new to R and I'm not sure what inherits and the "{username}" and "{repo}" to pass arguments to functions really do. I try to maintain the format of the function, however at the bottom of the comment is what I would write in my package.

Solution 1 trying to maintain the function in the same format as yours:

#' Determine whether the repo already exists
#' @param username account name
#' @param repo repo name
#' @details very much inspired by available::available_on_github()
#' @noRd
repo_exists <- function(username, repo) {
  check_repo <- function(username, repo) {
    # Check repository in the API where all the packages are listed
    res <- jsonlite::fromJSON("http://rpkg-api.gepuro.net/rpkg?q={username}/{name}")
    # Check exactly that the repository exists on github
    any(res$url == "https://github.com/{username}/{name}.git")
  }
  !inherits(
    try(check_repo(
      username = username,
      repo = repo
    ),
    silent = TRUE
    ),
    "try-error"
  )
}

Solution 2 as I would do it:

#' Determine whether the repo already exists
#' @param username account name
#' @param repo repo name
#' @details very much inspired by available::available_on_github()
#' @noRd
repo_exists <- function(username, repo) {
  # Check repository in the API where all the packages are listed
  res <- jsonlite::fromJSON(paste0("http://rpkg-api.gepuro.net/rpkg?q=",username,"/",repo))
  # Check exactly that the repository exists on github
  any(res$url == "https://github.com/{username}/{name}.git")
}

Hope it helps.

EDIT 1 && 2: sorry, first comment on github and didn't know how to highlight code EDIT 3: Function can be changed easily to give information about if only the username or only the package exists and give an error message saying if the user should change only his/her username or only the name of his/her package