Skip to content

Commit

Permalink
refactor jql builder
Browse files Browse the repository at this point in the history
  • Loading branch information
keepcosmos committed Dec 12, 2016
1 parent 6a17c93 commit ec2ae0a
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 11 deletions.
4 changes: 2 additions & 2 deletions lib/terjira/client/base.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
require_relative 'jql_query_builer'
require_relative 'jql_builder'
require_relative 'auth_option_builder'

module Terjira
module Client
# Abstract class to delegate jira-ruby resource class
class Base
extend JQLQueryBuilder
extend JQLBuilder
extend AuthOptionBuilder

DEFAULT_CACHE_SEC = 60
Expand Down
6 changes: 1 addition & 5 deletions lib/terjira/client/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,14 @@ class << self
def all(options = {})
return resource.all if options.blank?
max_results = options.delete(:max_results) || 500
resource.jql(build_jql_query(options), max_results: max_results)
resource.jql(build_jql(options), max_results: max_results)
end

def find(issue, options = {})
resp = api_get("issue/#{issue.key_value}", options)
build(resp)
end

def current_my_issues
jql("assignee = #{key_value} AND statusCategory != 'Done'")
end

def assign(issue, assignee)
body = { name: assignee.key_value }.to_json
api_put("issue/#{issue.key_value}/assignee", body)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module Terjira
module Client
module JQLQueryBuilder
module JQLBuilder
JQL_KEYS = %w(board sprint assignee issuetype priority project status statusCategory).freeze

def build_jql_query(options = {})
def build_jql(options = {})
q_options = options.inject({}) do |memo, (k, v)|
memo[k.to_s] = v
memo
Expand Down
1 change: 1 addition & 0 deletions lib/terjira/sprint_cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ def show(sprint = nil)
def active
opts = suggest_options(required: [:board])
board = opts[:board]

sprints = client_class.find_active(board)

opts[:assignee] ||= current_username
Expand Down
37 changes: 37 additions & 0 deletions spec/client/jql_builder_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require 'spec_helper'
require 'terjira/client/jql_builder'

class TestJQLBuilder
extend Terjira::Client::JQLBuilder
end

describe Terjira::Client::JQLBuilder do
subject { TestJQLBuilder }

it 'builds string value jql' do
result = subject.build_jql(board: 1)
expect(result).to be == 'board=1'

result = subject.build_jql(issuetype: 'Task')
expect(result).to be == 'issuetype=Task'
end

it 'builds array value jql' do
result = subject.build_jql(sprint: [1, 2, 3])
expect(result).to be == 'sprint IN ("1","2","3")'

result = subject.build_jql(priority: %w(high low))
expect(result).to be == 'priority IN ("high","low")'
end

it 'builds multiple key values jql' do
result = subject.build_jql(sprint: 1, issuetype: %w(Task Done))

expect(result).to be == 'sprint=1 AND issuetype IN ("Task","Done")'
end

it 'filters unkown jql key' do
result = subject.build_jql(unkown: 1)
expect(result).to be == ''
end
end
26 changes: 24 additions & 2 deletions spec/sprint_cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
end

context '#list' do
it 'must show sprints' do
it 'presents sprints' do
allow(Terjira::Client::Board).to receive(:all).and_return(scrum_boards)
allow(Terjira::Client::Sprint).to receive(:all).and_return(sprints)

Expand All @@ -31,7 +31,7 @@
end

context '#show' do
it 'must sprint with issues' do
it 'presents the sprint with issues' do
sprint = sprints.first
allow(Terjira::Client::Sprint).to receive(:find).and_return(sprint)
allow(Terjira::Client::Issue).to receive(:all).and_return(issues)
Expand All @@ -47,4 +47,26 @@
end
end
end

context '#active' do
it 'presents active sprints with issues' do
board = boards.first
allow(Terjira::Client::Sprint).to receive(:find_active).with(board.id).and_return(sprints)
allow(Terjira::Client::Issue).to receive(:all).and_return(issues)

result = capture(:stdout) do
described_class.start ['active', '--board', board.id]
end

sprints.each do |sprint|
expect(result).to be_include sprint.id.to_s
expect(result).to be_include sprint.name
expect(result).to be_include sprint.goal
end
issues.each do |issue|
expect(result).to be_include issue.key
expect(result).to be_include issue.summary
end
end
end
end

0 comments on commit ec2ae0a

Please sign in to comment.