I\'m trying build a method in which I can access a Github user name, and publish either all commits or at least a number of commits from that user.
Is there a call to GE
Iterating through a user's repositories is sub-optimal because it misses any commits they make in other repositories. A better way is to use the Events API instead.
The first step is to get the user's events:
GET /users/:username/events
Next you need to iterate through the returned events, checking for items where result.type is set to PushEvent. Each one of these corresponds to a git push
by the user and the commits from that push are available (in reverse chronological order) as result.payload.commits
.
You can filter those to ignore any commits made by other users, by checking that commit.author.email
matches what you expect. You also have access to properties like sha
, message
and url
on that object, and you can eliminate duplicate commits across multiple pushes using the distinct
property.
Overall there is more legwork involved, but it also gives you a far more accurate representation of what a user has actually committed.
In case it helps, here's some example code taken from my website, which uses the above approach to fetch the last commit for a user (implemented using Node.js and the octokat npm module):
const USER = 'TODO: your GitHub user name'
const EMAIL = 'TODO: your GitHub email address'
const github = require('octokat')({ token: 'TODO: your GitHub API token' })
return github.fromUrl(`https://api.github.com/users/${USER}/events`)
.fetch()
.then(events => {
let lastCommit
events.some(event => {
return event.type === 'PushEvent' && event.payload.commits.reverse().some(commit => {
if (commit.author.email === EMAIL) {
lastCommit = {
repo: event.repo.name,
sha: commit.sha,
time: new Date(event.createdAt),
message: commit.message,
url: commit.url
}
return true
}
return false
})
})
return lastCommit
})