Skip to content

Commit 5608067

Browse files
authored
feat: error when username in wrong case (#26)
1 parent 65522b2 commit 5608067

File tree

4 files changed

+74
-15
lines changed

4 files changed

+74
-15
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ Imports:
2626
tibble,
2727
withr,
2828
fs,
29-
rlang
29+
rlang,
30+
cli
3031
URL: https://github.com/ropensci-org/gitcellar
3132
BugReports: https://github.com/ropensci-org/gitcellar/issues
3233
Suggests:

R/organization.R

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@
2828
#' download_organization_repos(c("maelle-test", "maelle-test"))
2929
#' download_organization_repos("maelle-test", keep = "testy2") # only keep the testy2 repo
3030
#' }
31-
download_organization_repos <- function(organizations = NULL,
32-
extra_repos = NULL,
33-
keep = character(0),
34-
dest_folder = getwd()) {
35-
31+
download_organization_repos <- function(
32+
organizations = NULL,
33+
extra_repos = NULL,
34+
keep = character(0),
35+
dest_folder = getwd()
36+
) {
3637
user_types <- check_users(organizations)
3738

3839
if (!dir.exists(dest_folder)) dir.create(dest_folder)
@@ -47,12 +48,20 @@ download_organization_repos <- function(organizations = NULL,
4748
)
4849
}
4950

50-
repos <- purrr::map2(organizations, user_types, launch_org_migrations, keep = keep, extra_repos = extra_repos) |>
51+
repos <- purrr::map2(
52+
organizations,
53+
user_types,
54+
launch_org_migrations,
55+
keep = keep,
56+
extra_repos = extra_repos
57+
) |>
5158
unlist(recursive = FALSE)
5259

5360
repo_names <- purrr::map_chr(repos, function(x) x$name)
5461

55-
message("Waiting one minute for things to kick off properly before downloads...")
62+
message(
63+
"Waiting one minute for things to kick off properly before downloads..."
64+
)
5665
Sys.sleep(60)
5766

5867
start_time <- Sys.time()
@@ -71,11 +80,15 @@ download_organization_repos <- function(organizations = NULL,
7180
ready_repos <- repos[status == "exported"]
7281
purrr::walk(ready_repos, function(repo) repo$launch_download())
7382
repos <- repos[status != "exported"]
74-
if (length(repos) > 0) message(sprintf("Still not saved: %s.", toString(purrr::map_chr(repos, ~.x[["name"]]))))
83+
if (length(repos) > 0)
84+
message(sprintf(
85+
"Still not saved: %s.",
86+
toString(purrr::map_chr(repos, ~ .x[["name"]]))
87+
))
7588
}
7689

7790
if (length(repos) > 0) {
78-
leftover <- toString(purrr::map_chr(repos, ~.x[["name"]]))
91+
leftover <- toString(purrr::map_chr(repos, ~ .x[["name"]]))
7992
message(sprintf("Left-over: %s.", leftover))
8093
} else {
8194
leftover <- NULL
@@ -87,7 +100,12 @@ download_organization_repos <- function(organizations = NULL,
87100
)
88101
}
89102

90-
launch_org_migrations <- function(username, user_type, extra_repos, keep = character(0)) {
103+
launch_org_migrations <- function(
104+
username,
105+
user_type,
106+
extra_repos,
107+
keep = character(0)
108+
) {
91109
repo_names <- if (user_type == "organization") {
92110
gh::gh(
93111
"/orgs/{org}/repos",
@@ -105,7 +123,9 @@ launch_org_migrations <- function(username, user_type, extra_repos, keep = chara
105123
per_page = 100,
106124
.limit = Inf
107125
) |>
108-
purrr::keep(\(x) startsWith(x[["full_name"]], sprintf("%s/", username))) |>
126+
purrr::keep(
127+
\(x) startsWith(x[["full_name"]], sprintf("%s/", username))
128+
) |>
109129
purrr::map_chr("name")
110130
}
111131

@@ -125,12 +145,29 @@ launch_org_migrations <- function(username, user_type, extra_repos, keep = chara
125145
}
126146

127147
message(sprintf("Launching archive creation for username %s", username))
128-
purrr::map(repo_names, repo$new, organization = username, user_type = user_type)
129-
148+
purrr::map(
149+
repo_names,
150+
repo$new,
151+
organization = username,
152+
user_type = user_type
153+
)
130154
}
131155

132-
check_user <- function(username) {
156+
check_user <- function(username, call = rlang::caller_env()) {
133157
info <- gh::gh("/users/{user}", user = username)
158+
159+
# check the case was correct
160+
# https://github.com/ropensci-org/gitcellar/issues/25
161+
if (info[["login"]] != username) {
162+
cli::cli_abort(
163+
c(
164+
x = '{.arg username} {.val {username}} not equal to actual user name {.val {info[["login"]]}}',
165+
i = "Please fix the case"
166+
),
167+
call = call
168+
)
169+
}
170+
134171
tolower(info[["type"]])
135172
}
136173

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# wrong case triggers an error
2+
3+
Code
4+
download_organization_repos("MAELLE")
5+
Condition
6+
Error in `purrr::map_chr()`:
7+
i In index: 1.
8+
Caused by error in `map_()`:
9+
x `username` "MAELLE" not equal to actual user name "maelle"
10+
i Please fix the case
11+

tests/testthat/test-download_organization_repos.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,13 @@ test_that("external repos in `extra_repos` trigger an error", {
66
)
77

88
})
9+
10+
test_that("wrong case triggers an error", {
11+
skip_on_ci()
12+
expect_snapshot(
13+
download_organization_repos("MAELLE"),
14+
error = TRUE
15+
)
16+
17+
})
18+

0 commit comments

Comments
 (0)