Skip to content

Instantly share code, notes, and snippets.

@kimh
Last active December 21, 2020 16:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kimh/8894101 to your computer and use it in GitHub Desktop.
Save kimh/8894101 to your computer and use it in GitHub Desktop.
Github News Feed

Preview

preview

Description

Displaying Github new feeds of your organization.

Dependency

gem faraday

Installation

dashing install 8894101

Configuration

Open github_feed.rb and edit user=, org=, and token.

Usage

Add this to your dashboard layout file.

<li data-row="1" data-col="1" data-sizex="2" data-sizey="2">
  <div data-id="github_feed" data-view="GithubFeed" data-title="Github News Feed"></div>
<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">
class Dashing.GithubFeed extends Dashing.Widget
<div class="header">
<i class="icon icon-github"></i>
<h1 class="title" data-bind="title"></h1>
</div>
<li class="content-area" data-foreach-item="items">
<div class="commit-container">
<p class="ago" data-bind="item.ago"></p>
<div class="avatar">
<img data-bind-src="item.avatar_url">
</div>
<div class="gh-feed-title-message">
<i class="gh-feed-title" data-bind="item.title"></i>
<div class="gh-feed-message">
<pre data-bind="item.message"></pre>
</div>
</div>
<div class="clear"></div>
</div>
</li>
# coding: utf-8
require "faraday"
def symbolize_keys(hash)
hash.inject({}){|new_hash, key_value|
key, value = key_value
value = symbolize_keys(value) if value.is_a?(Hash)
new_hash[key.to_sym] = value
new_hash
}
end
class GithubFeed
# Types of events displayed on feed
EVENT_TYPES = ["PushEvent"]
def initialize(user, org, token)
@token = token
@user = user
@org = org
@client = Faraday.new(:url => 'https://api.github.com/')
end
def events
events = json_get("/users/#{@user}/events/orgs/#{@org}")
events.map do |event_json|
event_type = event_json[:type]
Object.const_get(event_type).new(event_json) if EVENT_TYPES.include?(event_type)
end.compact
end
private
def json_get(path)
response = @client.get(path) do |req|
req.headers['Authorization'] = "token #{@token}"
end
json = JSON.parse(response.body)
if json.is_a? Array
json.map {|j| symbolize_keys j}
elsif json.is_a? Hash
symbolize_keys json
else
raise "Only Array or Hash"
end
end
end
class GithubEvent
def initialize(json_data)
@data = json_data
end
def type
@data[:type].downcase.to_sym
end
def date
Time.iso8601(@data[:created_at])
end
def ago
sec = (Time.now - date).floor
if sec < 3600
sec = sec / 60
"#{sec} minutes ago"
elsif sec > 3600 && sec < 86400
hour = sec / 60 / 60
"#{hour} hours ago"
else
day = sec / 60 / 60 / 24
"#{day} days ago"
end
end
end
class PushEvent < GithubEvent
def author
@data[:actor]
end
def repo
@data[:repo][:name]
end
def commit
symbolize_keys @data[:payload][:commits].first
end
def branch
@data[:payload][:ref].split("/").last
end
def title
"#{author[:login]} pushed #{branch} at #{repo}"
end
end
user = "your user name in the organization"
org = "name of your organization"
token = "token"
hist_size = 5
SCHEDULER.every '30s', :first_in => 0 do
feed = GithubFeed.new(user, org, token)
events = feed.events.map do |event|
{
commit_sha: event.commit[:sha],
message: event.commit[:message],
author: event.author[:login],
url: event.commit[:url],
branch: event.branch,
avatar_url: event.author[:avatar_url],
ago: event.ago,
title: event.title,
}
end
send_event('github_feed', {items: events[0..hist_size-1]}) unless events.empty?
end
// ----------------------------------------------------------------------------
// Sass declarations
// ----------------------------------------------------------------------------
$background-color: #eb9c3c;
$title-color: rgba(255, 255, 255, 0.7);
$moreinfo-color: rgba(255, 255, 255, 0.7);
// ----------------------------------------------------------------------------
// Widget-comment styles
// ----------------------------------------------------------------------------
.widget-github-feed {
vertical-align: top !important;
background-color: $background-color;
pre {
white-space: pre-wrap;
word-wrap: break-word;
margin: 0 0 0 0;
}
.header {
.title {
color: $title-color;
}
.name {
padding-left: 5px;
}
}
.commit-container {
padding-bottom: 15px;
font-size: 13px;
.ago {
text-align: left;
color: #e5e5e5;
font-size: 12px;
margin: 2px;
}
.avatar {
float: left;
width: 50px;
height: 50px;
}
.gh-feed-title-message {
padding-left: 10px;
float: left;
vertical-align: top;
width: 450px;
text-align: left;
.gh-feed-title {
margin: 0 0 2px 0;
color: #2E2E2E;
}
.gf-feed-message {
word-wrap:break-all;
}
}
.clear {
clear:both;
}
}
.icon.icon-github {
font-size: 58px;
height: 58px;
width: 66px;
}
.more-info {
color: $moreinfo-color;
}
}
@ajones
Copy link

ajones commented Jul 23, 2014

I am seeing an error when using your feed module

scheduler caught exception:

no implicit conversion of Symbol into Integer
....../github_feed.rb:29:in `[]'

@curphey
Copy link

curphey commented Nov 3, 2014

Same Aaron, did you solve it? If not I will look at it.

@gwilakers
Copy link

I am getting this error when trying to start my app:

scheduler caught exception:
undefined method `inject' for nil:NilClass

It seems to be coming from the github_feed.rb file, on line 6.

Anyone else having this problem or have a solution? Thanks.

@LuRsT
Copy link

LuRsT commented Jan 21, 2015

I'm experiencing the same problem 😞

@alexnewmannn
Copy link

Hey, just noticed this on your gist

<li data-row="1" data-col="1" data-sizex="2" data-sizey="2">
  <div data-id="github_feed" data-view="GithubFeed" data-title="Github News Feed"></div>
<li data-row="1" data-col="1" data-sizex="2" data-sizey="1">

I'm guessing it should be

<li data-row="1" data-col="1" data-sizex="2" data-sizey="2">
  <div data-id="github_feed" data-view="GithubFeed" data-title="Github News Feed"></div>
</li>

:)

@DemitryT
Copy link

@alexnewmann +1, easy to overlook. You should be added as a contributor to make the revision or suggest it straight to the author.

@kitusmark
Copy link

I've managed to set everything up but I'm not seeing any feed. It loads the widget and the title but no github data. I've set up a personal access token with all the permissions and mi user and org and nothing.
Any ideas?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment