Skip to content

Commit 2836ee2

Browse files
committed
use gh cli to upload
1 parent ab0faa0 commit 2836ee2

File tree

6 files changed

+227
-9
lines changed

6 files changed

+227
-9
lines changed

DESCRIPTION

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: ffopportunity
22
Title: Models for Fantasy Football Expected Points
3-
Version: 0.1.0.07
3+
Version: 0.1.1
44
Authors@R: c(
55
person("Joe", "Sydlowski", , "syd235@gmail.com", role = c("aut", "cre", "cph")),
66
person("Tan", "Ho", , "tan@tanho.ca", role = "aut", comment = c(ORCID = "0000-0001-8388-5155"))
@@ -14,8 +14,9 @@ URL: https://ffopportunity.ffverse.com,
1414
BugReports: https://github.com/ffverse/ffopportunity/issues
1515
Depends:
1616
R (>= 3.6.0)
17-
Imports:
17+
Imports:
1818
cli (>= 3.0.0),
19+
data.table,
1920
dplyr (>= 1.0.0),
2021
glue (>= 1.0.0),
2122
hardhat (>= 0.1.0),
@@ -33,7 +34,7 @@ Imports:
3334
tidyselect (>= 1.0.0),
3435
utils,
3536
xgboost (>= 1.1)
36-
Suggests:
37+
Suggests:
3738
arrow (>= 5.0.0),
3839
covr,
3940
curl,
@@ -44,4 +45,4 @@ Suggests:
4445
Config/testthat/edition: 3
4546
Encoding: UTF-8
4647
Roxygen: list(markdown = TRUE)
47-
RoxygenNote: 7.2.1
48+
RoxygenNote: 7.3.2

NAMESPACE

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ export(ep_preprocess)
1212
export(ep_summarize)
1313
import(recipes)
1414
import(xgboost)
15+
importFrom(data.table,":=")
16+
importFrom(data.table,.BY)
17+
importFrom(data.table,.EACHI)
18+
importFrom(data.table,.GRP)
19+
importFrom(data.table,.I)
20+
importFrom(data.table,.N)
21+
importFrom(data.table,.NGRP)
22+
importFrom(data.table,.SD)
23+
importFrom(data.table,data.table)
24+
importFrom(data.table,setDF)
25+
importFrom(data.table,setDT)
1526
importFrom(magrittr,"%>%")
1627
importFrom(rlang,.data)
1728
importFrom(rlang,.env)

R/ffopportunity-package.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#' @keywords internal
2+
"_PACKAGE"
3+
4+
## usethis namespace: start
5+
#' @importFrom data.table :=
6+
#' @importFrom data.table .BY
7+
#' @importFrom data.table .EACHI
8+
#' @importFrom data.table .GRP
9+
#' @importFrom data.table .I
10+
#' @importFrom data.table .N
11+
#' @importFrom data.table .NGRP
12+
#' @importFrom data.table .SD
13+
#' @importFrom data.table data.table
14+
#' @importFrom data.table setDT
15+
#' @importFrom data.table setDF
16+
## usethis namespace: end
17+
NULL

R/utils_gh_cli.R

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
# See cli manual at https://cli.github.com/manual/
2+
3+
# This functions tries the gh command in a terminal. If it errors, the gh cli
4+
# isn't available on the machine or at least not on the PATH variable
5+
gh_cli_available <- function() {
6+
gh_test <- try(system("gh", intern = TRUE), silent = TRUE)
7+
8+
if (inherits(gh_test, "try-error")) {
9+
cli::cli_abort(
10+
"The Github Command Line Interface is not available on your machine! \\
11+
Please visit {.url https://github.com/cli/cli#installation} \\
12+
for install instructions."
13+
)
14+
}
15+
16+
invisible(TRUE)
17+
}
18+
19+
gh_cli_release_upload <- function(
20+
files,
21+
tag,
22+
...,
23+
repo = "ffverse/ffopportunity",
24+
overwrite = TRUE
25+
) {
26+
# see https://cli.github.com/manual/gh_release_upload
27+
28+
# validate file paths
29+
file_available <- file.exists(files)
30+
31+
# if files are missing, warn the user and update the files vector to keep
32+
# valid file paths only. If there are no valid file paths, exit the function.
33+
if (!all(file_available)) {
34+
cli::cli_alert_warning(
35+
"The following file{?s} {?is/are} missing: {.path {files[!file_available]}}"
36+
)
37+
38+
if (all(file_available == FALSE)) {
39+
cli::cli_alert_warning("There's nothing left to upload. Exiting!")
40+
return(invisible(FALSE))
41+
}
42+
}
43+
# keep valid file paths
44+
files <- files[file_available]
45+
46+
# Make sure the gh cli is available
47+
gh_cli_available()
48+
49+
# create command for the shell
50+
cli_command <- paste(
51+
"gh release upload",
52+
tag,
53+
paste(files, collapse = " "),
54+
"-R",
55+
repo,
56+
if (isTRUE(overwrite)) "--clobber" else ""
57+
)
58+
59+
cli::cli_alert_info(
60+
"Start upload of {cli::no(length(files))} file{?s} to \\
61+
{.url {paste0('https://github.com/', repo, '/releases')}} \\
62+
@ {.field {tag}}",
63+
wrap = TRUE
64+
)
65+
66+
cli_output <- .invoke_cli_command(cli_command = cli_command)
67+
68+
cli::cli_alert_success("Upload successfully completed.")
69+
70+
invisible(TRUE)
71+
}
72+
73+
gh_cli_release_tags <- function(repo = "ffverse/ffopportunity") {
74+
# see https://cli.github.com/manual/gh_release_list
75+
76+
# Make sure the gh cli is available
77+
gh_cli_available()
78+
79+
# create command for the shell
80+
cli_command <- paste(
81+
"gh release list",
82+
"-R",
83+
repo,
84+
"--json tagName"
85+
)
86+
87+
cli_output <- .invoke_cli_command(cli_command = cli_command)
88+
89+
.cli_parse_json(cli_output = cli_output)[["tagName"]]
90+
}
91+
92+
gh_cli_release_assets <- function(tag, ..., repo = "ffverse/ffopportunity") {
93+
# see https://cli.github.com/manual/gh_release_view
94+
95+
# Make sure the gh cli is available
96+
gh_cli_available()
97+
98+
# create command for the shell
99+
cli_command <- paste(
100+
"gh release view",
101+
tag,
102+
"-R",
103+
repo,
104+
"--json assets"
105+
)
106+
107+
cli_output <- .invoke_cli_command(cli_command = cli_command)
108+
109+
out <- .cli_parse_json(cli_output = cli_output)[["assets"]]
110+
111+
setDT(out)
112+
ret <- out[,
113+
list(name, size, downloads = downloadCount, last_update = updatedAt, url)
114+
][, size_string := as.character(rlang::as_bytes(size))][
115+
!grepl("timestamp", name)
116+
]
117+
setDF(ret)
118+
ret
119+
}
120+
121+
.invoke_cli_command <- function(cli_command) {
122+
# This command will error regularly on R error and also errors on warnings
123+
# because some failures raise a warning only and we want workflows to fail
124+
# if somethings didn't work
125+
out <- purrr::quietly(system)(cli_command, intern = TRUE)
126+
if (length(out$warnings)) {
127+
cli::cli_abort(
128+
"The GitHub cli errored with the following message: {.val {out$result}}. \\
129+
Here is the R message: {.val {out$warnings}}",
130+
call = NULL
131+
)
132+
}
133+
out$result
134+
}
135+
136+
.cli_parse_json <- function(cli_output) {
137+
# regex shamelessly stolen from crayon::strip_style
138+
ansi_regex <- "(?:(?:\\x{001b}\\[)|\\x{009b})(?:(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m])|\\x{001b}[A-M]"
139+
gsub(ansi_regex, "", cli_output, perl = TRUE, useBytes = TRUE) |>
140+
paste0(collapse = "") |>
141+
jsonlite::parse_json(simplifyVector = TRUE)
142+
}

man/ffopportunity-package.Rd

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

update/ep_update.R

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
pkgload::load_all()
22

3+
4+
35
save_ep_data <- function(season, folder_path, version){
46

57
ep_object <- ffopportunity::ep_build(season,version = version)
@@ -23,12 +25,28 @@ save_ep_data <- function(season, folder_path, version){
2325
writeLines(as.character(version), file.path(folder_path,"version.txt"))
2426
}
2527

26-
upload_ep_data <- function(folder_path, version){
27-
list.files(folder_path, pattern = "csv$|rds$|parquet$|txt$", full.names = TRUE) %>%
28-
piggyback::pb_upload(repo = "ffverse/ffopportunity", tag = "latest-data", overwrite = TRUE)
28+
upload_ep_data <- function(folder_path, version) {
29+
list.files(
30+
folder_path,
31+
pattern = "csv$|rds$|parquet$|txt$",
32+
full.names = TRUE
33+
) %>%
34+
gh_cli_release_upload(
35+
tag = "latest-data",
36+
repo = "ffverse/ffopportunity",
37+
overwrite = TRUE
38+
)
2939

30-
list.files(folder_path, pattern = "csv$|rds$|parquet$|txt$", full.names = TRUE) %>%
31-
piggyback::pb_upload(repo = "ffverse/ffopportunity", tag = glue::glue("{version}-data"), overwrite = TRUE)
40+
list.files(
41+
folder_path,
42+
pattern = "csv$|rds$|parquet$|txt$",
43+
full.names = TRUE
44+
) %>%
45+
gh_cli_release_upload(
46+
tag = glue::glue("{version}-data"),
47+
repo = "ffverse/ffopportunity",
48+
overwrite = TRUE
49+
)
3250

3351
cli::cli_alert_success("Completed ep upload! {Sys.time()}")
3452
}

0 commit comments

Comments
 (0)