Skip to content

Instantly share code, notes, and snippets.

@srenatus
Last active December 23, 2015 03:59
Show Gist options
  • Save srenatus/6577412 to your computer and use it in GitHub Desktop.
Save srenatus/6577412 to your computer and use it in GitHub Desktop.
Quickly gather news about the cookbooks locked in your Berksfile.lock
$ rake -T
rake news:all                  # cookbook news for all cookbooks
rake news:community            # new versions of community cookbooks
rake news:elasticsearch        # new versions of cookbook elasticsearch
rake news:ntp                  # new versions of cookbook ntp
rake news:osops-utils          # upstream changes for cookbook osops-utils in rcbops-cookbooks/osops-utils
$ rake news:ntp  # no news
$ rake news:elasticsearch
https://community.opscode.com/cookbooks/elasticsearch : 0.3.3, 0.3.2
$ rake news:osops-utils
open https://github.com/rcbops-cookbooks/osops-utils/compare/6b53b1839bdf588d77083191ef266add6c7acba5...master
require 'octokit'
require 'json'
require 'open-uri'
#require 'pp'
# NB
# - get_master_sha fails with some forked repositories
# (it returns the SHA of the forked-off repo (?!) and the repo is thus recognized as having news)
$open = "open" # xdg-open, sensible-browser, ...
def uri_to_gh(uri)
[
/^https:\/\/(www\.)*github\.com\/(?<owner>[\w-]+)\/(?<repo>[\w-]+)\.git$/,
/^git:\/\/(www\.)*github\.com\/(?<owner>[\w-]+)\/(?<repo>[\w-]+)(\.git)*$/,
/^git@github\.com:(?<owner>[\w-]+)\/(?<repo>[\w-]+)\.git$/
].each do |re|
if m = re.match(uri)
return { owner: m[:owner], repo: m[:repo] }
end
end
nil
end
def get_master_sha(repo_hash)
Octokit::Client.new(login: ENV['GH_USER'] || ENV['USER'], password: ENV['GH_PASSWORD'])
.commit(repo_hash, 'master')[:sha]
end
namespace :news do
lockfile = JSON::parse(File.read('Berksfile.lock'))
repos, community = lockfile['sources'].partition {|k,v| v['git']}
gh_repos, other_repos = repos.map {|a| a[1].update( name: a[0], github: uri_to_gh(a[1]['git'])) }#.tap {|x| pp x}
.partition {|a| a[:github]}
puts "Warn: unrecognized repos: #{other_repos.map {|r| "#{r[:name]}: #{r['git']}" }.join ', '}" unless other_repos.empty?
desc 'cookbook news for all cookbooks'
task 'all' => ['community', 'github']
desc 'new versions of community cookbooks'
task 'community' => community.map {|a| a[0] }
community.each do |cb|
name = cb[0]
version = Gem::Version.new cb[1]['locked_version']
desc "new versions of cookbook #{name}"
task name do
cb_data = JSON::parse(open("https://cookbooks.opscode.com/api/v1/cookbooks/#{name}").read)
newer = cb_data['versions'].map {|url| Gem::Version.new(/^.*\/([\d_]+)$/.match(url).captures.first.gsub(/_/,'.')) }
.take_while {|ver| ver > version }
.join ', '
puts "https://community.opscode.com/cookbooks/#{name} : #{newer}" unless newer.empty?
end
end
desc 'upstream changes for all cookbooks from github'
task 'github' => gh_repos.map {|a| a[:name]}
gh_repos.each do |cb|
desc "upstream changes for cookbook #{cb[:name]} in #{cb[:github][:owner]}/#{cb[:github][:repo]}"
task cb[:name] do
begin
master = get_master_sha cb[:github]
rescue
puts "Warn: #{$!}"
end
unless master == cb['ref']
sh "#{$open} https://github.com/#{cb[:github][:owner]}/#{cb[:github][:repo]}/compare/#{cb['ref']}...master"
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment