This repository has been archived by the owner on Nov 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathREADME.Rmd
241 lines (191 loc) · 5.36 KB
/
README.Rmd
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%",
knitr.kable.NA = ''
)
```
# GithubMetrics
<!-- badges: start -->
[![R-CMD-check](https://github.com/openpharma/GithubMetrics/workflows/R-CMD-check/badge.svg)](https://github.com/openpharma/GithubMetrics/actions)
[![Codecov test coverage](https://codecov.io/gh/openpharma/GithubMetrics/branch/master/graph/badge.svg)](https://codecov.io/gh/openpharma/GithubMetrics?branch=master)
[![HitCount](http://hits.dwyl.com/OpenPharma/GithubMetrics.svg)](http://hits.dwyl.com/OpenPharma/GithubMetrics)
<!-- badges: end -->
The aim of this package is to provide a wrapper on `gh` to quickly get you
key Github repo information you need.The code here is used within Roche to
quickly let me pull answer simple questions like:
* How many studies have more than 1 data scientist (and roughly what's the commit split)
* What are the common languages being used (proxied through file type distribution within repos)
* Pull commit metadata to enrich other study info held in other systems
## Installation
You can install the released version of GithubMetrics from [CRAN](https://CRAN.R-project.org) with:
``` r
install.packages("GithubMetrics")
```
## Setup
```{r setup, warning=FALSE, message=FALSE}
library(GithubMetrics)
library(tidyverse)
library(glue)
organisation <- "openpharma"
```
## Repos in an org
Pull all the repos present within an org (that I can see).
```{r}
repos_raw <- gh_repos_get(
org = organisation
)
repos_clean <- gh_repos_clean(repos_raw)
glimpse(repos_clean)
```
Realistically, research code is likely to be on Github Enterprise, so
the `.api_url` and `.token` parameters can be passed through to `gh()`.
Commented code below shows how you can use an on-premise Github server.
```{r, eval = FALSE}
# repos_raw <- gh_repos_get(
# org = organisation,
# .api_url = "https://github.roche.com/api/v3",
# .token = Sys.getenv("GITHUB_PAT_ROCHE")
# )
```
## Commits
Get every commit for all the repos in this organisation.
```{r}
repo_all_commits <- gh_commits_get(
repos_clean %>% filter(size > 0) %>% pull(full_name),
days_back = 365*10
)
glimpse(repo_all_commits)
```
## People
Pull all the people that have committed in `r `.
```{r}
contributors <- repo_all_commits %>%
group_by(author) %>%
summarise(
commits = n()
) %>%
filter(!author %in% c(".gitconfig missing email","actions-user"))
contributors <- contributors %>%
left_join(
gh_user_get(contributors$author),
by = c("author"="username")
)
contributors %>%
arrange(-commits) %>%
mutate(
last_active = Sys.Date() - last_active,
contributor = glue('<img src="{avatar}" alt="" height="30"> {author}'),
blog = case_when(
blog == "" ~ "",
TRUE ~ as.character(glue('<a href="{blog}">link</a>'))
)
) %>%
select(contributor,commits,name,last_active,company,location,blog) %>%
knitr::kable(
)
```
## Files
Pull a specific file using `gh_file_get()`.
```{r}
desc_formatted <- gh_file_get(
repo = "GithubMetrics",
org = "OpenPharma",
file = "DESCRIPTION"
) %>%
# format the description
desc::desc(text = .)
# Print it
desc_formatted$get(c("Package","Title","Version")) %>%
tibble::enframe() %>%
knitr::kable()
```
Get all of the files present in the last commit of all the repos using
`gh_repo_files_get()`.
```{r}
repo_files <- gh_repo_files_get(
repo_commits = repo_all_commits,
only_last_commit = TRUE
)
glimpse(repo_files)
repo_files %>%
group_by(repo) %>%
summarise(
Files = n(),
`R files` = sum(lang %in% "R"),
`Python files` = sum(lang %in% c("Python","Jupyter Notebook"))
) %>% knitr::kable(
caption = "Types of files in the organisation"
)
```
```{r}
results <- gh_repo_search(
code = "tidyverse",
organisation = organisation
)
glimpse(results)
```
```{r, warning=FALSE}
helper_gh_repo_search <- function(x, org = "openpharma"){
## Slow it down! as search has 30 calls a minute rate limit.
## If you prem the search rate limit is higher, so usually not needed
if(interactive()){message("Wait 5 seconds")}
Sys.sleep(5)
## End slow down
results <- gh_repo_search(
code = x,
organisation = org
)
if(is.na(results)) {
results <- return()
}
results %>%
mutate(Package = x, Organisation = org) %>%
group_by(Organisation,Package) %>%
summarise(
Repos = n_distinct(full_name), .groups = "drop"
)
}
packages <- c(
"tidyverse","pkgdown","dplyr","data.table"
)
package_use <- bind_rows(
packages %>%
map_df(
helper_gh_repo_search, org = "PHCAnalytics"
),
packages %>%
map_df(
helper_gh_repo_search, org = "openpharma"
),
packages %>%
map_df(
helper_gh_repo_search, org = "AstraZeneca"
),
packages %>%
map_df(
helper_gh_repo_search, org = "Roche"
),
packages %>%
map_df(
helper_gh_repo_search, org = "Genentech"
),
packages %>%
map_df(
helper_gh_repo_search, org = "Novartis"
)
)
package_use %>%
pivot_wider(names_from = "Package", values_from = "Repos") %>%
mutate(Total = rowSums(.[,-1], na.rm = TRUE)) %>%
arrange(-Total) %>%
knitr::kable(
caption = "Package use detected within repositaries in Pharma orgs"
)
```