GitHub Integration with R
Problem
A Publisher wants to create an all-in-one status dashboard for their project hosted on Posit Connect, allowing users to see tasks assigned to them in GitHub right from the Connect dashboard. The Publisher wants to limit the display to issues assigned to the logged-in user.
Solution
Using Posit Connect’s GitHub Integration the Publisher is able to create a Shiny app that displays issues assigned to the logged-in user.
app.R
library(shiny)
library(bslib)
library(gh)
library(httr2)
library(connectapi)
<- page_sidebar(
ui title = "My GitHub Issues",
sidebar = sidebar(
title = "GitHub Repo",
textInput("gh_repo", "GitHub Repository", value = "octocat/Hello-World", placeholder = "octocat/Hello-World"),
),layout_columns(
card(
card_header("assigned"),
verbatimTextOutput("assigned")
),card(
card_header("issues"),
verbatimTextOutput("issues")
),card(
card_header("prs"),
verbatimTextOutput("prs")
),card(
card_header("whoami"),
verbatimTextOutput("whoami")
),
)
)
<- function(input, output, session) {
server
# check if running on Posit Connect
if (Sys.getenv("RSTUDIO_PRODUCT") == "CONNECT") {
# initialize Connect API client
<- connect()
client # read the user-session-token header
<- session$request$HTTP_POSIT_CONNECT_USER_SESSION_TOKEN
user_session_token # grab the OAuth Integration access token using the session token
<- get_oauth_credentials(client, user_session_token)
credentials <- credentials$access_token
token else {
} # grab the access token from the GITHUB_TOKEN env var if running locally
<- Sys.getenv("GITHUB_TOKEN")
token
}
$assigned <- renderText ({
output<- gh(
assigned "GET /issues",
.token = token
)paste(assigned)
})
$issues <- renderText({
outputreq(input$gh_repo)
<- gh(
my_issues "GET /repos/{gh_repo}/issues",
gh_repo = input$gh_repo,
.token = token
)paste(my_issues)
})
$prs <- renderText({
outputreq(input$gh_repo)
<- gh(
my_pulls "GET /repos/{gh_repo}/pulls",
gh_repo = input$gh_repo,
.token = token
)paste(my_pulls)
})
$whoami <- renderText({
outputgh_whoami(
.token = token
|>
) c(x$name, x$login)}() |>
{\(x) paste(x, collapse = "\n")}()
{\(x)
})
}
shinyApp(ui, server)
Running the app locally
Terminal
export CONNECT_SERVER=<connect-host>
export CONNECT_API_KEY=<connect-api-key>
# GITHUB_TOKEN is only required when running the example locally
GITHUB_TOKEN="<github-token>" R -e "shiny::runApp()"