diff --git a/R/post_bug.R b/R/post_bug.R index 8eebc36..c8c5537 100644 --- a/R/post_bug.R +++ b/R/post_bug.R @@ -24,8 +24,7 @@ components <- c("Accuracy", "Add-ons", "Analyses", "Documentation", "Graphics", #' [Webpage](https://bugs.r-project.org/bugzilla/enter_bug.cgi) for manual entry #' @return The ID of the issue posted. post_bug <- function(text, title, component, ..., - version, product, host, key) { - # Provide some checks/questions to the users + version, product, host, key) { # Fill description, version and summary if (missing(component)) { cli::cli_alert("Please, pick a component:") @@ -45,14 +44,18 @@ post_bug <- function(text, title, component, ..., return() } ask_final_confirmation() - url <- paste0(host, "rest/bug") - # bugs <- httr::POST(url, - # description = text, - # product = product, - # component = component, - # summary = title, - # version = version, - # ..., - # headers) - # bugs <- httr::content(bugs) + url <- paste0(host, "rest/bug?") + data <- list( + product = product, + component = component, + version = version, + summary = title, + description = text + ) + bugs <- httr::POST(url, body = data, encode = "json", ..., + add_headers(.headers = c( + "Content-Type"="application/json", + "Ocp-Apim-Subscription-Key"="my_subscrition_key"))) + bugs <- httr::content(bugs) + return(bugs) } diff --git a/tests/fixtures/post_bug_works.yml b/tests/fixtures/post_bug_works.yml new file mode 100644 index 0000000..a16868a --- /dev/null +++ b/tests/fixtures/post_bug_works.yml @@ -0,0 +1,35 @@ +http_interactions: +- request: + method: post + uri: https://bugs.r-project.org/bugzilla/rest/bug + body: + encoding: '' + string: '{"product":"R","component":"Misc","version":"4.0.x","summary":"test!!!"}' + headers: + Accept: application/json, text/xml, application/xml, */* + Content-Type: application/json + response: + status: + status_code: 401 + category: Client error + reason: Unauthorized + message: 'Client error: (401) Unauthorized' + headers: + date: Thu, 15 Jul 2021 17:00:35 GMT + server: gazelle + etag: 8hv3qhJNknTVRt2cjTD2dw + content-length: '157' + content-type: application/json; charset=UTF-8 + access-control-allow-headers: accept, content-type, origin, user-agent, x-requested-with, + x-bugzilla-api-key, x-bugzilla-password, x-bugzilla-token, x-bugzilla-login + access-control-allow-origin: '*' + x-content-type-options: nosniff + x-frame-options: SAMEORIGIN + x-xss-protection: 1; mode=block + body: + encoding: UTF-8 + file: no + string: '{"code":"410","message":"You must log in before using this part of + Bugzilla.","error":true,"documentation":"https://bugzilla.readthedocs.org/en/latest/api/"}' + recorded_at: 2021-07-15 17:00:35 GMT + recorded_with: vcr/1.0.2, webmockr/0.8.0 diff --git a/tests/testthat/_snaps/post_bug.md b/tests/testthat/_snaps/post_bug.md new file mode 100644 index 0000000..854518f --- /dev/null +++ b/tests/testthat/_snaps/post_bug.md @@ -0,0 +1,38 @@ +# post_bug() works [plain] + + Code + expect_type(pg, "list") + +# post_bug() fails [plain] + + Code + expect_error(post_bug("This is the test description", "TEST!!!"), + "Do it and then return please.") + Message + > Please, pick a component: + Output + + 1: Accuracy 2: Add-ons + 3: Analyses 4: Documentation + 5: Graphics 6: I/O + 7: Installation 8: Language + 9: Low-level 10: Mac GUI/Mac specific + 11: Misc 12: Models + 13: S4methods 14: Startup + 15: System-specific 16: Translations + 17: Windows GUI/Window specific 18: Wishlist + + + -- Documentation --------------------------------------------------------------- + Message + > Have you read the documentation recently? + Output + (0 to cancel) + + 1: Yup + 2: Negative + 3: Nope + + Message + v Opening documentation... + diff --git a/tests/testthat/test-post_bug.R b/tests/testthat/test-post_bug.R new file mode 100644 index 0000000..06f3d59 --- /dev/null +++ b/tests/testthat/test-post_bug.R @@ -0,0 +1,17 @@ +cli::test_that_cli(configs = "plain", "post_bug() works", { + skip_if_not(interactive()) + vcr::use_cassette("post_bug_works", { + pg <- post_bug("This is the test description", "TEST!!!") + }) + expect_snapshot( + expect_type(pg, "list") + ) +}) + + +cli::test_that_cli(configs = "plain", "post_bug() fails", { + skip_if_not(interactive()) + expect_snapshot( + expect_error(post_bug("This is the test description", "TEST!!!"), "Do it and then return please.") + ) +})