Skip to content

Instantly share code, notes, and snippets.

@tgvashworth
Created July 6, 2012 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tgvashworth/3060821 to your computer and use it in GitHub Desktop.
Save tgvashworth/3060821 to your computer and use it in GitHub Desktop.
Tiny GitHub feed for your site
$ () ->
# Github Activity Stream
# Requires jQuery!
# To get going just edit this config to match your username and
# how many items you'd like to display.
# The container element should be a <ul> or <ol>
# Enjoy!
config =
user: 'phuu'
events: 5
container: $('.github')
endpoint: 'https://api.github.com'
html_root: 'https://github.com/'
# No container? Go no furter
return false if config.container.length == 0
# Make the first letter a capital
wordify = (action) ->
action[0].toUpperCase() + action[1..]
# Build a sentence based on the Github event type
lookup = (data) ->
if data.event.payload.action
action = wordify(data.event.payload.action)
if data.event.payload.ref_type
ref_type = data.event.payload.ref_type
ref = if data.event.payload.ref then ' ' + data.event.payload.ref else ''
sentenceFor =
'CommitCommentEvent': () ->
'Commented on commit ' + data.comment.link + ' of ' + data.repo.link
'CreateEvent': () ->
if ref_type == 'branch'
'Created ' + ref_type + ' ' + data.branch.link + ' of ' + data.repo.link
if ref_type == 'repository'
'Created ' + ref_type + ' ' + data.repo.link
else
'Created ' + ref_type + ref + ' of ' + data.repo.link
'DeleteEvent': () ->
'Deleted ' + ref_type + ref + ' of ' + data.repo.link
'DownloadEvent': () ->
'Created new download ' + data.download.link ' for ' + data.repo.link
'FollowEvent': () ->
'Began following ' + data.target.link
'ForkEvent': () ->
'Forked ' + data.repo.link
'GistEvent': () ->
action + 'd gist ' + data.gist.link
'IssueCommentEvent': () ->
'Commented on issue ' + data.issue.link + ' of ' + data.repo.link
'IssuesEvent': () ->
action + ' issue ' + data.issue.link + ' of ' + data.repo.link
'PublicEvent': () ->
'Open sourced ' + data.repo.link + '. Yay!'
'PullRequestEvent': () ->
action + ' pull request ' + data.pull_request.link + ' on ' + data.repo.link
'PullRequestReviewCommentEvent': () ->
'Commented on pull request ' + data.comment.link + ' on ' + data.repo.link
'PushEvent': () ->
'Pushed ' + data.push.link + ' to ' + data.repo.link
'WatchEvent': () ->
action + ' watching ' + data.repo.link
sentenceFor[data.event.type]
# Extract some useful information from the Github JSON
sentenceify = (event) ->
data =
event: event
# Repo information
if event.repo
repo = event.repo
repo.html_url = config.html_root + repo.name
repo.link = '<a href="' + repo.html_url + '">' + repo.name + '</a>'
data.repo = repo
# Branch information
if event.payload.ref_type && event.payload.ref_type == 'branch'
branch = event.payload
branch.html_url = config.html_root + repo.name + '/tree/' + branch.ref
branch.link = '<a href="' + branch.html_url + '">' + branch.ref + '</a>'
data.branch = branch
# Issue info
if event.payload.issue
issue = event.payload.issue
issue.link = '<a href="' + issue.html_url + '">#' + issue.number + '</a>'
data.issue = issue
# Issue Comment info
if event.payload.comment && event.payload.issue
comment = event.payload.comment
comment.link = '<a href="' + comment.html_url + '">#' + issue.numbner + '</a>'
data.comment = comment
# Comment info
if event.payload.comment && ! event.payload.issue && false
comment = event.payload.comment
comment.link = '<a href="' + comment.html_url + '">' + comment.commit_id[..7] + '</a>'
data.comment = comment
# Download info
if event.payload.download
download = event.payload.download
download.link = '<a href="' + download.html_url + '">' + download.name + '</a>'
data.download = download
# User (target) info
if event.payload.target
target = event.payload.target
target.link = '<a href="' + target.html_url + '">' + target.login + '</a>'
data.target = target
# Gist info
if event.payload.gist
gist = event.payload.gist
gist.link = '<a href="' + gist.html_url + '">"' + gist.description + '"</a>'
data.gist = gist
# Pull Request info
if event.payload.pull_request
pull_request = event.payload.pull_request
pull_request.link = '<a href="' + pull_request.html_url + '">#' + pull_request.number + '</a>'
data.pull_request = pull_request
# Push info
if event.payload.push_id
push = event.payload
push.short_head = push.head[..7]
push.html_url = repo.html_url + '/commit/' + push.head
push.link = '<a href="' + push.html_url + '">' + push.short_head + '</a>'
data.push = push
# Grab the right sentence
lookup(data)
# Grab some data from Github
$.getJSON config.endpoint+'/users/'+config.user+'/events/public?callback=?', (data) ->
# Get the most recent 4 events
config.container.empty()
events = data.data[..config.events-1]
for event in events
# Build an li, and add it to the container
sentence = sentenceify event
if sentence
$('<li/>',
'class': 'github-event'
html: sentence
).appendTo config.container
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment