Last active
December 2, 2015 21:46
-
-
Save codeout/0c6b5d1f678b1d80266a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'easy_diff' | |
require 'junoser' | |
require 'net/http' | |
require 'rugged' | |
require 'uri' | |
class Tracker | |
def initialize(path, credential, pattern) | |
@repository = Rugged::Repository.new(path) | |
@credential = credential | |
@pattern = pattern | |
end | |
def master | |
hash = extract_tree(@repository.branches['master'].target.tree) | |
extract_hash(nil, hash).each_with_object({}) {|i, hash| | |
hash[i.keys[0]] = i.values[0] | |
} | |
end | |
def origin | |
hash = extract_tree(@repository.branches['origin/master'].target.tree) | |
extract_hash(nil, hash).each_with_object({}) {|i, hash| | |
hash[i.keys[0]] = i.values[0] | |
} | |
end | |
def fetch | |
@repository.remotes['origin'].fetch(credentials: @credential) | |
end | |
def diff | |
str = '' | |
removed, added = master.easy_diff(origin) | |
removed.each do |path, lines| | |
lines.each {|l| str << "- #{path}: #{l}\n" } | |
end | |
added.each do |path, lines| | |
lines.each {|l| str << "+ #{path}: #{l}\n" } | |
end | |
str | |
end | |
def notify(uri_str) | |
message = diff | |
return if diff.empty? | |
uri = URI.parse(uri_str) | |
request = Net::HTTP::Post.new(uri.request_uri) | |
request.body = "連絡先一覧 アップデートした?\n" + message | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.start {|h| h.request(request) } | |
end | |
private | |
def extract_tree(tree) | |
case tree.type | |
when :tree | |
Hash[tree.map {|i| | |
[i[:name], extract_tree(@repository.lookup(i[:oid]))] | |
}] | |
when :blob | |
Junoser::Cli.display_set(tree.content).scan(@pattern) | |
end | |
end | |
def extract_hash(prefix, original) | |
prefix ||= '.' | |
original.map {|key, value| | |
case value | |
when Hash | |
extract_hash("#{prefix}/#{key}", value) | |
when Array | |
{"#{prefix}/#{key}" => value} | |
end | |
}.flatten | |
end | |
end | |
credential = Rugged::Credentials::SshKey.new(username: 'git', privatekey: 'path_to_deploy_secret_key') | |
tracker = Tracker.new('path_to_git_repo', credential, /.*description.*[T|P]:.*/) | |
tracker.fetch | |
tracker.notify('https://codeance.slack.com/services/hooks/slackbot?token=ccvIktQ90VrBP03gzsA6WsJK&channel=%23test') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment