generated from gadenbuie/status
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-repo-info.R
153 lines (131 loc) · 4.3 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# Library ----
library(gh)
library(memoise)
library(purrr)
library(dplyr)
library(tidyr)
library(glue)
library(readr)
# gh functions ----
gh_workflows <- function(owner, repo, ...) {
tryCatch(
gh("/repos/{owner}/{repo}/actions/workflows", owner = owner, repo = repo) %>%
.$workflows,
error = function(e) NULL
)
}
gh_runs <- function(owner, repo, workflow_id, ...) {
runs <- gh(
"/repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs",
owner = owner,
repo = repo,
workflow_id = workflow_id,
per_page = 1
)
# Return an empty list if the workflow as never run
if (length(runs$workflow_runs) == 0) {
return(list())
}
runs$workflow_runs[[1]]
}
gh_url <- function(url) {
gh(url)
}
gh_repo <- function(owner, repo, ...) {
gh("/repos/{owner}/{repo}", owner = owner, repo = repo)
}
gh_owner_repos <- 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))
}
# memoize everything to avoid repeated interactive calls over a short period
gh_workflows <- memoise::memoise(gh_workflows, cache = cache_memory())
gh_runs <- memoise::memoise(gh_runs, cache = cache_memory())
gh_url <- memoise::memoise(gh_url, cache = cache_memory())
gh_repo <- memoise::memoise(gh_repo, cache = cache_memory())
gh_owner_repos <- memoise::memoise(gh_owner_repos, 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 %>%
# Remove workflows that have never run
filter(purrr::map_int(runs, length) > 0) %>%
mutate(
event = map_chr(runs, "event"),
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()
workflows <-
workflows %>%
filter(event %in% c("push", "schedule")) %>%
mutate(badge = glue::glue("[![]({badge_url})]({html_url_run})")) %>%
group_by(owner, repo, commit_id, commit_message) %>%
summarize(badge = paste(badge, collapse = " ")) %>%
ungroup()
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
}