#!/usr/bin/env ruby

require 'json'
require 'open-uri'
require 'fileutils'
require 'date'

all_releases = open('https://api.github.com/repos/griff/metaz/releases', 'r') do |f|
  JSON.parse(f.read, symbolize_names: true)
end
all_releases.sort! {|a, b| b[:created_at] <=> a[:created_at] }

if ENV['TRAVIS_BRANCH'] != ENV['TRAVIS_TAG']
  release = all_releases.select {|r| !r[:draft] }.first
  prerelease = true
else
  prerelease = false
  release = all_releases.select {|r| !r[:prerelease] && !r[:draft] }.first
  if ENV['TRAVIS_TAG'] == release[:tag_name]
    release = all_releases.select {|r| !r[:prerelease] && !r[:draft] }[1]
  end
end
version=`/usr/libexec/PlistBuddy -c "print :CFBundleShortVersionString" "build/Release/MetaZ.app/Contents/Info.plist"`
log = `git log '--pretty=format:- %s' #{release[:tag_name]}..HEAD`
puts "### Changes in version #{version.strip} (#{Date.today.strftime('%e. %B %Y')})"
puts ''
if prerelease
  puts 'This is an automatic release generated by Travis CI'
puts ''
end
puts 'The complete list of changes is:'
puts ''
puts log

#!/usr/bin/env ruby

require 'json'
require 'open-uri'
require 'fileutils'
require 'date'

all_releases = open('https://api.github.com/repos/griff/metaz/releases', 'r') do |f|
  JSON.parse(f.read, symbolize_names: true)
end
all_releases.sort! {|a, b| b[:created_at] <=> a[:created_at] }

if ENV['TRAVIS_BRANCH'] != ENV['TRAVIS_TAG']
  release = all_releases.select {|r| !r[:draft] }.first
  prerelease = true
else
  prerelease = false
  release = all_releases.select {|r| !r[:prerelease] && !r[:draft] }.first
end
version=`/usr/libexec/PlistBuddy -c "print :CFBundleShortVersionString" "build/Release/MetaZ.app/Contents/Info.plist"`
log = `git log '--pretty=format:- %s' #{release[:tag_name]}..HEAD`
puts "### Changes in version #{version.strip} (#{Date.today.strftime('%e. %B %Y')})"
puts ''
if prerelease
  puts 'This is an automatic release generated by Travis CI'
puts ''
end
puts 'The complete list of changes is:'
puts ''
puts log

--- 
+++ 
@@ -16,6 +16,9 @@
 else
   prerelease = false
   release = all_releases.select {|r| !r[:prerelease] && !r[:draft] }.first
+  if ENV['TRAVIS_TAG'] == release[:tag_name]
+    release = all_releases.select {|r| !r[:prerelease] && !r[:draft] }[1]
+  end
 end
 version=`/usr/libexec/PlistBuddy -c "print :CFBundleShortVersionString" "build/Release/MetaZ.app/Contents/Info.plist"`
 log = `git log '--pretty=format:- %s' #{release[:tag_name]}..HEAD`
Handle release notes when building tag
