Skip to content

Instantly share code, notes, and snippets.

@jrnold
Last active April 16, 2017 01:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrnold/600197bc4851b3b90759e078a2fe53be to your computer and use it in GitHub Desktop.
Save jrnold/600197bc4851b3b90759e078a2fe53be to your computer and use it in GitHub Desktop.
CSSS 564 git stuff
remote_download <- function(url, local_path = NULL, quiet = FALSE, branch = NULL,
credentials = NULL) {
local_path <- local_path %||% tools::file_path_sans_ext(basename(url))
if (!quiet) {
message("Downloading git repo ", url, " to ", local_path)
}
git2r::clone(url, local_path, credentials = credentials, progress = FALSE)
if (!is.null(x$branch)) {
r <- git2r::repository(bundle)
git2r::checkout(r, branch)
}
r
}
ORGANIZATION <- "UW-CSSS-564"
# Download an assignment
download_assignment <- function(repo, user, local_path = NULL, quiet = FALSE, credentials = NULL) {
url <- sprintf("https://github.com/%s/%s.git", user, repo)
r <- remote_download(url, local_path = local_path, quiet = quiet, credentials = credentials)
upstream <- sprintf("https://github.com/%s/%s.git", ORANIZATION, repo)
git2r::remote_add(r, "upstream", upstream)
git2r::fetch(r, "upstream")
r
}
library("purrr")
# teams
org_teams <- gh("GET /orgs/:org/teams", org = "UW-CSSS-564")
student_team_id <- keep(org_teams, function(x) x$name == "Students Spring 2017")[[1]]$id
students <- gh("GET /teams/:id/members", id = as.character(student_team_id))
members <- gh("GET /orgs/:org/members", org = "UW-CSSS-564")
# fots
forks <- gh("GET /repos/:owner/:repo/forks", owner = "UW-CSSS-564",
repo = "CSSS5642017-assignment-1")
# create pull request
forks <- gh("POST /repos/:owner/:repo/pulls",
owner = ORGANIZATION,
repo = "CSSS5642017-assignment-1",
# user's version which has been altered
head = "jrnold:master",
# branch to merge it to
base = "master",
title = "Assignment 1 Submission",
body = "Submission")
library("git2r")
clone_repo <- function(url, local_path = NULL, ...) {
if (is.null(local_path)) local_path <- basename(gsub("\\.git$", "", localpath))
clone(url, local_path, ...)
}
add_upstream <- function(url = NULL, remote = "upstream", repo = getwd()) {
repo <- git2r::repository(repo)
if (is.null(url)) {
origin <- git2r::remote_url(repo, "origin")
org <- "UW-CSSS-564"
url <- gsub("(github.com[/:]).*?/", paste0("\\1", org), origin)
}
git2r::remote_add(repo, name = "upstream", url = url)
}
pull_upstream <- function(repo = getwd()) {
repo <- git2r::repository(repo)
git2r::fetch(repo, "upstream")
git2r::sync(repo, "upstream/master")
}
library("gh")
library("git2r")
library("purrr")
current_repo <- function(path = getwd()) {
git2r::repository(git2r::discover_repository("."))
}
# Is user assigned to a pull request
is_assignee <- function(pr, user) {
user %in% map_chr(pr$assignees, "login")
}
# Get pull requests to which the user is assigned
get_assigned_pull_requests <- function(repo, user) {
pr_path <- sprintf("GET /repos/UW-CSSS-564/%s/pulls", repo)
keep(gh(pr_path), is_assignee, user = user)
}
# Get pull requests to which the user is assigned
add_remote_pull_request <- function(pr, repo) {
url <- pr$head$repo$clone_url
username <- pr$head$repo$owner$login
remote_add(repo, username, url)
fetch(repo, username)
}
parse_github_url <- function(url) {
pattern <- "github\\.com[:/]([^/]*)/([^./]*)"
parsed <- str_match(url, pattern)
list(owner = parsed[ , 2], name = parsed[ , 3])
}
# Add all pull requests to which user is assigned as remotes
add_remote_pull_requests <- function(repo = NULL, user = NULL) {
repo <- repo %||% current_repo()
user <- user %||% default_signature(repo)@name
origin <- remote_url(repo, "origin")
gh_repo <- parse_github_url(origin)
pull_reqs <- get_assigned_pull_requests(gh_repo$name, user)
map(pull_reqs, add_remote_pull_request, repo = repo)
}
# merge pull requests
add_remote_pull_requests()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment