R/github-api.R

Defines functions gh_user_info gh_fork gh_upload gh_pull_rq

Documented in gh_fork gh_pull_rq gh_upload gh_user_info

#' Get user information
#'
#' Retrieve authenticated user information
#'
#' @return Returns a named character vector. The values
#'   of the vector are \code{userid}, \code{name}, and
#'   \code{email}, respectively.
#'
#' @keywords internal
gh_user_info <- function(token) {
  url <- 'https://api.github.com/user'
  rq <- httr::GET(url, httr::verbose(),
            httr::add_headers(`Authorization` =
                          paste0('token ', token)))
  rq <- httr::content(rq)
  userinfo <- c(userid = rq$login,
                name = rq$name,
                email = rq$email)
  return(userinfo)
}

#' Fork 'deeplexR' repository
#' @keywords internal
gh_fork <- function(repo, token) {
  url <- paste0('https://api.github.com/repos/deeplexR/',
                repo, '/forks')
  rq <- httr::POST(url, httr::verbose(), encode = 'json',
                   httr::add_headers(
                     `Authorization` = paste0('token ', token))
                   )
  return(httr::content(rq))
}

#' Upload a file to user's repository
#' @keywords internal
gh_upload <- function(file, owner, repo, path,
                      gh_name, gh_email, token) {
  url <- paste0('https://api.github.com/repos/',
                owner, '/', repo, '/contents/', path)
  rq <- httr::PUT(url, httr::verbose(), encode = 'json',
            httr::add_headers(
              `Authorization` = paste0('token ', token)),
            body = list(
              content = base64enc::base64encode(file),
              committer = list(name = gh_name,
                               email = gh_email),
              message = paste0('File uploaded ',
                               '(by lexicoR:::gh_upload)'))
            )
  return(httr::content(rq))
}

#' Create pull request in 'deeplexR' repository
#' @keywords internal
gh_pull_rq <- function(userid, repo, msg_title, msg_body, token) {
  url <- paste0('https://api.github.com/repos/',
                'deeplexR/', repo, '/pulls')
  rq <- httr::POST(url, httr::verbose(), encode = 'json',
            httr::add_headers(
              `Authorization` = paste0('token ', token)),
            body = list(
              title = msg_title,
              head = paste0(userid, ':master'),
              base = 'master',
              body = msg_body,
              maintainer_can_modify = TRUE)
            )
}
deeplexR/lexicoR documentation built on May 26, 2019, 2:33 a.m.