Skip to content

Commit ba6db83

Browse files
Merge pull request #23 from mavenlink/keep_v2_support_for_search
Keep v2 support for search
2 parents c16583f + a7eea86 commit ba6db83

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

lib/jira/resource/issue.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ def self.all(client)
5555
end
5656
end
5757

58+
59+
def self.jql_v2(client, jql, options = {fields: nil, start_at: nil, max_results: nil, expand: nil})
60+
url = client.options[:rest_base_path] + "/search?jql=" + CGI.escape(jql)
61+
62+
url << "&fields=#{options[:fields].map{ |value| CGI.escape(client.Field.name_to_id(value)) }.join(',')}" if options[:fields]
63+
url << "&startAt=#{CGI.escape(options[:start_at].to_s)}" if options[:start_at]
64+
url << "&maxResults=#{CGI.escape(options[:max_results].to_s)}" if options[:max_results]
65+
66+
if options[:expand]
67+
options[:expand] = [options[:expand]] if options[:expand].is_a?(String)
68+
url << "&expand=#{options[:expand].to_a.map{ |value| CGI.escape(value.to_s) }.join(',')}"
69+
end
70+
71+
response = client.get(url)
72+
json = parse_json(response.body)
73+
json['issues'].map do |issue|
74+
client.Issue.build(issue)
75+
end
76+
end
77+
5878
def self.jql(client, jql, options = {fields: nil, next_page_token: nil, max_results: nil, expand: nil})
5979
url = client.options[:rest_base_path_v3] + "/search/jql?jql=" + CGI.escape(jql)
6080

spec/jira/resource/issue_spec.rb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,75 @@ class JIRAResourceDelegation < SimpleDelegator # :nodoc:
133133
expect(JIRA::Resource::Issue.jql(client,'foo bar', expand: %w(transitions))).to eq("issues" =>[""])
134134
end
135135

136+
it "should search an issue with a jql query string for v2 version" do
137+
response = double()
138+
issue = double()
139+
140+
allow(response).to receive(:body).and_return('{"issues": {"key":"foo"}}')
141+
expect(client).to receive(:get).with('/jira/rest/api/2/search?jql=foo+bar').
142+
and_return(response)
143+
expect(client).to receive(:Issue).and_return(issue)
144+
expect(issue).to receive(:build).with(["key", "foo"]).and_return('')
145+
146+
expect(JIRA::Resource::Issue.jql_v2(client,'foo bar')).to eq([''])
147+
end
148+
149+
it "should search an issue with a jql query string and fields for v2 version" do
150+
response = double()
151+
issue = double()
152+
153+
allow(response).to receive(:body).and_return('{"issues": {"key":"foo"}}')
154+
expect(client).to receive(:get)
155+
.with('/jira/rest/api/2/search?jql=foo+bar&fields=foo,bar')
156+
.and_return(response)
157+
expect(client).to receive(:Issue).and_return(issue)
158+
expect(issue).to receive(:build).with(["key", "foo"]).and_return('')
159+
160+
expect(JIRA::Resource::Issue.jql_v2(client, 'foo bar', fields: ['foo','bar'])).to eq([''])
161+
end
162+
163+
it "should search an issue with a jql query string, start at, and maxResults for v2 version" do
164+
response = double()
165+
issue = double()
166+
167+
allow(response).to receive(:body).and_return('{"issues": {"key":"foo"}}')
168+
expect(client).to receive(:get)
169+
.with('/jira/rest/api/2/search?jql=foo+bar&startAt=1&maxResults=3')
170+
.and_return(response)
171+
expect(client).to receive(:Issue).and_return(issue)
172+
expect(issue).to receive(:build).with(["key", "foo"]).and_return('')
173+
174+
expect(JIRA::Resource::Issue.jql_v2(client,'foo bar', start_at: 1, max_results: 3)).to eq([''])
175+
end
176+
177+
it "should search an issue with a jql query string and string expand for v2 version" do
178+
response = double()
179+
issue = double()
180+
181+
allow(response).to receive(:body).and_return('{"issues": {"key":"foo"}}')
182+
expect(client).to receive(:get)
183+
.with('/jira/rest/api/2/search?jql=foo+bar&expand=transitions')
184+
.and_return(response)
185+
expect(client).to receive(:Issue).and_return(issue)
186+
expect(issue).to receive(:build).with(["key", "foo"]).and_return('')
187+
188+
expect(JIRA::Resource::Issue.jql_v2(client,'foo bar', expand: 'transitions')).to eq([''])
189+
end
190+
191+
it "should search an issue with a jql query string and array expand for v2 version" do
192+
response = double()
193+
issue = double()
194+
195+
allow(response).to receive(:body).and_return('{"issues": {"key":"foo"}}')
196+
expect(client).to receive(:get)
197+
.with('/jira/rest/api/2/search?jql=foo+bar&expand=transitions')
198+
.and_return(response)
199+
expect(client).to receive(:Issue).and_return(issue)
200+
expect(issue).to receive(:build).with(["key", "foo"]).and_return('')
201+
202+
expect(JIRA::Resource::Issue.jql_v2(client,'foo bar', expand: %w(transitions))).to eq([''])
203+
end
204+
136205
it 'should return meta data available for editing an issue' do
137206
subject = JIRA::Resource::Issue.new(client, :attrs => {'fields' => {'key' =>'TST=123'}})
138207
response = double()
@@ -222,3 +291,4 @@ class JIRAResourceDelegation < SimpleDelegator # :nodoc:
222291
end
223292
end
224293
end
294+

0 commit comments

Comments
 (0)