Skip to content

Instantly share code, notes, and snippets.

@jdblischak
Created November 2, 2017 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jdblischak/df0459d1626248d407f51ccf9cb7eb25 to your computer and use it in GitHub Desktop.
Save jdblischak/df0459d1626248d407f51ccf9cb7eb25 to your computer and use it in GitHub Desktop.
Create PR with R
#!/usr/bin/env Rscript
# This example code implements the fork and pull request workflow for GitHub
# directly from R using the packages gh and git2r. gh provides an R interface to
# the GitHub REST API v3, and git2r provides an R interface to libgit2. It
# submits the Pull Request to the practice repository Spoon-Knife.
#
# The following sequence of steps are performed:
#
# authenticate -> fork -> clone -> branch -> edit -> add -> commit -> push ->
# pull request
#
# Executing this script requires some upfront setup. Specifically you need to
# have created SSH keys and registered them with your GitHub account, and you
# need to have generated a personal access token and exported it in your
# .Renviron file as GITHUB_PAT.
#
# License: This code is in the public domain. You can do whatever you like with
# it, but it also comes with no guarantees.
# Links:
#
# GitHub REST API v3: https://developer.github.com/v3/
# gh: https://github.com/r-lib/gh
# git2r: https://github.com/ropensci/git2r
# SSH setup: https://help.github.com/articles/connecting-to-github-with-ssh/
# Token generation: https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/
# .Renviron file: https://csgillespie.github.io/efficientR/set-up.html#renviron
# Spoon-Knife: https://github.com/octocat/Spoon-Knife
# Example result: https://github.com/octocat/Spoon-Knife/pull/14465
library("gh")
library("git2r")
# Authenticate -----------------------------------------------------------------
stopifnot(file.exists("~/.Renviron"), Sys.getenv("GITHUB_PAT") != "")
login <- gh_whoami()
message("You are logged into GitHub as ", login$login)
# Fork -------------------------------------------------------------------------
# https://developer.github.com/v3/repos/forks/#create-a-fork
fork <- gh("POST /repos/:owner/:repo/forks", owner = "octocat", repo = "Spoon-knife")
message("You forked ", fork$parent$full_name, " to ", fork$full_name)
browseURL(fork$html_url)
# Clone ------------------------------------------------------------------------
# Using SSH
r <- clone(fork$ssh_url, local_path = file.path(tempdir(), fork$name))
# Using HTTPS
# pswd <- getPass::getPass()
# r <- clone(fork$clone_url, local_path = file.path(tempdir(), fork$name),
# credentials = cred_user_pass(username = login$login, password = pswd))
message("You cloned ", fork$full_name, " to ", workdir(r))
# Branch -----------------------------------------------------------------------
git_log <- commits(r)
b <- branch_create(commit = git_log[[1]], name = "feature-branch")
checkout(b)
message("You created a new branch '", b@name, "' and checked it out")
# Edit -------------------------------------------------------------------------
fname <- paste0(workdir(r), "pr.txt")
file.create(fname)
cat("This file was created with the R packages gh and git2r\n", file = fname)
message("You created the file ", fname)
# Add and commit ---------------------------------------------------------------
add(r, fname)
message("You added the file ", fname)
commit(r, "Add pr.txt")
message("You committed the file ", fname)
# Push -------------------------------------------------------------------------
# Using SSH
push(r, name = "origin", refspec = paste0("refs/heads/", b@name))
# Using HTTPS
# push(r, name = "origin", refspec = paste0("refs/heads/", b@name),
# credentials = cred_user_pass(username = login$login, password = pswd))
message("You pushed the local branch '", b@name,
"' to the remote repository 'origin' (i.e. ",
remote_url(r, "origin"), ")")
# Pull Request -----------------------------------------------------------------
# https://developer.github.com/v3/pulls/#create-a-pull-request
pr <- gh("POST /repos/:owner/:repo/pulls", owner = "octocat", repo = "Spoon-knife",
title = "Automated pull request via R package gh",
head = paste(login$login, b@name, sep = ":"),
base = "master",
body = sprintf("I created this PR using the R packages [gh](https://github.com/r-lib/gh) and [git2r](https://github.com/ropensci/git2r).\n\nI added a new file called %s.",
basename(fname)),
maintainer_can_modify = TRUE)
message("You submitted a Pull Request to ", fork$parent$full_name)
message(" ", pr$head$label, " to be merged into ", pr$base$label)
message(pr$html_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment