Skip to content

Commit 9df6bbd

Browse files
authored
chore: make it more robust (#83)
* chore: make it more robust * docs: fix docs * better * ok * ci: cyclocomp * ci: git config * try * fix
1 parent 5832104 commit 9df6bbd

12 files changed

+134
-92
lines changed

.github/workflows/R-CMD-check.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ jobs:
3232
steps:
3333
- uses: actions/checkout@v4
3434

35+
- name: git config
36+
run: |
37+
git config --global user.email "actions@github.com"
38+
git config --global user.name "gh-pages committer"
39+
3540
- uses: r-lib/actions/setup-pandoc@v2
3641

3742
- uses: r-lib/actions/setup-r@v2

.github/workflows/lint-changed-files.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ jobs:
1515
steps:
1616
- uses: actions/checkout@v4
1717

18+
- name: git config
19+
run: |
20+
git config --global user.email "actions@github.com"
21+
git config --global user.name "gh-pages committer"
22+
1823
- uses: r-lib/actions/setup-r@v2
1924

2025
- uses: r-lib/actions/setup-r-dependencies@v2
@@ -23,6 +28,7 @@ jobs:
2328
any::gh
2429
any::lintr
2530
any::purrr
31+
cyclocomp
2632
needs: check
2733

2834
- name: Add lintr options

.github/workflows/test-coverage.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ jobs:
1818
steps:
1919
- uses: actions/checkout@v4
2020

21+
- name: git config
22+
run: |
23+
git config --global user.email "actions@github.com"
24+
git config --global user.name "gh-pages committer"
25+
2126
- uses: r-lib/actions/setup-r@v2
2227
with:
2328
use-public-rspm: true

R/github.R

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ check_global_git <- function() {
3737
}
3838
}
3939

