Skip to content

Instantly share code, notes, and snippets.

@andrewbroman
Created May 26, 2013 07:16
Show Gist options
  • Save andrewbroman/5651961 to your computer and use it in GitHub Desktop.
Save andrewbroman/5651961 to your computer and use it in GitHub Desktop.
=begin
Plugin: Github Logger
Version: 1.1
Description: Logs daily Github activity for the specified user
Author: [Brett Terpstra](http://brettterpstra.com)
Configuration:
github_user: githubuser
github_tags: "#social #coding"
Notes:
=end
# NOTE: Requires json gem
config = {
'description' => ['Logs daily Github activity for the specified user','github_user should be your Github username'],
'github_user' => 'afbroman',
'github_tags' => '#social #coding',
}
$slog.register_plugin({ 'class' => 'GithubLogger', 'config' => config })
class GithubLogger < Slogger
def do_log
if @config.key?(self.class.name)
config = @config[self.class.name]
if !config.key?('github_user') || config['github_user'] == ''
@log.warn("Github user has not been configured or is invalid, please edit your slogger_config file.")
return
end
else
@log.warn("Github user has not been configured, please edit your slogger_config file.")
return
end
@log.info("Logging Github activity for #{config['github_user']}")
begin
url = URI.parse "https://github.com/#{config['github_user'].strip}.json"
http = Net::HTTP.new url.host, url.port
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.use_ssl = true
res = nil
http.start do |agent|
res = agent.get(url.path).read_body
end
rescue Exception => e
@log.error("ERROR retrieving Github url: #{url}")
# p e
end
return false if res.nil?
json = JSON.parse(res)
output = ""
json.each {|action|
date = Time.parse(action['created_at'])
if date > @timespan
case action['type']
when "PushEvent"
if !action["repository"]
action['repository'] = {"name" => "unknown repository"}
end
output += "* Pushed to branch *#{action['payload']['ref'].gsub(/refs\/heads\//,'')}* of [#{action['repository']['name']}](#{action['url']})\n"
action['payload']['shas'].each do |sha|
output += " * #{sha[2].gsub(/\n+/," ")}\n" unless sha.length < 3
end
when "GistEvent"
output += "* Created gist [#{action['payload']['name']}](#{action['payload']['url']})\n"
output += " * #{action['payload']['desc'].gsub(/\n/," ")}\n" unless action['payload']['desc'].nil?
when "WatchEvent"
if action['payload']['action'] == "started"
output += "* Started watching [#{action['repository']['owner']}/#{action['repository']['name']}](#{action['repository']['url']})\n"
output += " * #{action['repository']['description'].gsub(/\n/," ")}\n" unless action['repository']['description'].nil?
end
end
else
break
end
}
return false if output.strip == ""
entry = "## Github activity for #{Time.now.strftime(@date_format)}:\n\n#{output}\n#{config['github_tags']}"
DayOne.new.to_dayone({ 'content' => entry })
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment