PageRenderTime 37ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/pullrequests

https://bitbucket.org/osrf/bitbucket-client-tools
Ruby | 70 lines | 46 code | 9 blank | 15 comment | 5 complexity | 2afebc8be5ea27f7ad43fbade362d0a8 MD5 | raw file
  1. #!/usr/bin/env ruby
  2. # based on http://www.alphadevx.com/a/88-Writing-a-REST-Client-in-Ruby
  3. require 'rubygems'
  4. require 'rest_client'
  5. require 'json'
  6. class BitbucketPullRequests
  7. # URL for bitbucket 2.0 api
  8. @url_prefix
  9. @url_suffix
  10. # constructor
  11. def initialize
  12. @url_prefix = 'https://bitbucket.org/api/2.0/repositories/'
  13. @url_suffix = '/pullrequests'
  14. end
  15. #output += jsonHash["size"].to_s + " open pull requests: "+"\n"
  16. #fields = ["id", "branch\t", "commit"]
  17. #fields.each { |field|
  18. # output += field + "\t"
  19. #}
  20. #output += "\n"
  21. #pr.keys.each { |key|
  22. # output += " "+key+"\n"
  23. #}
  24. # summary of open pull requests
  25. def getPullRequestsSummary(owner, repository)
  26. url = @url_prefix + owner + "/" + repository + @url_suffix
  27. response = RestClient.get(url)
  28. jsonHash = JSON.parse(response.body)
  29. output = ""
  30. jsonHash["values"].each { |pr|
  31. output += pr["id"].to_s
  32. output += "\t"+pr["source"]["commit"]["sha"]
  33. output += "\t"+pr["source"]["branch"]["name"]
  34. output += "\n"
  35. }
  36. return output
  37. end
  38. # list of files changed by pull request
  39. def getPullRequestDetail(owner, repository, id)
  40. url = @url_prefix + owner + "/" + repository + @url_suffix + "/" + id.to_s + "/patch"
  41. response = RestClient.get(url)
  42. output = ""
  43. response.lines.map(&:chomp).each do |line|
  44. if line.include? '+++'
  45. line["+++ b/"] = ""
  46. output += line + "\n"
  47. end
  48. end
  49. return output
  50. end
  51. end
  52. client = BitbucketPullRequests.new
  53. if ARGV.length == 2
  54. puts client.getPullRequestsSummary(ARGV[0], ARGV[1])
  55. elsif ARGV.length == 3
  56. puts client.getPullRequestDetail(ARGV[0], ARGV[1], ARGV[2].to_i)
  57. elsif ARGV.length == 1
  58. puts client.getPullRequestDetail("osrf", "gazebo", ARGV[0].to_i)
  59. else
  60. puts client.getPullRequestsSummary("osrf", "gazebo")
  61. end