Skip to content

Commit

Permalink
add event to create issue from within app
Browse files Browse the repository at this point in the history
- gh::gh uses Renviron GHPAT
  • Loading branch information
LiNk-NY committed Aug 30, 2023
1 parent a46284a commit da8a20e
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 12 deletions.
65 changes: 53 additions & 12 deletions R/BiocWorkshopSubmit.R
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Code adapted from
# https://deanattali.com/2015/06/14/mimicking-google-form-shiny/

.TEMPLATE_FORM <- paste(
.TEMPLATE_FORM <- c(
'/request', 'id="{{id}}"', 'title="{{title}}"',
'description="{{description}}"', 'section="{{section}}"',
'startfile="{{startfile}}"', 'source="{{ghrepo}}"',
'docker="{{url}}:{{tag}}"'
)

.workshop_template <- function(.data) {
whisker::whisker.render(.TEMPLATE_FORM, data = .data)
.ISSUE_GH_REPO <- "Bioconductor/workshop-contributions"

.workshop_template <- function(.data, sep = "\n") {
whisker::whisker.render(
paste(.TEMPLATE_FORM, collapse = sep),
data = .data
)
}

mandatory <- function(label) {
Expand All @@ -23,6 +28,9 @@ appCSS <- paste(
".mandatory_star { color: red; }", "#error { color: red; }", sep = "\n"
)

.ISSUE_BTN_CSS <-
"color: #fff; background-color; #337ab7; border-color: #2e6da4"

#' Prepare a Bioconductor Workshop Submission
#'
#' @import shiny
Expand Down Expand Up @@ -98,12 +106,23 @@ BiocWorkshopSubmit <- function(...) {
placeholder = "ghcr.io/username/repo"
),
textInput("tag", "Container Tag", value = "latest"),
actionButton("submit", "Render", class = "btn-primary")
actionButton("render", "Render", class = "btn-primary")
),
hidden(
div(
id = "render_msg",
h3("Review the GitHub issue comment on the right"),
actionButton(
"post", "Create Issue", icon("paper-plane"),
style = .ISSUE_BTN_CSS,
class = "btn-danger"
)
)
),
hidden(
div(
id = "thankyou_msg",
h3("Your response was submitted successfully!")
h3("Submitted successfully!")
)
),
hidden(
Expand Down Expand Up @@ -149,8 +168,8 @@ BiocWorkshopSubmit <- function(...) {
logical(1)
)
mandatoryFilled <- all(mandatoryFilled)
# enable/disable the submit button
toggleState(id = "submit", condition = mandatoryFilled)
# enable/disable the render button
toggleState(id = "render", condition = mandatoryFilled)
})
formData <- reactive({
data <- vapply(fieldsAll, function(x) input[[x]], character(1L))
Expand All @@ -166,29 +185,51 @@ BiocWorkshopSubmit <- function(...) {
height = "380px", fontSize = 18, mode = "r"
)
})
observeEvent(input$submit, {
observeEvent(input$render, {
tryCatch({
fdata <- formData()
gh_comment <- .workshop_template(.data = fdata)
updateAceEditor(
session,
"code",
value = .workshop_template(.data = fdata)
value = gh_comment
)
reset("form")
# reset("form")
hide("form")
hide("error")
show("thankyou_msg")
show("render_msg")
hide("presubmit")
},
error = function(e) {
html("error_msg", e$message)
show(id = "error", anim = TRUE, animType = "fade")
},
finally = {
enable("submit")
enable("render")
hide("submit_msg")
}
)
})
observeEvent(input$post, {
tryCatch({
fdata <- formData()
gh_comment <- .workshop_template(.data = fdata, sep = " ")
ghrepo <- .ISSUE_GH_REPO
issue_title <- paste0(
"[", fdata[["section"]], "] ", fdata[["title"]]
)
create_gh_issue(
ghrepo = ghrepo,
title = issue_title,
body = gh_comment
)
hide("render_msg")
show("thankyou_msg")
}, error = function(e) {
html("error_msg", e$message)
show(id = "error", anim = TRUE, animType = "fade")
})
})
}

shinyApp(ui, server, ...)
Expand Down
12 changes: 12 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ read_gh_file <- function(ghrepo) {
description <- gsub("\n", " ", description)
head(strsplit(description, "\\.")[[1L]], 1L)
}

create_gh_issue <- function(ghrepo, title, body) {
ghrepo <- unlist(strsplit(ghrepo, "/", fixed = TRUE))
names(ghrepo) <- c("owner", "repo")
gh(
"POST /repos/{owner}/{repo}/issues",
owner = ghrepo["owner"],
repo = ghrepo["repo"],
title = jsonlite::unbox(title),
body = jsonlite::unbox(body)
)
}

0 comments on commit da8a20e

Please sign in to comment.