github_release.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #!/usr/bin/env ruby
  2. require 'pathname'
  3. require 'octokit'
  4. raise 'usage: github_release.rb version' unless ARGV.length == 1
  5. VERSION = ARGV[0]
  6. ACCESS_TOKEN = ENV['GITHUB_ACCESS_TOKEN']
  7. raise 'GITHUB_ACCESS_TOKEN must be set to create GitHub releases' unless ACCESS_TOKEN
  8. BUILD_SH = Pathname(__FILE__).+('../../build.sh').expand_path
  9. RELEASE = "v#{VERSION}"
  10. REPOSITORY = 'realm/realm-swift'
  11. def release_notes(version)
  12. changelog = BUILD_SH.parent.+('CHANGELOG.md').readlines
  13. current_version_index = changelog.find_index { |line| line =~ (/^#{Regexp.escape version}/) }
  14. unless current_version_index
  15. raise "Update the changelog for the last version (#{version})"
  16. end
  17. current_version_index += 2
  18. previous_version_lines = changelog[(current_version_index+1)...-1]
  19. previous_version_index = current_version_index + (previous_version_lines.find_index { |line| line =~ /^\d+\.\d+\.\d+(-(alpha|beta|rc)(\.\d+)?)?\s+/ } || changelog.count)
  20. relevant = changelog[current_version_index..previous_version_index]
  21. relevant.join.strip
  22. end
  23. RELEASE_NOTES = release_notes(VERSION)
  24. github = Octokit::Client.new
  25. github.access_token = ENV['GITHUB_ACCESS_TOKEN']
  26. puts 'Creating GitHub release'
  27. prerelease = (VERSION =~ /alpha|beta|rc|preview/) ? true : false
  28. response = github.create_release(REPOSITORY, RELEASE, name: RELEASE, body: RELEASE_NOTES, prerelease: prerelease)
  29. release_url = response[:url]
  30. Dir.glob 'build/*.zip' do |upload|
  31. puts "Uploading #{upload} to GitHub"
  32. github.upload_asset(release_url, upload, content_type: 'application/zip')
  33. end