feat: search issue API

This commit is contained in:
Muhsin Keloth
2024-05-09 22:28:47 +05:30
parent df29b43540
commit 5e1dbfb64d
8 changed files with 153 additions and 0 deletions
@@ -49,6 +49,18 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::Bas
end
end
def search_issue
render json: { error: 'Specify search string with parameter q' }, status: :unprocessable_entity if params[:q].blank? && return
term = params[:q]
issues = linear_processor_service.search_issue(term)
if issues.is_a?(Hash) && issues[:error]
render json: { error: issues[:error] }, status: :unprocessable_entity
else
render json: issues, status: :ok
end
end
private
def linear_processor_service
+1
View File
@@ -234,6 +234,7 @@ Rails.application.routes.draw do
post :create_issue
post :link_issue
post :unlink_issue
get :search_issue
end
end
end
@@ -50,6 +50,14 @@ class Integrations::Linear::ProcessorService
}
end
def search_issue(term)
response = linear_client.search_issue(term)
return response if response[:error]
response['searchIssues']['nodes'].map(&:as_json)
end
private
def linear_hook
+6
View File
@@ -29,6 +29,12 @@ class Linear
execute_query(LinearQueries.team_entites_query(team_id))
end
def search_issue(term)
raise ArgumentError, 'Missing search term' if term.blank?
execute_query(LinearQueries.search_issue(term))
end
def create_issue(params)
validate_team_and_title(params)
validate_priority(params[:priority])
+14
View File
@@ -44,4 +44,18 @@ module LinearQueries
}
GRAPHQL
end
def self.search_issue(term)
<<~GRAPHQL
query {
searchIssues(term: "#{term}") {
nodes {
id
title
description
}
}
}
GRAPHQL
end
end
@@ -187,4 +187,36 @@ RSpec.describe 'Linear Integration API', type: :request do
end
end
end
describe 'GET /api/v1/accounts/:account_id/integrations/linear/search_issue' do
let(:term) { 'issue' }
context 'when it is an authenticated user' do
context 'when search is successful' do
let(:search_results) { [{ 'id' => 'issue1', 'title' => 'Sample Issue' }] }
it 'returns search results' do
allow(processor_service).to receive(:search_issue).with(term).and_return(search_results)
get "/api/v1/accounts/#{account.id}/integrations/linear/search_issue",
params: { q: term },
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:ok)
expect(response.body).to include('Sample Issue')
end
end
context 'when search fails' do
it 'returns error message' do
allow(processor_service).to receive(:search_issue).with(term).and_return(error: 'error message')
get "/api/v1/accounts/#{account.id}/integrations/linear/search_issue",
params: { q: term },
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:unprocessable_entity)
expect(response.body).to include('error message')
end
end
end
end
end
@@ -128,4 +128,54 @@ describe Integrations::Linear::ProcessorService do
end
end
end
describe '#unlink_issue' do
let(:link_id) { 'attachment1' }
let(:unlink_response) { { link_id: link_id } }
context 'when Linear client returns valid data' do
it 'returns parsed unlink data' do
allow(linear_client).to receive(:unlink_issue).with(link_id).and_return(unlink_response)
result = service.unlink_issue(link_id)
expect(result).to eq(unlink_response)
end
end
context 'when Linear client returns an error' do
let(:error_response) { { error: 'Some error message' } }
it 'returns the error' do
allow(linear_client).to receive(:unlink_issue).with(link_id).and_return(error_response)
result = service.unlink_issue(link_id)
expect(result).to eq(error_response)
end
end
end
describe '#search_issue' do
let(:term) { 'search term' }
let(:search_response) do
{
'searchIssues' => { 'nodes' => [{ 'id' => 'issue1', 'title' => 'Issue title', 'description' => 'Issue description' }] }
}
end
context 'when Linear client returns valid data' do
it 'returns parsed search data' do
allow(linear_client).to receive(:search_issue).with(term).and_return(search_response)
result = service.search_issue(term)
expect(result).to contain_exactly({ 'id' => 'issue1', 'title' => 'Issue title', 'description' => 'Issue description' })
end
end
context 'when Linear client returns an error' do
let(:error_response) { { error: 'Some error message' } }
it 'returns the error' do
allow(linear_client).to receive(:search_issue).with(term).and_return(error_response)
result = service.search_issue(term)
expect(result).to eq(error_response)
end
end
end
end
+30
View File
@@ -249,4 +249,34 @@ describe Linear do
end
end
end
context 'when querying issues' do
let(:term) { 'term' }
context 'when the API response is success' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 200, body: { data: { searchIssues: { nodes: [{ id: 'issue1', title: 'Title' }] } } }.to_json)
end
it 'returns issues' do
response = linear_client.search_issue(term)
expect(response).to eq({ 'searchIssues' => { 'nodes' => [{ 'id' => 'issue1', 'title' => 'Title' }] } })
end
end
context 'when the API response is an error' do
before do
linear_client.instance_variable_set(:@client, client)
stub_request(:post, url)
.to_return(status: 422, body: { errors: [{ message: 'Error retrieving data' }] }.to_json)
end
it 'raises an exception' do
response = linear_client.search_issue(term)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
end
end
end
end