Last active
June 16, 2020 04:28
-
-
Save grantmcdermott/22ec552a1e45ac0ae7595a0167e203f9 to your computer and use it in GitHub Desktop.
Simple function for extracting commit messages from a GitHub user profile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Context: https://twitter.com/grant_mcdermott/status/1272747522562244608 | |
## Note: To get the most out of this function (private repos, etc.), I recommend | |
## adding a GitHub Personal Access Token to your .Renviron file. The easiest way | |
## to do this is with the {usethis} package. See here: | |
## https://happygitwithr.com/github-pat.html#step-by-step | |
library(gh) | |
library(purrr) | |
library(data.table) | |
gh_commit_msgs = | |
function(username, author = NULL) { | |
if(is.null(author)) author = username | |
repos = gh("GET /users/:username/repos", username = username, .limit = Inf) | |
repos = map(repos, "name") | |
msgs = | |
map(repos, | |
function(x) { | |
commits = gh("GET /repos/:username/:repo/commits", | |
username = username, author = author, repo = x, .limit = Inf) | |
m = map_chr(map(commits, "commit"), "message") | |
if(length(m)==0) m = NA_character_ | |
data.table(repo = x, msg = m) | |
}) | |
msgs = rbindlist(msgs) | |
msgs = msgs[!is.na(msg)] | |
} | |
## Examples ## | |
## All of my (public) commit messages | |
gmcd = gh_commit_msgs("grantmcdermott") | |
## List in order of most common commit messages | |
gmcd[, .N, by=msg][order(-N)] | |
## By default the function assumes you want commits from the user's own repo(s). | |
## But you can also specify commits to repos belonging to someone else or an | |
## organisation. | |
gh_commit_msgs(username = "uo-ec607", author = "grantmcdermott") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment