Created
August 22, 2011 15:40
-
-
Save oogali/1162706 to your computer and use it in GitHub Desktop.
JIRA REST API Example w/cookie hackery (should just use curb)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'yaml' | |
require 'json' | |
require 'net/http' | |
require 'uri' | |
class Jira | |
@base | |
@http | |
@cookiejar | |
def initialize(url) | |
uri = URI.parse url | |
@http = Net::HTTP.new uri.host, uri.port | |
@base = File.join(uri.path, 'rest') | |
@cookiejar = {} | |
end | |
def login(username, password) | |
self.post_content '/auth/latest/session', { 'username' => username, 'password' => password }.to_json | |
end | |
def logout | |
self.delete_request '/auth/latest/session' | |
end | |
def projects | |
self.project | |
end | |
def project(key = nil) | |
resp = self.get_content self.api_url('project' + (key ? "/#{key}" : '')) | |
JSON.parse resp | |
end | |
def issues(project) | |
end | |
def issue(key) | |
resp = self.get_content self.api_url("issue/#{key}") | |
JSON.parse resp | |
end | |
def get_content(url = nil) | |
resp = @http.get File.join(@base, url), { 'Cookie' => get_cookies } | |
update_cookies resp['Set-Cookie'] | |
resp.body | |
end | |
def post_content(url, content) | |
resp = @http.post File.join(@base, url), content, { 'Content-Type' => 'application/json', 'Cookie' => get_cookies } | |
update_cookies resp['Set-Cookie'] | |
resp.body | |
end | |
def delete_request(url) | |
resp = @http.delete File.join(@base, url), { 'Cookie' => get_cookies } | |
end | |
def api_url(url) | |
File.join('/api/2.0.alpha1', url) | |
end | |
def update_cookies(jar) | |
return if !jar | |
jar.gsub(/Path=\S+[,]*/, '').split(/;\s+/).each do |cookie| | |
next unless (pair = cookie.split(/\=/)).length == 2 | |
@cookiejar[pair.first] = pair.last | |
end | |
end | |
def get_cookies | |
@cookiejar.map { |x| x * '=' }.join('; ') | |
end | |
end | |
jira = Jira.new ENV['JIRA_URL'] | |
jira.login ENV['JIRA_USER'], ENV['JIRA_PASSWD'] | |
puts jira.issue('SYS-277')['fields']['status'].to_yaml | |
jira.logout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment