-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub-issues-r.Rmd
212 lines (143 loc) · 7.62 KB
/
github-issues-r.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
---
title: "Github Issues"
author: "Robert Saldivar"
date: "7/7/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r, message=FALSE}
library(tidyverse)
library(gh)
library(purrr)
library(tidyr)
library(curl)
library(keyring)
library(plotly)
library(rlang)
```
```{r}
# The Example for this comes from https://github.com/jennybc/analyze-github-stuff-with-r
#Example Code
issues_gh <- gh("/repos/brunj7/nceas-r-packages/issues", owner = "brunj7", repo = "nceas-r-packages", state = "all")
map_chr_hack <- function(.x, .f, ...) {
map(.x, .f, ...) %>%
map_if(is.null, ~ NA_character_) %>%
flatten_chr()
}
map_dfr_hack <- function(.x, .f, ...) {
map(.x, .f, ...) %>%
map_if(is_na, ~ NA_character_) %>%
flatten_dfr()
}
issue_gh_df <- issues_gh %>%
{
data.frame(number = map_int(., "number"), #Calls the issue number
id = map_int(., "id"), #Calls the issue id
title = map_chr(., "title"), #Calls the title of the issue
state = map_chr(., "state"), #States if the issue is open or closed
opener = map_chr(., c("user", "login")), #The user who created the issue
created_at = map_chr(., "created_at") %>% as.Date(), #The date the issue was created
closed_at = map_chr_hack(., "closed_at") %>% as.Date(), #The date the issued was closed
n_comments = map_int(., "comments") #This is the number of comments in an issue
)
}
```
This demonstrates how to store a token/password/key in the keyring package
```{r}
#This section will demonstrate how to use the keyring package to store tokens.
#Keyring allows the user to set multiple tokens without needing to set them in the environment
#Here is a link to a youtube video demonstrating how to use the keyring package: https://www.youtube.com/watch?v=Q8Cilx-MOsU
#Here is a link demondstrating how to get tokens from github: https://docs.github.com/en/github/authenticating-to-github/creating-a-personal-access-token
#This is going to create a new keyring to store tokens
# keyring::keyring_create("Example_Keyring") #this only needs to be run once
#This sets a password in the keyring
# keyring::key_set_with_value(service= "Key", #This is what will be called when using the keyring to access the stored token
# password= "Password", # this is the password/token that is being saved
# keyring= "Example_Keyring" #This is the keyring that the password is being associated with
# )
#Example of how to use keyring with gh
#gh("/repos/:user/:repo", .token = keyring::key_get(service = "Key", keyring = "Example_Keyring"))
#Alternatively you can store token in .renviron by
# usethis::edit_r_environ()
# GITHUB_PAT = password
#Here is a good reference for setting .renviron: https://rstats.wtf/r-startup.html
```
```{r}
# This section will be to try to access a private repo
gh_issues_private <- gh("/repos/Science-for-Nature-and-People/github-issues-r/issues",
owner = "Science-for-Nature-and People",
repo = "github-issues-r",
state = "all",
.token = NULL # Token is set to NULL because GITHUB_PAT has been set in my .Renviron, if someone uses this code without it set it will fail.
#keyring::key_get("Github_Token", keyring = "Git_Credentials")
)
gh_issues_private_df <- gh_issues_private %>%
{
data.frame(labels = map_dfr(., ~ .$"labels"[1])
)
}
```
```{r}
#this is so I can look at how the repo is originally being pulled
snapp_wg_scicomp_issues_proto <- gh("/repos/SNAPP/snapp-wg-scicomp/issues",
.api_url = "https://github.nceas.ucsb.edu/api/v3",
owner = "SNAPP",
repo = "snapp-wg-scicomp",
#For this section of code to work the token from github enterprise needs to have been set in a keyring named "Git_Credentials" and name the value "Enterprise token"
.token = keyring::key_get("Github_Enterprise_Token", keyring = "Git_Credentials"),
state = "all",
.limit = "all"
)
proto_map <- map(snapp_wg_scicomp_issues_proto, "labels") %>% flatten() %>% map_chr_hack(., "name") %>% as.data.frame()
proto_graph <- ggplot(proto_map, aes(x = .)) +
geom_bar()+
xlab("Labels") +
ylab("Count") +
theme(axis.text.x = element_text(angle = 90, hjust=1, vjust = 0.5))
proto_graph
```
```{r}
#Trying to use the gh function on the snapp_wg_scicomp repo which a github enterprise repo.
snapp_wg_scicomp_issues <- gh("/repos/SNAPP/snapp-wg-scicomp/issues",
.api_url = "https://github.nceas.ucsb.edu/api/v3",
owner = "SNAPP",
repo = "snapp-wg-scicomp",
#For this section of code to work the token from github enterprise needs to have been set in a keyring named "Git_Credentials" and name the value "Enterprise token"
.token = keyring::key_get("Github_Enterprise_Token", keyring = "Git_Credentials"),
state = "all",
.limit = "all") %>%
{
data.frame(number = map_int(., "number"), #Calls the issue number
id = map_int(., "id"), #Calls the issue id
state = map_chr(., "state"), #States if the issue is open or closed
creator = map_chr(., c("user", "login")), #The user who created the issue
created_at = map_chr(., "created_at") %>% as.Date(), #The date the issue was created
closed_at = map_chr_hack(., "closed_at") %>% as.Date(), #The date the issued was closed
n_comments = map_int(., "comments"), #This is the number of comments in an issue #The date the issue was created
title = map_chr(., "title"), #Calls the title of the issue
labels1 = map_dfr_hack(., ~.$"labels"[1]), # gives everything under the first label
labels2 = map_dfr_hack(., ~.$"labels"[2]) # inaccurate, currently if the issue does not have a second label it will go to the next issue with a second label and repeat until finished
)
} %>%
select("number", "id", "title", "creator", "state", "created_at", "closed_at", "labels1.name", "labels2.name", "n_comments")
#One option is for this work flow is give all of the issues 2 labels, and if only one label properly applies to the issue then the second label could be "placeholder" and the "placeholder labels could be filtered out when its time to report.
```
```{r}
#this will filter issues that were created between July 1st 2019 to jun 30th 2020
snapp_wg_scicomp_issues_filtered <- snapp_wg_scicomp_issues %>%
filter(created_at > "2019-07-01" & created_at < "2020-06-30")
snapp_wg_scicomp_issues_filtered_labels <- snapp_wg_scicomp_issues_filtered %>%
stack(select = c("labels1.name", "labels2.name"))
```
```{r}
#This part will count the number of issues per working group
count(snapp_wg_scicomp_issues_filtered_labels, snapp_wg_scicomp_issues_filtered_labels$values)
snapp_Scicomp_issues_plot <- ggplot(snapp_wg_scicomp_issues_filtered_labels, aes(x = values)) +
geom_bar() +
xlab("Labels") +
ylab("Count") +
theme(axis.text.x = element_text(angle = 90, hjust=1, vjust = 0.5))
snapp_Scicomp_issues_plot # Graph needs to be cleaned up
```