Created
          April 20, 2017 20:15 
        
      - 
      
- 
        Save jwaldrip/23820bc21c3272d022e396c37775625e to your computer and use it in GitHub Desktop. 
    Release code to github
  
        
  
    
      This file contains hidden or 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 'bundler/setup' | |
| require 'date' | |
| require 'net/https' | |
| require 'json' | |
| TRUTHY_STRINGS = %w(t true y yes 1).flat_map do |str| | |
| [str.downcase, str.upcase, str.capitalize] | |
| end.uniq | |
| CreateReleaseError = Class.new(StandardError) | |
| UniqueReleaseError = Class.new(StandardError) | |
| CodeNotMasterError = Class.new(StandardError) | |
| def tag_repo | |
| verify_master! | |
| verify_uniqueness! | |
| count = 0 | |
| uri = URI::HTTPS.build(host: 'api.github.com', path: "/repos/#{owner}/#{repo}/releases") | |
| begin | |
| tag_name = Date.today.strftime("v%Y.%-m.%d.#{count}") | |
| Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| | |
| request = Net::HTTP::Post.new uri | |
| request['Authorization'] = "token #{github_token}" | |
| request.body = JSON.dump( | |
| { | |
| "tag_name": tag_name, | |
| "target_commitish": current_sha, | |
| "name": "Release: #{tag_name}", | |
| "body": "No Details", | |
| "draft": false, | |
| "prerelease": false | |
| } | |
| ) | |
| response = http.request request | |
| raise CreateReleaseError unless Integer(response.code) == 201 | |
| end | |
| rescue CreateReleaseError | |
| count += 1 | |
| retry | |
| end | |
| end | |
| def remote_url | |
| @remote_url ||= `git config --get remote.origin.url`.strip | |
| end | |
| def remote_matches | |
| @remote_matches ||= remote_url.match(/.*@.*:(?<owner>.*)\/(?<repo>.*)\.git/) || | |
| remote_url.match(/(ssh|https):\/\/.*@.*\/(?<owner>.*)\/(?<repo>.*)\.git/) | |
| end | |
| def owner | |
| @owner ||= remote_matches[:owner] | |
| end | |
| def repo | |
| @repo ||= remote_matches[:repo] | |
| end | |
| def github_token | |
| ENV.fetch('GITHUB_TOKEN') | |
| end | |
| def current_sha | |
| @sha ||= `git rev-parse HEAD`.strip | |
| end | |
| def verify_master! | |
| raise CodeNotMasterError, 'Code is not in master!' unless system "git merge-base --is-ancestor #{current_sha} master" | |
| end | |
| def verify_uniqueness! | |
| uri = URI::HTTPS.build(host: 'api.github.com', path: "/repos/#{owner}/#{repo}/releases") | |
| Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http| | |
| request = Net::HTTP::Get.new uri | |
| request['Authorization'] = "token #{github_token}" | |
| response = http.request request | |
| raise UniqueReleaseError, 'Code has already been released!' if JSON.load(response.body).any? { |release| release['target_commitish'] == current_sha } | |
| end | |
| end | |
| # Tag it! | |
| tag_repo | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment