【问题标题】:Get lines added/deleted for list of pull requests获取为拉取请求列表添加/删除的行
【发布时间】:2014-01-10 05:13:08
【问题描述】:

假设我有一个拉取请求 ID 列表,例如 gist

如果我只想为每个 ID 设置两个变量:“添加的行”和“删除的行”。如何使用 octokit 为每个拉取请求获取这些变量?

我想我会在 ruby​​ 中这样开始:

require 'octokit'
require 'csv'

list = [2825, 2119, 2629]
output = []

for id in list
   output.push(Octokit.pull_request('rubinius/rubinius', id, options = {}))
 end

begin
   file = File.open("/Users/Username/Desktop/pr_mining_output.txt", "w")
   file.write(output) 
 rescue IOError => e
   #some error occur, dir not writable etc.
 ensure
   file.close unless file == nil
 end

但这似乎只是简单地覆盖了文件并只给我一个结果而不是 3(或者在 list 对象中有很多。我怎样才能让它给我所有 3 的数据?

【问题讨论】:

    标签: ruby git octokit


    【解决方案1】:
    require 'octokit'
    require 'csv'
    
    client = Octokit::Client.new :login => 'mylogin', :password => 'mypass'
    repo = 'rubinius/rubinius'
    numbers = [2825, 2119, 2629]
    
    CSV.open('results.csv', 'w') do |csv|
      for number in numbers
        begin   
          pull = client.pull_request(repo, number)
          csv << [pull.number, pull.additions, pull.deletions]
        rescue Octokit::NotFound
        end
      end    
    end
    

    【讨论】:

    • 这很好用,除非它遇到已转换为问题的拉取请求 ID。然后它给出一个 404 并且不继续。有没有办法确保它继续到列表中的下一个。例如,尝试 numbers = [642, 630, 623] 来复制此错误。
    • @histelheim,处理Octokit::NotFound。查看更新的代码。
    【解决方案2】:
    require 'octokit'
    require 'csv'
    
    client = Octokit::Client.new :login => 'username', :password => 'password'
    repo = 'rubinius/rubinius'
    numbers = CSV.read('/Users/User/Downloads/numbers.csv').flatten
    
    CSV.open('results.csv', 'w') do |csv|
        for number in numbers
          begin
            pull = client.pull_request(repo, number)
            csv << [pull.number, pull.additions, pull.deletions]
          rescue
            csv << [number, 0, 0]
            next
          end
        end
      end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2013-08-21
      相关资源
      最近更新 更多