diff --git a/NAMESPACE b/NAMESPACE index ebfba0f..dd73e6d 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -22,6 +22,7 @@ export(delete_creds) export(extract_meta) export(extract_object_id) export(extract_quiz) +export(find_issue) export(get_chapters) export(get_github) export(get_gs_pptx) diff --git a/R/github_handling.R b/R/github_handling.R index 5807060..af3e92c 100644 --- a/R/github_handling.R +++ b/R/github_handling.R @@ -253,3 +253,56 @@ check_git_repo <- function(repo_name, return(exists) } + + + + +#' Find an issue on GitHub with a particular title +#' +#' Given text and repository name, find if an issue exists. +#' +#' @param text What text to be searched for in the GitHub issues. Can be regex. +#' @param repo_name the name of the repository, e.g. jhudsl/OTTR_Template +#' @param token A personal access token from GitHub. Only necessary if the +#' repository being checked is a private repository. +#' +#' @return A TRUE/FALSE whether or not the issue with this text on this repository exists. +#' +#' @export +#' +#' @examples \dontrun{ +#' +#' authorize("github") +#' +#' find_issue(text = "TEST", repo_name = "jhudsl/ottrpal") +#' +#' } + +find_issue <- function(text, repo_name, token = NULL) { + + if (!is.character(repo_name)) { + repo <- as.character(repo_name) + } + + # Github api get + result <- httr::GET( + paste0("https://api.github.com/repos/", repo_name, "/issues"), + #httr::add_headers(Authorization = paste0("Bearer ", token)), + httr::accept_json() + ) + + if (httr::status_code(result) != 200) { + httr::stop_for_status(result) + } + + # Process and return results + result_content <- httr::content(result, "text") + result_list <- jsonlite::fromJSON(result_content) + + issue_exists <- length(grep(text, result_list$title)) + + # Print out the result + write(issue_exists, stdout()) + + return(result_list) +} diff --git a/man/find_issue.Rd b/man/find_issue.Rd new file mode 100644 index 0000000..a8f8c9a --- /dev/null +++ b/man/find_issue.Rd @@ -0,0 +1,31 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/github_handling.R +\name{find_issue} +\alias{find_issue} +\title{Find an issue on GitHub with a particular title} +\usage{ +find_issue(text, repo_name, token = NULL) +} +\arguments{ +\item{text}{What text to be searched for in the GitHub issues. Can be regex.} + +\item{repo_name}{the name of the repository, e.g. jhudsl/OTTR_Template} + +\item{token}{A personal access token from GitHub. Only necessary if the +repository being checked is a private repository.} +} +\value{ +A TRUE/FALSE whether or not the issue with this text on this repository exists. +} +\description{ +Given text and repository name, find if an issue exists. +} +\examples{ +\dontrun{ + +authorize("github") + +find_issue(text = "TEST", repo_name = "jhudsl/ottrpal") + +} +} diff --git a/tests/testthat/test-gh.R b/tests/testthat/test-gh.R new file mode 100644 index 0000000..4a7f78c --- /dev/null +++ b/tests/testthat/test-gh.R @@ -0,0 +1,7 @@ + +test_that("Test issue finder", { + + issue <- find_issue(text = "TEST:", repo_name = "jhudsl/ottrpal") + + testthat::expect_true(length(issue$id) > 0) +})