Skip to content

Instantly share code, notes, and snippets.

@jakubsob
Last active December 19, 2024 22:42
Show Gist options
  • Save jakubsob/292aec83e8956f9219fef67c90f8e677 to your computer and use it in GitHub Desktop.
Save jakubsob/292aec83e8956f9219fef67c90f8e677 to your computer and use it in GitHub Desktop.
Get PR stats table from a repo
get_prs <- function(repo, owner, base = "dev", state = "closed") {
query <- list(
base = base,
state = state
)
query <- paste(purrr::imap_chr(query, \(x, y) paste0(y, "=", x)), collapse = "&")
prs <- gh::gh(glue::glue("GET /repos/{owner}/{repo}/pulls?{query}"), .limit = Inf)
prs |>
purrr::map(\(x) {
pull_number <- x$number
files <- gh::gh(glue::glue("/repos/{owner}/{repo}/pulls/{pull_number}/files"), .limit = Inf)
changes <- files |>
purrr::map(\(f) {
tibble::tibble(
additions = f$additions,
deletions = f$deletions,
changes = f$changes
)
}) |>
dplyr::bind_rows() |>
dplyr::summarise(dplyr::across(everything(), sum))
tibble::tibble(
title = x$title,
author = x$user$login,
created_at = x$created_at,
updated_at = x$updated_at,
closed_at = x$closed_at,
merged_at = x$merged_at,
n_files_changed = length(files),
changes = changes,
url = x$url
)
}) |>
dplyr::bind_rows() |>
tidyr::unnest(changes) |>
dplyr::arrange(dplyr::desc(n_files_changed))
}
add_stats <- function(data) {
data |>
dplyr::mutate(
merged_at = lubridate::as_datetime(merged_at),
created_at = lubridate::as_datetime(created_at),
lead_time_to_change_minutes = lubridate::as.duration(merged_at - created_at) |>
lubridate::time_length("minutes"),
lead_time_to_change_days = lubridate::as.duration(merged_at - created_at) |>
lubridate::time_length("days"),
avg_changes_per_file = changes / n_files_changed
)
}
fastests_prs <- function(stats) {
stats |>
dplyr::filter(lead_time_to_change_minutes > 0) |>
dplyr::arrange(lead_time_to_change_minutes) |>
dplyr::select(title, author, lead_time_to_change_minutes) |>
dplyr::slice_head(n = 5)
}
lead_time_to_change_days <- function(stats) {
stats |>
ggplot2::ggplot(ggplot2::aes(x = lead_time_to_change_days)) +
ggplot2::geom_histogram(binwidth = 1, color = "black", boundary = 0) +
ggplot2::scale_x_continuous(breaks = seq(0, 30, 1)) +
ggplot2::labs(title = "Lead Time to Change", x = "Lead Time to Change (days)", y = "Number of Pull Requests") +
ggplot2::theme_minimal()
}
n_files_changed <- function(stats) {
stats |>
ggplot2::ggplot(ggplot2::aes(x = n_files_changed)) +
ggplot2::geom_histogram(binwidth = 1, color = "black", boundary = 0) +
ggplot2::scale_x_continuous(breaks = seq(0, 1000, 10)) +
ggplot2::labs(title = "Histogram of Number of Files Changed", x = "Number Files Changed", y = "Frequency") +
ggplot2::theme_minimal()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment