Guest User

Untitled

a guest
Jan 23rd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require 'rubygems'
  4. require 'yaml'
  5. require 'json'
  6. require 'net/http'
  7. require 'uri'
  8.  
  9. class Jira
  10. @base
  11. @http
  12. @cookiejar
  13.  
  14. def initialize(url)
  15. uri = URI.parse url
  16. @http = Net::HTTP.new uri.host, uri.port
  17. @base = File.join(uri.path, 'rest')
  18. @cookiejar = {}
  19. end
  20.  
  21. def login(username, password)
  22. self.post_content '/auth/latest/session', { 'username' => username, 'password' => password }.to_json
  23. end
  24.  
  25. def logout
  26. self.delete_request '/auth/latest/session'
  27. end
  28.  
  29. def projects
  30. self.project
  31. end
  32.  
  33. def project(key = nil)
  34. resp = self.get_content self.api_url('project' + (key ? "/#{key}" : ''))
  35. JSON.parse resp
  36. end
  37.  
  38. def issues(project)
  39. end
  40.  
  41. def issue(key)
  42. resp = self.get_content self.api_url("issue/#{key}")
  43. JSON.parse resp
  44. end
  45.  
  46. def get_content(url = nil)
  47. resp = @http.get File.join(@base, url), { 'Cookie' => get_cookies }
  48. update_cookies resp['Set-Cookie']
  49. resp.body
  50. end
  51.  
  52. def post_content(url, content)
  53. resp = @http.post File.join(@base, url), content, { 'Content-Type' => 'application/json', 'Cookie' => get_cookies }
  54. update_cookies resp['Set-Cookie']
  55. resp.body
  56. end
  57.  
  58. def delete_request(url)
  59. resp = @http.delete File.join(@base, url), { 'Cookie' => get_cookies }
  60. end
  61.  
  62. def api_url(url)
  63. File.join('/api/2.0.alpha1', url)
  64. end
  65.  
  66. def update_cookies(jar)
  67. return if !jar
  68. jar.gsub(/Path=\S+[,]*/, '').split(/;\s+/).each do |cookie|
  69. next unless (pair = cookie.split(/\=/)).length == 2
  70. @cookiejar[pair.first] = pair.last
  71. end
  72. end
  73.  
  74. def get_cookies
  75. @cookiejar.map { |x| x * '=' }.join('; ')
  76. end
  77. end
  78.  
  79. jira = Jira.new ENV['JIRA_URL']
  80. jira.login ENV['JIRA_USER'], ENV['JIRA_PASSWD']
  81. puts jira.issue('SYS-277')['fields']['status'].to_yaml
  82. jira.logout
Add Comment
Please, Sign In to add comment