Sha256: feb6c6805ff17b335d03bb2ad603c88b0823c012e9689dd8f83d7a88da214206

Contents?: true

Size: 1.86 KB

Versions: 7

Compression:

Stored size: 1.86 KB

Contents

require 'gh'
require 'timeout'

module GH
  # Public: ...
  class MergeCommit < Wrapper
    wraps GH::Normalizer
    double_dispatch

    def setup(backend, options)
      @ssl = options[:ssl]
      super
    end

    def modify_hash(hash)
      setup_lazy_loading(super)
    rescue Exception => error
      raise Error.new(error, hash)
    end

    private

    def lazy_load(hash, key)
      return unless key =~ /^(merge|head|base)_commit$/ and hash.include? 'mergeable'
      return unless force_merge_commit(hash)
      fields = pull_request_refs(hash)
      fields['base_commit'] ||= commit_for hash, hash['base']
      fields['head_commit'] ||= commit_for hash, hash['head']
      fields
    rescue Exception => error
      raise Error.new(error, hash)
    end

    def commit_for(from, hash)
      { 'sha' => hash['sha'], 'ref' => hash['ref'],
        '_links' => { 'self' => { 'href' => git_url_for(from, hash['sha']) } } }
    end

    def git_url_for(hash, commitish)
      hash['_links']['self']['href'].gsub(%r{/pulls/(\d+)$}, "/git/#{commitish}")
    end

    def pull_request_refs(hash)
      link     = git_url_for(hash, 'refs/pull/\1')
      commits  = self[link].map do |data|
        ref    = data['ref']
        name   = ref.split('/').last + "_commit"
        object = data['object'].merge 'ref' => ref
        [name, object]
      end
      Hash[commits]
    end

    def force_merge_commit(hash)
      Timeout.timeout(10) do # MAGIC NUMBERS FTW
        # FIXME: Rick said "this will become part of the API"
        # until then, please look the other way
        while hash['mergeable'].nil?
          url = hash['_links']['html']['href'] + '/mergeable'
          case frontend.http(:get, url).body
          when "true"  then hash['mergeable'] = true
          when "false" then hash['mergeable'] = false
          end
        end
      end
      hash['mergeable']
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
gh-0.6.1 lib/gh/merge_commit.rb
gh-0.6.0 lib/gh/merge_commit.rb
gh-0.5.1 lib/gh/merge_commit.rb
gh-0.5.0 lib/gh/merge_commit.rb
gh-0.4.2 lib/gh/merge_commit.rb
gh-0.4.1 lib/gh/merge_commit.rb
gh-0.4.0 lib/gh/merge_commit.rb