Skip to content

Commit da1a8c2

Browse files
authored
Progress on create_from_github() (#216)
* Progress on create_from_github() Fixes #114. For now. * open_project() already has this logic * Disambiguate `repo` and repo's name; user -> owner
1 parent 8eea8d7 commit da1a8c2

File tree

3 files changed

+91
-20
lines changed

3 files changed

+91
-20
lines changed

R/create.R

Lines changed: 61 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -77,43 +77,92 @@ create_project <- function(path,
7777
invisible(TRUE)
7878
}
7979

80-
#' Create a project from a github repository
80+
#' Create a repo and project from GitHub
81+
#'
82+
#' Creates a new local Git repository from a repository on GitHub. If you have
83+
#' pre-configured a GitHub personal access token (PAT) as described in
84+
#' [gh::gh_whoami()], you will get more sensible default behavior for the `fork`
85+
#' argument. You cannot create a fork without a PAT. Currently only works for
86+
#' public repositories. A future version of this function will likely have an
87+
#' interface closer to [use_github()], i.e. more ability to accept credentials
88+
#' and more control over the Git configuration of the affected remote or local
89+
#' repositories.
90+
#'
91+
#' @seealso [use_course()] for one-time download of all files in a Git repo,
92+
#' without any local or remote Git operations.
8193
#'
8294
#' @inheritParams create_package
83-
#' @param repo Full name of repository: `owner/repo`
84-
#' @param fork Create a fork before cloning? Defaults to `TRUE` if you can't
85-
#' push to `repo`, `FALSE` if you can.
95+
#' @param repo GitHub repo specification in this form: `owner/reponame`. The
96+
#' second part will be the name of the new local repo.
97+
#' @inheritParams use_course
98+
#' @param fork Create and clone a fork? Or clone `repo` itself? Defaults to
99+
#' `TRUE` if you can't push to `repo`, `FALSE` if you can.
100+
#' @param rstudio Initiate an [RStudio
101+
#' Project](https://support.rstudio.com/hc/en-us/articles/200526207-Using-Projects)?
102+
#' Defaults to `TRUE` if in an RStudio session and project has no
103+
#' pre-existing `.Rproj` file. Defaults to `FALSE` otherwise.
86104
#' @export
87-
create_from_github <- function(repo, path, fork = NA, open = TRUE) {
105+
#' @examples
106+
#' \dontrun{
107+
#' create_from_github("r-lib/usethis")
108+
#' }
109+
create_from_github <- function(repo,
110+
destdir = NULL,
111+
fork = NA,
112+
rstudio = NULL,
113+
open = interactive()) {
114+
destdir <- destdir %||% conspicuous_place()
115+
check_is_dir(destdir)
116+
88117
repo <- strsplit(repo, "/")[[1]]
89118
if (length(repo) != 2) {
90-
stop("`repo` must be of form user/reponame", call. = FALSE)
119+
stop(
120+
code("repo"), " must be of form ",
121+
value("owner/reponame"), call. = FALSE
122+
)
91123
}
92124
owner <- repo[[1]]
93125
repo <- repo[[2]]
94126
repo_info <- gh::gh("GET /repos/:owner/:repo", owner = owner, repo = repo)
95127

96-
check_not_nested(dirname(path), repo)
128+
check_not_nested(destdir, repo)
97129

98-
# By default, fork only if you can't push to the repo
99130
if (is.na(fork)) {
100-
fork <- !repo_info$permissions$push
131+
perms <- repo_info$permissions
132+
if (is.null(perms)) {
133+
# if permissions are absent, there's no PAT and we can't fork
134+
fork <- FALSE
135+
} else {
136+
# fork only if can't push to the repo
137+
fork <- !isTRUE(perms$push)
138+
}
101139
}
140+
## TODO(jennybc): should we also be checking if fork = TRUE but user owns the
141+
## repo?
102142

103143
if (fork) {
104144
done("Forking repo")
105-
fork_info <- gh::gh("POST /repos/:owner/:repo/forks", owner = owner, repo = repo)
145+
fork_info <- gh::gh(
146+
"POST /repos/:owner/:repo/forks",
147+
owner = owner, repo = repo
148+
)
106149
owner <- fork_info$owner$login
107150
git_url <- fork_info$git_url
108151
} else {
109152
git_url <- repo_info$git_url
110153
}
111154

112-
done("Cloning repo")
113-
repo_path <- create_directory(path, repo)
155+
repo_path <- create_directory(destdir, repo)
156+
done("Cloning repo from ", value(git_url), " into ", value(repo_path))
114157
git2r::clone(git_url, normalizePath(repo_path), progress = FALSE)
115158
proj_set(repo_path)
116159

160+
rstudio <- rstudio %||% rstudioapi::isAvailable()
161+
rstudio <- rstudio && !is_rstudio_project(repo_path)
162+
if (rstudio) {
163+
use_rstudio()
164+
}
165+
117166
if (open) {
118167
open_project(repo_path, repo)
119168
}

R/proj.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ proj_crit <- function() {
44
rprojroot::has_file(".here") |
55
rprojroot::is_rstudio_project |
66
rprojroot::is_r_package |
7+
rprojroot::is_git_root |
78
rprojroot::is_remake_project |
89
rprojroot::is_projectile_project
910
}

man/create_from_github.Rd

Lines changed: 29 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)