Skip to content

Instantly share code, notes, and snippets.

@ryanleesipes
Created September 19, 2019 21:29
Show Gist options
  • Save ryanleesipes/dd32a1fec5c2d1264f2e3c7f617c754b to your computer and use it in GitHub Desktop.
Save ryanleesipes/dd32a1fec5c2d1264f2e3c7f617c754b to your computer and use it in GitHub Desktop.
Dashing Widget: Github News Feed (Updated)

Preview

preview

Description

Displaying Github new feeds of your organization.

Dependency

gem faraday

Installation

dashing install 7a346fa63e2aa5b56588

Configuration

Export the following variables in you environment

GITHUB_USER=user
GITHUB_ORG=org
GITHUB_TOKEN=token
GITHUB_URL=http://api.github.com # or private url
GITHUB_HISTSIZE=5 # or however many news items you want to show in the widget

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>
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
@token = ENV['GITHUB_TOKEN']
@user = ENV['GITHUB_USER']
@org = ENV['GITHUB_ORG']
@client = Faraday.new(:url => ENV['GITHUB_URL'])
end
def events
events = json_get("/orgs/#{@org}/events")
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
hist_size = ENV['GITHUB_HISTSIZE'].to_i || 5
SCHEDULER.every '30s', :first_in => 0 do
feed = GithubFeed.new
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: #F66414;
$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-sign{
font-size: 58px;
height: 58px;
width: 66px;
}
.more-info {
color: $moreinfo-color;
}
}

The MIT License (MIT)

Copyright (c) 2016 Levi Smith

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

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