From 96f6c9af429d1821e66ad19ccbce9ca36453298b Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 13 Jan 2025 11:38:49 -0500 Subject: [PATCH 1/5] Add find_issue --- R/github_handling.R | 50 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/R/github_handling.R b/R/github_handling.R index 5807060..de25c88 100644 --- a/R/github_handling.R +++ b/R/github_handling.R @@ -253,3 +253,53 @@ 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 = "Broken URL", repo_name = "jhudsl/OTTR_Template") +#' +#' } + +find_issue <- function(text, repo_name, token) { + + if (!is.character(repo)) { + repo <- as.character(repo) + } + + # Github api get + result <- httr::GET( + paste0("https://api.github.com/repos/", repo, "/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()) +} From 329f1cf0a476b058b989051d6420e1acb2324c23 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 13 Jan 2025 12:04:41 -0500 Subject: [PATCH 2/5] Update with find_issue --- R/github_handling.R | 8 +++++++- tests/testthat/test-gh.R | 7 +++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/testthat/test-gh.R diff --git a/R/github_handling.R b/R/github_handling.R index de25c88..d6b3b00 100644 --- a/R/github_handling.R +++ b/R/github_handling.R @@ -273,7 +273,13 @@ check_git_repo <- function(repo_name, #' @examples \dontrun{ #' #' authorize("github") -#' find_issue(text = "Broken URL", repo_name = "jhudsl/OTTR_Template") +#' params <- '{"title":"TEST","body":"Some text"}' +#' +#' gh::gh("/repos/{repo_name}/issues", repo_name = "jhudsl/ottrpal", +#' .token = get_token(app_name = "github"), +#' .params = params) +#' +#' 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..c1b8390 --- /dev/null +++ b/tests/testthat/test-gh.R @@ -0,0 +1,7 @@ +if (Sys.getenv("GH_PAT") != "") { + +test_that("Test issue finder", { + + find_issue(text = "TEST:", repo_name = "jhudsl/ottrpal") +}) +} From 0d3dbc6790c685437a9e326f07f8409145ebf4de Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 13 Jan 2025 12:18:01 -0500 Subject: [PATCH 3/5] Update find_issue --- R/github_handling.R | 22 ++++++++++++---------- tests/testthat/test-gh.R | 6 +++--- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/R/github_handling.R b/R/github_handling.R index d6b3b00..737fbf6 100644 --- a/R/github_handling.R +++ b/R/github_handling.R @@ -273,26 +273,26 @@ check_git_repo <- function(repo_name, #' @examples \dontrun{ #' #' authorize("github") -#' params <- '{"title":"TEST","body":"Some text"}' -#' -#' gh::gh("/repos/{repo_name}/issues", repo_name = "jhudsl/ottrpal", -#' .token = get_token(app_name = "github"), -#' .params = params) #' #' find_issue(text = "TEST", repo_name = "jhudsl/ottrpal") #' #' } -find_issue <- function(text, repo_name, token) { +find_issue <- function(text, repo_name, token = NULL) { - if (!is.character(repo)) { - repo <- as.character(repo) + if (!is.character(repo_name)) { + repo <- as.character(repo_name) + } + # Try to get credentials other way + if (is.null(token)) { + # Get auth token + token <- get_token(app_name = "github") } # Github api get result <- httr::GET( - paste0("https://api.github.com/repos/", repo, "/issues"), - httr::add_headers(Authorization = paste0("Bearer ", token)), + paste0("https://api.github.com/repos/", repo_name, "/issues"), + #httr::add_headers(Authorization = paste0("Bearer ", token)), httr::accept_json() ) @@ -308,4 +308,6 @@ find_issue <- function(text, repo_name, token) { # Print out the result write(issue_exists, stdout()) + + return(result_list) } diff --git a/tests/testthat/test-gh.R b/tests/testthat/test-gh.R index c1b8390..4a7f78c 100644 --- a/tests/testthat/test-gh.R +++ b/tests/testthat/test-gh.R @@ -1,7 +1,7 @@ -if (Sys.getenv("GH_PAT") != "") { test_that("Test issue finder", { - find_issue(text = "TEST:", repo_name = "jhudsl/ottrpal") + issue <- find_issue(text = "TEST:", repo_name = "jhudsl/ottrpal") + + testthat::expect_true(length(issue$id) > 0) }) -} From 198744aa7238985de5b26f60a5ee6c7af1041393 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 13 Jan 2025 12:19:01 -0500 Subject: [PATCH 4/5] Update docs --- NAMESPACE | 1 + man/find_issue.Rd | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 man/find_issue.Rd 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/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") + +} +} From 0cbe7179722547169f850f31a4114f24c443e169 Mon Sep 17 00:00:00 2001 From: Candace Savonen Date: Mon, 13 Jan 2025 12:23:24 -0500 Subject: [PATCH 5/5] No auth --- R/github_handling.R | 5 ----- 1 file changed, 5 deletions(-) diff --git a/R/github_handling.R b/R/github_handling.R index 737fbf6..af3e92c 100644 --- a/R/github_handling.R +++ b/R/github_handling.R @@ -283,11 +283,6 @@ find_issue <- function(text, repo_name, token = NULL) { if (!is.character(repo_name)) { repo <- as.character(repo_name) } - # Try to get credentials other way - if (is.null(token)) { - # Get auth token - token <- get_token(app_name = "github") - } # Github api get result <- httr::GET(