jiralicious 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
6
+ *.swo
7
+ TAGS
8
+ *~
9
+ \#*#
10
+ *.#*
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use 1.9.2@jiralicious --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in jiralicious.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Jason Stewart
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,21 @@
1
+ = jiralicious
2
+
3
+ Description goes here.
4
+
5
+ TODO: Add something useful here.
6
+
7
+ == Contributing to jiralicious
8
+
9
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
10
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
11
+ * Fork the project
12
+ * Start a feature/bugfix branch
13
+ * Commit and push until you are happy with your contribution
14
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
15
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
16
+
17
+ == Copyright
18
+
19
+ Copyright (c) 2010 Jason Stewart. See LICENSE for
20
+ further details.
21
+
data/Rakefile ADDED
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ RSpec::Core::RakeTask.new(:spec) do |spec|
8
+ spec.pattern = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
12
+ spec.pattern = 'spec/**/*_spec.rb'
13
+ spec.rcov = true
14
+ end
15
+
16
+ task :default => :spec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "jiralicious/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "jiralicious"
7
+ s.version = Jiralicious::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.homepage = "http://github.com/jstewart/jiralicious"
10
+ s.license = "MIT"
11
+ s.summary = %Q{A Ruby library for interacting with JIRA's REST API}
12
+ s.description = %Q{A Ruby library for interacting with JIRA's REST API}
13
+ s.email = "jstewart@fusionary.com"
14
+ s.authors = ["Jason Stewart"]
15
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
16
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
17
+ s.add_runtime_dependency 'httparty', '~>0.7.8'
18
+ s.add_runtime_dependency 'hashie', '~>1.1.0'
19
+ s.add_runtime_dependency 'json', '~>1.6.3'
20
+ s.add_development_dependency 'rspec', '~>2.6.0'
21
+ s.add_development_dependency 'fakeweb', '~>1.3.0'
22
+
23
+ s.files = `git ls-files`.split("\n")
24
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
25
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
26
+ s.require_paths = ["lib"]
27
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+
3
+ require 'hashie'
4
+ require 'httparty'
5
+ require 'json'
6
+
7
+
8
+ require 'jiralicious/parsers/field_parser'
9
+ require 'jiralicious/errors'
10
+ require 'jiralicious/issue'
11
+ require 'jiralicious/search'
12
+ require 'jiralicious/search_result'
13
+ require 'jiralicious/session'
14
+
15
+
16
+ module Jiralicious
17
+ extend Configuration
18
+ extend self
19
+
20
+ def session
21
+ @session ||= Session.new
22
+ end
23
+
24
+ def rest_path
25
+ "#{self.uri}/rest/api/#{self.api_version}"
26
+ end
27
+ end
@@ -0,0 +1,35 @@
1
+ # encoding: utf-8
2
+
3
+ module Jiralicious
4
+ module Configuration
5
+ VALID_OPTIONS = [:username, :password, :uri, :api_version]
6
+ DEFAULT_USERNAME = nil
7
+ DEFAULT_PASSWORD = nil
8
+ DEFAULT_URI = nil
9
+ DEFAULT_API_VERSION = "latest"
10
+
11
+ def configure
12
+ yield self
13
+ end
14
+
15
+ attr_accessor *VALID_OPTIONS
16
+
17
+ # Reset when extended into class
18
+ def self.extended(base)
19
+ base.reset
20
+ end
21
+
22
+ def options
23
+ VALID_OPTIONS.inject({}) do |option, key|
24
+ option.merge!(key => send(key))
25
+ end
26
+ end
27
+
28
+ def reset
29
+ self.username = DEFAULT_USERNAME
30
+ self.password = DEFAULT_PASSWORD
31
+ self.uri = DEFAULT_URI
32
+ self.api_version = DEFAULT_API_VERSION
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,17 @@
1
+ # encoding: utf-8
2
+
3
+ module Jiralicious
4
+ class JiraError < StandardError; end
5
+ class AuthenticationError < StandardError; end
6
+ class NotLoggedIn < AuthenticationError; end
7
+ class InvalidLogin < AuthenticationError; end
8
+
9
+ # These are in the JIRA API docs. Not sure about specifics, as the docs don't
10
+ # mention them. Added here for completeness and future implementation.
11
+ # http://confluence.atlassian.com/display/JIRA/JIRA+REST+API+%28Alpha%29+Tutorial
12
+ class CookieExpired < AuthenticationError; end
13
+ class CaptchaRequired < AuthenticationError; end
14
+ class IssueNotFound < StandardError; end
15
+ class JqlError < StandardError; end
16
+ class TransitionError < StandardError; end
17
+ end
@@ -0,0 +1,56 @@
1
+ # encoding: utf-8
2
+ module Jiralicious
3
+ class Issue < Hashie::Trash
4
+ include Jiralicious::Parsers::FieldParser
5
+
6
+ property :jira_key, :from => :key
7
+ property :expand
8
+ property :jira_self, :from => :self
9
+ property :fields
10
+ property :transitions
11
+
12
+ def initialize(decoded_json, default = nil, &blk)
13
+ super(decoded_json)
14
+ parse!(decoded_json["fields"])
15
+ end
16
+
17
+ def self.find(key, options = {})
18
+ response = Jiralicious.session.perform_request do
19
+ Jiralicious::Session.get("#{Jiralicious.rest_path}/issue/#{key}")
20
+ end
21
+
22
+ if response.code == 200
23
+ response = JSON.parse(response.body)
24
+ else
25
+ raise Jiralicious::IssueNotFound
26
+ end
27
+
28
+ new(response)
29
+ end
30
+
31
+ def self.get_transitions(transitions_url)
32
+ response = Jiralicious.session.perform_request do
33
+ Jiralicious::Session.get(transitions_url)
34
+ end
35
+ JSON.parse(response.body)
36
+ end
37
+
38
+ def self.transition(transitions_url, data)
39
+ response = Jiralicious.session.perform_request do
40
+ Jiralicious::Session.post(transitions_url, :body => data.to_json)
41
+ end
42
+
43
+ case response.code
44
+ when 204
45
+ response.body
46
+ when 400
47
+ error = JSON.parse(response.body)
48
+ raise Jiralicious::TransitionError.new(error['errorMessages'].join('\n'))
49
+ when 404
50
+ error = JSON.parse(response.body)
51
+ raise Jiralicious::IssueNotFound.new(error['errorMessages'].join('\n'))
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,43 @@
1
+ # encoding: utf-8
2
+ module Jiralicious
3
+ module Parsers
4
+ module FieldParser
5
+ def parse!(fields)
6
+ unless fields.is_a?(Hash)
7
+ raise ArgumentError
8
+ end
9
+ @jiralicious_field_parser_data = {}
10
+ singleton = class << self; self end
11
+
12
+ fields.each do |field, details|
13
+ next if details["name"].nil?
14
+ method_value = mashify(details["value"])
15
+ method_name = details["name"].gsub(/(\w+)([A-Z].*)/, '\1_\2').
16
+ gsub(/\W/, "_").
17
+ downcase
18
+
19
+ if singleton.method_defined?(method_name)
20
+ method_name = "jira_#{method_name}"
21
+ end
22
+
23
+ @jiralicious_field_parser_data[method_name] = method_value
24
+ singleton.send :define_method, method_name do
25
+ @jiralicious_field_parser_data[method_name]
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def mashify(data)
33
+ if data.is_a?(Array)
34
+ data.map { |d| mashify(d) }
35
+ elsif data.is_a?(Hash)
36
+ Hashie::Mash.new(data)
37
+ else
38
+ data
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,26 @@
1
+ # encoding: utf-8
2
+ module Jiralicious
3
+ def search(jql, options = {})
4
+ options[:start_at] ||= 0
5
+ options[:max_results] ||= 50
6
+
7
+ request_body = {
8
+ :jql => jql,
9
+ :startAt => options[:start_at],
10
+ :maxResults => options[:max_results]
11
+ }.to_json
12
+
13
+ response = Jiralicious.session.perform_request do
14
+ Jiralicious::Session.post(
15
+ "#{Jiralicious.rest_path}/search",
16
+ :body => request_body
17
+ )
18
+ end
19
+
20
+ if response.code == 200
21
+ Jiralicious::SearchResult.new(JSON.parse(response.body))
22
+ else
23
+ raise Jiralicious::JqlError
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,17 @@
1
+ module Jiralicious
2
+ class SearchResult
3
+ attr_reader :offset, :num_results
4
+
5
+ def initialize(search_data)
6
+ @issues = search_data["issues"]
7
+ @offset = search_data["startAt"]
8
+ @num_results = search_data["total"]
9
+ end
10
+
11
+ def issues
12
+ @issues.map do |issue|
13
+ Jiralicious::Issue.find(issue["key"])
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,100 @@
1
+ # encoding: utf-8
2
+ require 'jiralicious/configuration'
3
+
4
+ module Jiralicious
5
+ class Session
6
+ include HTTParty
7
+ attr_accessor :session, :login_info
8
+ headers 'Content-Type' => 'application/json'
9
+
10
+ def alive?
11
+ @session && @login_info
12
+ end
13
+
14
+ def login
15
+ response = perform_request(:authenticating => true) do
16
+ self.class.post('/rest/auth/latest/session',
17
+ :body => {
18
+ :username => Jiralicious.username,
19
+ :password => Jiralicious.password}.to_json)
20
+ end
21
+
22
+ if response.code == 200
23
+ response = JSON.parse(response.body)
24
+ @session = response["session"]
25
+ @login_info = response["loginInfo"]
26
+ self.class.cookies({self.session["name"] => self.session["value"]})
27
+ else
28
+ clear_session
29
+ case response.code
30
+ when 401 then
31
+ raise Jiralicious::InvalidLogin.new("Invalid login")
32
+ when 403
33
+ raise Jiralicious::CaptchaRequired.new("Captacha is required. Try logging into Jira via the web interface")
34
+ else
35
+ # Give Net::HTTP reason
36
+ raise Jiralicious::JiraError.new(response.response.message)
37
+ end
38
+ end
39
+ end
40
+
41
+ def logout
42
+ response = perform_request do
43
+ self.class.delete('/rest/auth/latest/session')
44
+ end
45
+
46
+ if response.code == 204
47
+ clear_session
48
+ else
49
+ case response.code
50
+ when 401 then
51
+ raise Jiralicious::NotLoggedIn.new("Not logged in")
52
+ else
53
+ # Give Net::HTTP reason
54
+ raise Jiralicious::JiraError.new(response.response.message)
55
+ end
56
+ end
57
+ end
58
+
59
+ def perform_request(options = {}, &block)
60
+ self.class.base_uri Jiralicious.uri
61
+ self.login if require_login? && !options[:authenticating]
62
+
63
+ response = block.call
64
+ unless options[:authenticating]
65
+ if captcha_required(response)
66
+ raise Jiralicious::CaptchaRequired.
67
+ new("Captacha is required. Try logging into Jira via the web interface")
68
+ elsif cookie_invalid(response)
69
+ # Can usually be fixed by logging in again
70
+ clear_session
71
+ self.login
72
+ response = block.call
73
+ raise Jiralicious::CookieExpired if cookie_invalid(response)
74
+ end
75
+ end
76
+ response
77
+ end
78
+
79
+ private
80
+
81
+ def captcha_required(response)
82
+ response.code == 401 &&
83
+ # Fakeweb lowercases headers automatically. :(
84
+ (response.headers["X-Seraph-LoginReason"] == "AUTHENTICATION_DENIED" ||
85
+ response.headers["x-seraph-loginreason"] == "AUTHENTICATION_DENIED")
86
+ end
87
+
88
+ def cookie_invalid(response)
89
+ response.code == 401 && response.body =~ /cookie/i
90
+ end
91
+
92
+ def require_login?
93
+ !(Jiralicious.username.empty? && Jiralicious.password.empty?) && !alive?
94
+ end
95
+
96
+ def clear_session
97
+ @session = @login_info = nil
98
+ end
99
+ end
100
+ end