Skip to content

Commit

Permalink
Merge pull request #135 from rundel/artifacts
Browse files Browse the repository at this point in the history
Artifacts
  • Loading branch information
rundel authored Jul 31, 2023
2 parents cf3873c + 54b2005 commit d3555d6
Show file tree
Hide file tree
Showing 30 changed files with 1,064 additions and 155 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ Makefile
^cran-comments\.md$
^\.github$
^CRAN-SUBMISSION$
test/
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ inst/doc
doc
Meta
vignettes/articles/cache/
test
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: ghclass
Title: Tools for Managing Classes on GitHub
Version: 0.2.1
Version: 0.2.1.9000
Authors@R:
c(person(given = "Colin",
family = "Rundel",
Expand Down Expand Up @@ -37,7 +37,8 @@ Imports:
whisker,
withr,
dplyr,
cli (>= 3.0.0)
cli (>= 3.0.0),
lifecycle
Suggests:
here,
knitr,
Expand All @@ -51,4 +52,4 @@ Suggests:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.3
10 changes: 10 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# Generated by roxygen2: do not edit by hand

export(action_add_badge)
export(action_artifact_delete)
export(action_artifact_download)
export(action_artifacts)
export(action_remove_badge)
export(action_runs)
export(action_runtime)
export(action_status)
export(action_workflows)
export(branch_create)
Expand All @@ -11,10 +15,12 @@ export(branch_remove)
export(github_get_api_limit)
export(github_get_token)
export(github_orgs)
export(github_rate_limit)
export(github_reset_token)
export(github_set_api_limit)
export(github_set_token)
export(github_test_token)
export(github_token_scopes)
export(github_whoami)
export(issue_close)
export(issue_create)
Expand Down Expand Up @@ -42,6 +48,10 @@ export(org_set_repo_permission)
export(org_sitrep)
export(org_team_details)
export(org_teams)
export(pages_create)
export(pages_delete)
export(pages_enabled)
export(pages_status)
export(pr_create)
export(repo_add_file)
export(repo_add_team)
Expand Down
26 changes: 26 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,29 @@
# ghclass (development version)

# ghclass 0.3.0.9000

* Added support for basig GitHub Pages API endpoints - see `pages_enabled()`, `pages_status()`, `pages_create()`, and `pages_delete()`.

* Added support for retrieving details for GitHub Action artifacts via `action_artifacts()`

* Added support for downloading GitHub Action artifacts via `action_artifact_download()`

* Added support for deleting GitHub Action artifacts via `action_artifact_delete()`

* Added additional parameters to `org_repos()` to control which repos are returns and in what order, see function docs.

* Reworked `action_runs()` to work with multiple repositories and included new filtering options. This makes `action_status()` redundant and as such it has been deprecated.

* Added `action_runtime()` which supplements the results of `action_runs()` with the duration for each action run.

* Added `github_token_scopes()` which returns a vector of granted scopes for the given PAT.

* Added `github_rate_limit()` for checking current rate limit status.

* Added `add_badges` argument to `org_create_assignment()`

* Added support for GitHub's versioned REST API

# ghclass 0.2.1

* ghclass is now on [CRAN](https://cran.r-project.org/web/packages/ghclass/)!
Expand Down
32 changes: 27 additions & 5 deletions R/action.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,43 @@
#'
#' * `action_runs()` - retrieve details on repo workflow runs.
#'
#' * `action_status()` - retrieve details on most recent workflow runs.
#' * `action_status()` - DEPRECATED - retrieve details on most recent workflow runs.
#'
#' * `action_runtime()` - retrieves runtime durations for workflow runs.
#'
#' * `action_artifacts()` - retrieve details on available workflow artifacts.
#'
#' * `action_artifact_download()` - downloads artifact(s) into a local directory.
#'
#' * `action_artifact_delete()` - deletes artifact(s).
#'
#' @param repo Character. Address of repositories in `owner/name` format.
#' @param dir Character. Path to the directory where artifacts will be saved.
#' @param ids Integer or data frame. Artifact ids to be downloaded or deleted.
#' If a data frame is passed then the `id` column will be used.
#'
#' @return
#'
#' `action_workflows()`, `action_runs()`, and `action_status()` all return
#' tibbles containing information on requested repos' available workflows,
#' recent action runs, and recent action run statuses respectively.
#' `action_workflows()`, `action_runs()`, `action_runtime()`, and `action_artifacts`
#' all return tibbles containing information on requested repos' available workflows,
#' recent workflow runs, workflow runs runtimes, and generated artifacts
#' respectively.
#'
#' `action_artifact_download()` returns a character vector containing the paths of all
#' downloaded fules
#'
#' `action_artifact_delete()` returns an invisible data frame containing repository names and
#' ids of the deleted artifacts.
#'
#' @examples
#' \dontrun{
#' action_workflows("rundel/ghclass")
#'
#' action_runs("rundel/ghclass")
#'
#' action_status(c("rundel/ghclass", "rundel/parsermd"))
#' action_runtime(c("rundel/ghclass", "rundel/parsermd"))
#'
#' action_artifacts(c("rundel/ghclass", "rundel/parsermd"))
#' }
#'
NULL
Expand Down
84 changes: 84 additions & 0 deletions R/action_artifacts.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
github_api_action_artifacts = function(repo) {
arg_is_chr_scalar(repo)

ghclass_api_v3_req(
endpoint = "GET /repos/{owner}/{repo}/actions/artifacts",
owner = get_repo_owner(repo),
repo = get_repo_name(repo)
)
}

#' @name action
#' @rdname action
#'
#' @param repo Character. Address of repositories in `owner/name` format.
#' @param keep_expired Logical. Should expired artifacts be returned.
#' @param which Character. Either `"latest"` to return only the most recent of each
#' artifact or `"all"` to return all artifacts.
#' @export
#'
action_artifacts = function(repo, keep_expired=FALSE, which=c("latest", "all")) {
which = match.arg(which)

arg_is_chr(repo)
arg_is_chr_scalar(which)
arg_is_lgl_scalar(keep_expired)

res = purrr::map_dfr(
repo,
function(r) {
res = purrr::safely(github_api_action_artifacts)(r)
status_msg(
res,
fail = "Failed to retrieve artifacts for repo {.val {repo}}."
)

if (failed(res)) {
NULL
} else if (empty_result(res) || result(res)[["total_count"]] == 0) {
tibble::tibble(
repo = character(),
name = character(),
id = integer(),
size = integer(),
url = character(),
expired = logical(),
created = lubridate::ymd_hms(),
updated = lubridate::ymd_hms(),
expires = lubridate::ymd_hms()
)
} else {
artifacts = result(res)[["artifacts"]]

tibble::tibble(
repo = r,
name = purrr::map_chr(artifacts, "name", .default = NA),
id = purrr::map_int(artifacts, "id", .default = NA),
size = purrr::map_int(artifacts, "size_in_bytes", .default = NA),
url = purrr::map_chr(artifacts, "url", .default = NA),
expired = purrr::map_lgl(artifacts, "expired", .default = NA),
created = purrr::map_chr(artifacts, "created_at", .default = NA) %>% lubridate::ymd_hms(),
updated = purrr::map_chr(artifacts, "updated_at", .default = NA) %>% lubridate::ymd_hms(),
expires = purrr::map_chr(artifacts, "expires_at", .default = NA) %>% lubridate::ymd_hms()
)
}
}
)

if (!keep_expired) {
res = dplyr::filter(res, .data[["expired"]] == FALSE)
}

if (which == "latest") {
res %>%
dplyr::group_by(.data[["repo"]], .data[["name"]]) %>%
dplyr::arrange(dplyr::desc(.data[["created"]])) %>%
dplyr::slice(1) %>%
dplyr::ungroup()
} else if (which == "all") {
res
} else {
cli_stop("Invalid which choice.")
}

}
48 changes: 48 additions & 0 deletions R/action_artifacts_delete.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
github_api_delete_artifact = function(repo, id) {
arg_is_chr_scalar(repo)
arg_is_pos_int(id)

ghclass_api_v3_req(
endpoint = "DELETE /repos/{owner}/{repo}/actions/artifacts/{artifact_id}",
owner = get_repo_owner(repo),
repo = get_repo_name(repo),
artifact_id = id
)

}


#' @name action
#' @rdname action
#'
#' @export
#'
action_artifact_delete = function(repo, ids) {

arg_is_chr(repo)

if (is.numeric(ids))
ids = tibble::tibble(repo = repo, id = ids)
arg_is_df(ids)

df = dplyr::left_join(
tibble::tibble(repo = repo), ids,
by = "repo"
) %>%
dplyr::select("repo", "id")

purrr::pwalk(
df,
function(repo, id) {
res = purrr::safely(github_api_delete_artifact)(repo, id)

status_msg(
res,
"Deleted artifact {.val {id}} from repo {.val {repo}}.",
"Failed to delete artifact with id {.val {id}} from repo {.val {repo}}."
)
}
)

invisible(df)
}
Loading

0 comments on commit d3555d6

Please sign in to comment.