40-
clone_pkg <- function(pkg_repo, pkg_dir) {
41-
fs::dir_create(pkg_dir)
42-
usethis::create_from_github(pkg_repo, destdir = pkg_dir, open = FALSE)
43-
}
44-
4540
get_repo_meta <- function(pkg_repo, full = FALSE) {
4641
meta <- gh::gh(paste0("/repos/", pkg_repo)) # nolint: paste_linter
4742

R/pkgreview.R

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pkgreview_create <- function(pkg_repo, review_parent = ".",
4848
sprintf("%s-%s", meta[["name"]], template)
4949
)
5050

51-
clone_pkg(pkg_repo, pkg_dir = review_parent)
51+
create_from_github(pkg_repo, destdir = review_parent, open = FALSE)
5252

5353
# create project
5454
withr::local_options(list(usethis.quiet = TRUE))
@@ -118,17 +118,14 @@ pkgreview_init <- function(pkg_repo, review_dir = ".",
118118
}
119119

120120
usethis::create_project(review_dir, open = FALSE)
121+
use_onboarding_tmpl(template, destdir = review_dir)
122+
pkgreview_index_rmd(pkg_data, template, destdir = review_dir)
121123

122-
usethis::with_project(review_dir, {
123-
# create templates
124-
use_onboarding_tmpl(template)
125-
pkgreview_index_rmd(pkg_data, template)
126-
switch(
127-
template,
128-
review = pkgreview_readme_md(pkg_data),
129-
editor = pkgreview_request(pkg_data)
130-
)
131-
}, quiet = TRUE)
124+
switch(
125+
template,
126+
review = pkgreview_readme_md(pkg_data, destdir = review_dir),
127+
editor = pkgreview_request(pkg_data, destdir = review_dir)
128+
)
132129

133130
cli::cli_alert_success(
134131
"{template} project {.val {basename(review_dir)}} initialised"
@@ -220,15 +217,37 @@ pkgreview_getdata <- function(pkg_repo, pkg_dir = NULL,
220217
#' @return a list of whoami token metadata
221218
#' @export
222219
try_whoami <- function() {
223-
if (isTRUE(as.logical(Sys.getenv("CI")))) {
224-
return(
225-
list(
226-
name = "Maëlle Salmon",
227-
login = "maelle",
228-
html_url = "https://github.com/maelle"
229-
)
220+
if (on_ci()) { # nolint: object_usage_linter
221+
list(
222+
login = "maelle",
223+
html_url = "https://github.com/maelle"
224+
)
225+
} else {
226+
try(gh::gh_whoami(gh::gh_token()), silent = TRUE)
227+
}
228+
}
229+
230+
create_from_github <- function(pkg_repo, destdir, open) {
231+
if (on_ci()) { # nolint: object_usage_linter
232+
download_url <- sprintf(
233+
"https://github.com/%s/archive/refs/heads/main.zip",
234+
pkg_repo
235+
)
236+
temp_file <- withr::local_tempfile()
237+
curl::curl_download(download_url, temp_file)
238+
239+
temp_dir <- withr::local_tempdir()
240+
utils::unzip(temp_file, exdir = temp_dir)
241+
zip_name <- sprintf("%s-main", fs::path_file(pkg_repo))
242+
243+
fs::dir_copy(fs::path(temp_dir, zip_name), destdir)
244+
file.rename(
245+
file.path(destdir, zip_name),
246+
file.path(destdir, fs::path_file(pkg_repo))
230247
)
248+
gert::git_init(fs::path(destdir, fs::path_file(pkg_repo)))
249+
return(TRUE)
231250
}
232251

233-
try(gh::gh_whoami(gh::gh_token()), silent = TRUE)
252+
usethis::create_from_github(pkg_repo, destdir = destdir, open = open)
234253
}

R/render-templates.R

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,33 @@
1111
#'
1212
#' @param pkg_data package metadata generated by pkgreview_getdata()
1313
#' @param template character string, one of `review` or `editor`.
14+
#' @param destdir where to save the template
1415
#'
1516
#' @export
1617
# @importFrom usethis getFromNamespace check_installed
1718
# @importFrom usethis getFromNamespace render_template
1819
pkgreview_index_rmd <- function(pkg_data,
19-
template = c("review", "editor")) {
20+
template = c("review", "editor"),
21+
destdir) { # nolint: function_argument_linter
2022
template <- match.arg(template)
2123

22-
usethis::use_template(
23-
sprintf("%s-index", template),
24-
"index.Rmd",
25-
data = pkg_data,
26-
ignore = FALSE,
27-
open = FALSE,
28-
package = "pkgreviewr"
29-
)
24+
usethis::with_project(destdir, {
25+
usethis::use_template(
26+
sprintf("%s-index", template),
27+
"index.Rmd",
28+
data = pkg_data,
29+
ignore = FALSE,
30+
open = FALSE,
31+
package = "pkgreviewr"
32+
)
33+
})
3034
invisible(TRUE)
3135
}
3236

3337
#' @export
3438
#' @rdname pkgreview_index_rmd
35-
pkgreview_readme_md <- function(pkg_data) {
39+
pkgreview_readme_md <- function(pkg_data, destdir) {
40+
usethis::local_project(destdir)
3641
usethis::use_template(
3742
"review-README",
3843
"README.md",
@@ -48,43 +53,40 @@ pkgreview_readme_md <- function(pkg_data) {
4853
#' Clone an up to date copy of the specified ropensci software
4954
#' review/editor response template.
5055
#' @param template character string, one of `review` or `editor`.
56+
#' @param destdir where to save the template
5157
#'
52-
#' @return writes a `{template}.md` checklist template file in the project root.
58+
#' @return writes a `{template}.md` checklist template file in `destdir`.
5359
#' @export
5460
#'
5561
#' @examples
5662
#' \dontrun{
5763
#' use_onboarding_tmpl(template = "editor")
5864
#' }
59-
use_onboarding_tmpl <- function(template = c("review", "editor")) {
65+
use_onboarding_tmpl <- function(template = c("review", "editor"), destdir) { # nolint: function_argument_linter
6066
template <- match.arg(template)
6167
tmpl_txt <- gh::gh("/repos/:owner/:repo/contents/:path",
6268
owner = "ropensci",
6369
repo = "dev_guide",
6470
path = sprintf("templates/%s.md", template) # nolint: nonportable_path_litner
6571
)
6672

67-
temp_file <- withr::local_tempfile()
68-
curl::curl_download(tmpl_txt[["download_url"]], temp_file)
69-
tmpl_txt <- paste(brio::read_lines(temp_file), collapse = "\n")
70-
71-
new <- usethis::write_over(
72-
usethis::proj_path(fs::path_ext_set(template, ".md")),
73-
tmpl_txt
73+
curl::curl_download(
74+
tmpl_txt[["download_url"]],
75+
fs::path(destdir, fs::path_ext_set(template, ".md"))
7476
)
75-
invisible(new)
7677
}
7778

7879

7980

8081
#' @export
8182
#' @rdname pkgreview_index_rmd
82-
pkgreview_request <- function(pkg_data) {
83+
pkgreview_request <- function(pkg_data, destdir) {
8384
pkg_data <- c(
8485
pkg_data,
8586
editor = try_whoami()[["name"]]
8687
)
8788

89+
usethis::local_project(destdir)
8890
usethis::use_template(
8991
"request",
9092
"request.Rmd",

R/utils-ci.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# from testthat
2+
on_ci <- function() {
3+
isTRUE(as.logical(Sys.getenv("CI")))
4+
}

man/pkgreview_index_rmd.Rd

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

man/use_onboarding_tmpl.Rd

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

tests/testthat/_snaps/create-pkgreview.md

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,3 @@
1-
# review-proj-created-correctly
2-
3-
Code
4-
fs::dir_ls(review_dir, all = TRUE)
5-
Output
6-
/riem-review/.git
7-
/riem-review/.gitignore
8-
/riem-review/.here
9-
/riem-review/R
10-
/riem-review/README.md
11-
/riem-review/index.Rmd
12-
/riem-review/review.md
13-
14-
---
15-
16-
Code
17-
fs::dir_ls(pkg_dir, all = TRUE)
18-
Output
19-
/riem/.Rbuildignore
20-
/riem/.git
21-
/riem/.gitattributes
22-
/riem/.github
23-
/riem/.gitignore
24-
/riem/.lintr
25-
/riem/DESCRIPTION
26-
/riem/NAMESPACE
27-
/riem/NEWS.md
28-
/riem/R
29-
/riem/README.md
30-
/riem/_pkgdown.yml
31-
/riem/codemeta.json
32-
/riem/cran-comments.md
33-
/riem/man
34-
/riem/pkgdown
35-
/riem/riem.Rproj
36-
/riem/tests
37-
/riem/vignettes
38-
391
# get-pkg_data
402

413
Code
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# review-proj-created-correctly
2+
3+
Code
4+
fs::dir_ls(review_dir, all = TRUE)
5+
Output
6+
/riem-review/.git
7+
/riem-review/.gitignore
8+
/riem-review/.here
9+
/riem-review/R
10+
/riem-review/README.md
11+
/riem-review/index.Rmd
12+
/riem-review/review.md
13+
14+
---
15+
16+
Code
17+
fs::dir_ls(pkg_dir, all = TRUE)
18+
Output
19+
/riem/.Rbuildignore
20+
/riem/.git
21+
/riem/.gitattributes
22+
/riem/.github
23+
/riem/.gitignore
24+
/riem/.lintr
25+
/riem/DESCRIPTION
26+
/riem/NAMESPACE
27+
/riem/NEWS.md
28+
/riem/R
29+
/riem/README.md
30+
/riem/_pkgdown.yml
31+
/riem/codemeta.json
32+
/riem/cran-comments.md
33+
/riem/man
34+
/riem/pkgdown
35+
/riem/riem.Rproj
36+
/riem/tests
37+
/riem/vignettes
38+

tests/testthat/test-create-pkgreview.R

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ test_that("review-proj-created-correctly", {
99

1010
expect_true(fs::dir_exists(fs::path(review_parent, "riem-review")))
1111
expect_snapshot(
12-
fs::dir_ls(review_dir, all =TRUE),
13-
transform = function(x) sub(".*riem-review/", "/riem-review/", x)
12+
fs::dir_ls(review_dir, all = TRUE),
13+
transform = function(x) sub(".*riem-review/", "/riem-review/", x),
14+
variant = .Platform[["OS.type"]]
1415
)
1516
expect_snapshot(
16-
fs::dir_ls(pkg_dir, all =TRUE),
17-
transform = function(x) sub(".*riem/", "/riem/", x)
17+
fs::dir_ls(pkg_dir, all = TRUE),
18+
transform = function(x) sub(".*riem/", "/riem/", x),
19+
variant = .Platform[["OS.type"]]
1820
)
1921
})
2022

0 commit comments

Comments
 (0)