bin/pull_req_data_extraction.rb
# Load a commit from Github. Will return an empty hash if the commit does not exist.
Load a commit from Github. Will return an empty hash if the commit does not exist.
Load a commit from Github . Will return an empty hash if the commit does not exist .
def github_commit(owner, repo, sha)
    parent_dir = File.join('commits', "#{owner}@#{repo}")
    commit_json = File.join(parent_dir, "#{sha}.json")
    FileUtils::mkdir_p(parent_dir)
    r = nil
    if File.exists? commit_json
      r = begin
        JSON.parse File.open(commit_json).read
      rescue
        {}
      end
      return r
    end
    url = "https://api.github.com/repos/#{owner}/#{repo}/commits/#{sha}"
    log("Requesting #{url} (#{@remaining} remaining)")
    contents = nil
    begin
      r = open(url, 'User-Agent' => 'ghtorrent', 'Authorization' => "token #{token}")
      @remaining = r.meta['x-ratelimit-remaining'].to_i
      @reset = r.meta['x-ratelimit-reset'].to_i
      contents = r.read
      JSON.parse contents
    rescue OpenURI::HTTPError => e
      @remaining = e.io.meta['x-ratelimit-remaining'].to_i
      @reset = e.io.meta['x-ratelimit-reset'].to_i
      log "Cannot get #{url}. Error #{e.io.status[0].to_i}"
      {}
    rescue StandardError => e
      log "Cannot get #{url}. General error: #{e.message}"
      {}
    ensure
      File.open(commit_json, 'w') do |f|
        f.write contents unless r.nil?
        f.write '' if r.nil?
      end
      if 5000 - @remaining >= REQ_LIMIT
        to_sleep = @reset - Time.now.to_i + 2
        log "Request limit reached, sleeping for #{to_sleep} secs"
        sleep(to_sleep)
      end
    end
  end
def github_commit ( owner , repo , sha ) parent_dir = File . join ( 'commits' , "#{owner}@#{repo}" ) commit_json = File . join ( parent_dir , "#{sha}.json" ) FileUtils :: mkdir_p ( parent_dir ) r = nil if File . exists? commit_json r = begin JSON . parse File . open ( commit_json ) . read rescue { } end return r end url = "https://api.github.com/repos/#{owner}/#{repo}/commits/#{sha}" log ( "Requesting #{url} (#{@remaining} remaining)" ) contents = nil begin r = open ( url , 'User-Agent' => 'ghtorrent' , 'Authorization' => "token #{token}" ) @remaining = r . meta [ 'x-ratelimit-remaining' ] . to_i @reset = r . meta [ 'x-ratelimit-reset' ] . to_i contents = r . read JSON . parse contents rescue OpenURI :: HTTPError => e @remaining = e . io . meta [ 'x-ratelimit-remaining' ] . to_i @reset = e . io . meta [ 'x-ratelimit-reset' ] . to_i log "Cannot get #{url}. Error #{e.io.status[0].to_i}" { } rescue StandardError => e log "Cannot get #{url}. General error: #{e.message}" { } ensure File . open ( commit_json , 'w' ) do | f | f . write contents unless r . nil? f . write '' if r . nil? end if 5000 - @remaining >= REQ_LIMIT to_sleep = @reset - Time . now . to_i + 2 log "Request limit reached, sleeping for #{to_sleep} secs" sleep ( to_sleep ) end end end
Load a commit from Github.
Load a commit from Github .
# This means that the retrieval operation resulted in no commit being retrieved
owner
repo
sha