This repository has been archived by the owner on Dec 30, 2023. It is now read-only.
forked from gadenbuie/status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-repo-info.R
128 lines (110 loc) · 3.57 KB
/
gh-repo-info.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# Library ----
library(gh)
library(memoise)
library(purrr)
library(dplyr)
library(tidyr)
library(glue)
library(readr)
# gh functions ----
gh_workflows <- memoise::memoise(function(owner, repo, ...) {
tryCatch(
gh("/repos/{owner}/{repo}/actions/workflows", owner = owner, repo = repo) %>%
.$workflows,
error = function(e) NULL
)
}, cache = cache_memory())
gh_runs <- memoise::memoise(function(owner, repo, workflow_id, ...) {
gh(
"/repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs",
owner = owner,
repo = repo,
workflow_id = workflow_id,
per_page = 1
)$workflow_runs[[1]]
}, cache = cache_memory())
gh_url <- memoise::memoise(function(url) {
gh(url)
}, cache = cache_memory())
gh_repo <- memoise::memoise(function(owner, repo, ...) {
gh("/repos/{owner}/{repo}", owner = owner, repo = repo)
}, cache = cache_memory())
gh_owner_repos <- memoise::memoise(function(owner) {
gh("/users/{username}/repos", username = owner, .limit = Inf, type = "owner") %>%
map(keep, negate(is.null)) %>%
map(keep, negate(is.list)) %>%
map_dfr(as_tibble) %>%
filter(!private, !fork) %>%
mutate(owner = owner) %>%
mutate(subscribers_count = map(subscribers_url, gh) %>% map_int(length)) %>%
select(owner, repo = name, full_name, contains("count"), html_url_repo = html_url, fork) %>%
arrange(desc(stargazers_count))
}, cache = cache_memory())
gh_repo_stats <- function(repos) {
# repos should be a tibble now with owner, repo
repos$.repo <- pmap(repos, gh_repo)
repos %>%
mutate(
full_name = map_chr(.repo, "full_name"),
stargazers_count = map_dbl(.repo, "stargazers_count"),
subscribers_count = map_dbl(.repo, "subscribers_count"),
open_issues_count = map_dbl(.repo, "open_issues_count"),
forks_count = map_dbl(.repo, "forks_count"),
open_issues_count = map_dbl(.repo, "open_issues_count"),
fork = map_lgl(.repo, "fork"),
html_url_repo = map_chr(.repo, "html_url")
)
}
gh_repo_workflows <- function(repos) {
# repos should be a tibble with owner, repo
repos$.workflows <- repos %>% pmap(gh_workflows)
workflows <- repos %>%
unnest(.workflows) %>%
mutate(
workflow_id = map_chr(.workflows, "id"),
badge_url = map_chr(.workflows, "badge_url")
)
workflows$runs <- pmap(workflows, gh_runs)
workflows %>%
mutate(
html_url_run = map_chr(runs, "html_url"),
run_conclusion = map_chr(runs, "conclusion", .default = NA_character_),
commit_message = map_chr(runs, ~ .x$head_commit$message),
commit_id = map_chr(runs, `[[`, c("head_commit", "id"))
)
}
# Get repos
gh_get_repo_status <- function(
repo_list = NULL,
all_by_owner = NULL,
.write_csv = !interactive()
) {
if (is.null(repo_list) && is.null(all_by_owner)) {
stop("At least one repo must be listed or a username must be provided in `all_by_owner`")
}
by_vars <- c("owner", "repo")
repos <- if (!is.null(repo_list)) {
repo_list %>%
map_dfr(~ tibble(repo = .), .id = "owner") %>%
gh_repo_stats()
}
if (!is.null(all_by_owner)) {
owner_repos <- gh_owner_repos(all_by_owner)
if (!is.null(repos)) {
owner_repos <- owner_repos %>% anti_join(repos, by = by_vars)
}
repos <- bind_rows(repos, owner_repos)
}
workflows <- repos %>% select(owner, repo) %>% gh_repo_workflows()
repos <- bind_rows(
inner_join(repos, workflows, by = by_vars),
anti_join(repos, workflows, by = by_vars)
)
if (isTRUE(.write_csv)) {
repos %>%
select_if(negate(is.list)) %>%
arrange(full_name) %>%
write_csv("repos.csv")
}
repos
}