Skip to content

Commit

Permalink
Add git_pushes()
Browse files Browse the repository at this point in the history
  • Loading branch information
rundel committed Oct 2, 2024
1 parent 4737634 commit 1fb1480
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,4 @@ Suggests:
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export(repo_mirror_template)
export(repo_modify_file)
export(repo_n_commits)
export(repo_prs)
export(repo_pushes)
export(repo_put_file)
export(repo_remove_team)
export(repo_remove_user)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

* Added check to `repo_mirror_template()` for empty repositories to avoid cryptic GitHub api error.

* Added `repo_pushes()` to retrieve push activity for a repository.

# ghclass 0.3.0

* Added support for basig GitHub Pages API endpoints - see `pages_enabled()`, `pages_status()`, `pages_create()`, and `pages_delete()`.
Expand Down
2 changes: 2 additions & 0 deletions R/repo.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ NULL
#'
#' * `repo_prs()` - Returns a tibble of pull requests for a GitHub repository.
#'
#' * `repo_pushes()` - Returns a tibble of push activity to a GitHub repository.
#'
#' @param repo Character. Address of repository in `owner/repo` format.
#'
#' @return `repo_clone_url()` and `repo_branches()` both return a character vector.
Expand Down
83 changes: 83 additions & 0 deletions R/repo_pushes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@

github_api_repo_activity = function(repo, ref = NULL, actor = NULL, time_period = NULL, activity_type = NULL) {
arg_is_chr_scalar(repo, allow_null = FALSE)
arg_is_chr_scalar(ref, actor, time_period, activity_type, allow_null = TRUE)

if (!is.null(time_period))
stopifnot(time_period %in% c("day", "week", "month", "quarter", "year"))

if (!is.null(activity_type))
stopifnot(activity_type %in% c("push", "force_push", "branch_creation", "branch_deletion", "pr_merge", "merge_queue_merge"))

ghclass_api_v3_req(
endpoint = "GET /repos/:owner/:repo/activity",
owner = get_repo_owner(repo),
repo = get_repo_name(repo),
ref = ref,
actor = actor,
time_period = time_period,
activity_type = activity_type
)
}

#' @rdname repo_details
#'
#' @param time_period Character. The time period to filter by.
#' Options are "all time", "day", "week", "month", "quarter", "year".
#'
#' @export
#'

repo_pushes = function(repo, branch = NULL, time_period = c("all time", "day", "week", "month", "quarter", "year"), quiet = FALSE) {

time_period = match.arg(time_period)

if (time_period == "all time")
time_period = NULL

arg_is_chr(repo)
arg_is_chr_scalar(branch, time_period, allow_null = TRUE)
arg_is_lgl_scalar(quiet)

purrr::map_dfr(
repo,
function(repo) {
res = purrr::safely(github_api_repo_activity)(
repo, ref = branch, time_period = time_period, activity_type = "push"
)

if (!quiet) {
status_msg(
res,
fail = "Failed to retrieve activity from {.val {repo}}."
)
}

pushes = result(res)

if (empty_result(pushes)) {
tibble::tibble(
repo = character(),
login = character(),
ref = character(),
activity = character(),
date = as.POSIXct(character()),
before = character(),
after = character()
)
} else {
tibble::tibble(
repo = repo,
login = purrr::map_chr(pushes, c("actor", "login"), .default = NA),
ref = purrr::map_chr(pushes, c("ref"), .default = NA),
activity = purrr::map_chr(pushes, c("activity_type"), .default = NA),
date = lubridate::ymd_hms(
purrr::map_chr(pushes, c("timestamp"), .default = NA)
),
before = purrr::map_chr(pushes, c("before"), .default = NA),
after = purrr::map_chr(pushes, c("after"), .default = NA)
)
}
}
)
}
15 changes: 14 additions & 1 deletion man/repo_details.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1fb1480

Please sign in to comment.