-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgithub-api.Rmd
More file actions
67 lines (56 loc) · 1.54 KB
/
github-api.Rmd
File metadata and controls
67 lines (56 loc) · 1.54 KB
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
---
title: "Github API"
author: "Charles Lang"
date: "6/4/2019"
output: html_document
---
```{r}
#install.packages("jsonlite")
library(jsonlite)
#install.packages("httpuv")
library(httpuv)
#install.packages("httr")
library(httr)
# Can be github, linkedin etc depending on application
oauth_endpoints("github")
# Change based on what you
myapp <- oauth_app(appname = "",
key = "",
secret = "")
# Get OAuth credentials
github_token <- oauth2.0_token(oauth_endpoints("github"), myapp)
```
```{r}
library(dplyr)
#Add repos you want to pull from
repos <- c("REPO1/pulls", "REPO2/pulls")
gtoken <- config(token = github_token)
D2 <- data.frame(matrix(vector(), 0, 3))
names(D2) <- c("username","pull.date","unit")
for(i in 1:5){
unit <- paste("https://api.github.com/repos/REPONAME/",repos[i], sep = "")
# Use API
req <- GET(unit, httr::authenticate("GITHUB USER", "PASSWORD"), httr::verbose())
# Take action on http error
stop_for_status(req)
# Extract content from a request
json1 = content(req)
# Convert to a data.frame
gitDF = jsonlite::fromJSON(jsonlite::toJSON(json1))
# Subset data.frame
#gitDF[gitDF$full_name == "Github-API/datasharing", "created_at"]
user <- gitDF$user$login
pull.date <- gitDF$created_at
if(is.null(user)){
gitDF <- NULL
D1 <- NULL
} else {
D1 <- data.frame(unlist(user), unlist(pull.date))
names(D1) <- c("username","pull.date")
D1$unit <- repos[i]
D2 <- bind_rows(D1, D2)
gitDF <- NULL
D1 <- NULL
}
}
```