|
| 1 | +cm_data_gh_contributors <- function (path) { |
| 2 | + |
| 3 | + log <- cm_data_gitlog (path) |
| 4 | + gh_url <- pkg_gh_url_from_path (path) |
| 5 | + |
| 6 | + |
| 7 | +} |
| 8 | + |
| 9 | +contribs_from_log <- function (log) { |
| 10 | + |
| 11 | + gh_handle <- unique (log$aut_name) |
| 12 | + gh_email <- log$aut_email [match (gh_handle, log$aut_name)] |
| 13 | + |
| 14 | + # Remove any duplicates of either, but excluding non-entries: |
| 15 | + rm_dup_rows <- function (x) { |
| 16 | + x <- gsub ("\\s+", "", x) |
| 17 | + index <- seq_along (x) |
| 18 | + index_out <- which (duplicated (x) & nzchar (x)) |
| 19 | + if (length (index_out) > 0) { |
| 20 | + index <- index [-(index_out)] |
| 21 | + } |
| 22 | + return (index) |
| 23 | + } |
| 24 | + index1 <- rm_dup_rows (gh_handle) |
| 25 | + index2 <- rm_dup_rows (gh_email) |
| 26 | + |
| 27 | + # Then extract only instances where neither handles nor emails are |
| 28 | + # duplicated: |
| 29 | + index_table <- table (c (index1, index2)) |
| 30 | + index <- as.integer (names (index_table) [which (index_table == 2L)]) |
| 31 | + |
| 32 | + data.frame ( |
| 33 | + handle = gh_handle, |
| 34 | + email = gh_email |
| 35 | + ) [index, ] |
| 36 | +} |
| 37 | + |
| 38 | +contribs_from_gh_api <- function (path, n_per_page = 100) { |
| 39 | + |
| 40 | + is_test_env <- Sys.getenv ("REPOMETRICS_TESTS") == "true" |
| 41 | + |
| 42 | + gh_url <- pkg_gh_url_from_path (path) |
| 43 | + if (is.null (gh_url)) { |
| 44 | + return (NULL) |
| 45 | + } |
| 46 | + |
| 47 | + org_repo <- gsub ("https://github.com/", "", gh_url, fixed = TRUE) |
| 48 | + if (!grepl ("\\/$", org_repo)) { |
| 49 | + org_repo <- paste0 (org_repo, "/") |
| 50 | + } |
| 51 | + |
| 52 | + u_base <- "https://api.github.com/repos/" |
| 53 | + u_org_repo <- paste0 (u_base, org_repo) |
| 54 | + u_endpoint <- paste0 (u_org_repo, "contributors") |
| 55 | + |
| 56 | + req <- httr2::request (u_endpoint) |> |
| 57 | + httr2::req_url_query (per_page = n_per_page) |
| 58 | + |
| 59 | + body <- NULL |
| 60 | + next_page <- 1 |
| 61 | + |
| 62 | + while (!is.null (next_page)) { |
| 63 | + |
| 64 | + req <- add_gh_token_to_req (req) |
| 65 | + resp <- httr2::req_perform (req) |
| 66 | + httr2::resp_check_status (resp) |
| 67 | + |
| 68 | + body <- c (body, httr2::resp_body_json (resp)) |
| 69 | + |
| 70 | + next_page <- get_next_page (resp) |
| 71 | + if (is_test_env) { |
| 72 | + next_page <- NULL |
| 73 | + } |
| 74 | + |
| 75 | + req <- httr2::request (u_endpoint) |> |
| 76 | + httr2::req_url_query (per_page = 10) |> |
| 77 | + httr2::req_url_query (page = next_page) |
| 78 | + } |
| 79 | + |
| 80 | + login <- vapply (body, function (i) i$login, character (1L)) |
| 81 | + ctb_id <- vapply (body, function (i) i$id, integer (1L)) |
| 82 | + avatar_url <- vapply (body, function (i) i$avatar_url, character (1L)) |
| 83 | + api_url <- vapply (body, function (i) i$url, character (1L)) |
| 84 | + gh_url <- vapply (body, function (i) i$html_url, character (1L)) |
| 85 | + contributions <- vapply (body, function (i) i$contributions, integer (1L)) |
| 86 | + |
| 87 | + ctbs <- data.frame ( |
| 88 | + login = login, |
| 89 | + ctb_id = ctb_id, |
| 90 | + avatar_url = avatar_url, |
| 91 | + api_url = api_url, |
| 92 | + gh_url = gh_url, |
| 93 | + contributions = contributions |
| 94 | + ) |
| 95 | + |
| 96 | + ctbs_user_info <- lapply (ctbs$login, user_from_gh_api) |
| 97 | + ctbs_user_info <- do.call (rbind, ctbs_user_info) |
| 98 | + |
| 99 | + ctbs <- dplyr::left_join (ctbs, ctbs_user_info, by = c ("login", "ctb_id")) |
| 100 | + |
| 101 | + return (ctbs) |
| 102 | +} |
| 103 | + |
| 104 | +user_from_gh_api <- function (user) { |
| 105 | + |
| 106 | + u_base <- "https://api.github.com/users/" |
| 107 | + u_endpoint <- paste0 (u_base, user) |
| 108 | + |
| 109 | + req <- httr2::request (u_endpoint) |> |
| 110 | + add_gh_token_to_req () |
| 111 | + resp <- httr2::req_perform (req) |
| 112 | + httr2::resp_check_status (resp) |
| 113 | + body <- httr2::resp_body_json (resp) |
| 114 | + |
| 115 | + data.frame ( |
| 116 | + login = body$login, |
| 117 | + ctb_id = body$id, |
| 118 | + name = null2na_char (body$name), |
| 119 | + company = null2na_char (body$company), |
| 120 | + email = null2na_char (body$email), |
| 121 | + location = null2na_char (body$location), |
| 122 | + blog = null2na_char (body$blog), |
| 123 | + bio = null2na_char (body$bio), |
| 124 | + public_repos = body$public_repos, |
| 125 | + followers = body$followers, |
| 126 | + following = body$following, |
| 127 | + created_at = body$created_at, |
| 128 | + updated_at = body$updated_at |
| 129 | + ) |
| 130 | +} |
0 commit comments