From ecce6e021b43f5bf0cba7baee11e8a397fe47d1f Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Fri, 10 May 2024 15:50:13 +0530 Subject: [PATCH] chore: update link issue API --- .../v1/accounts/integrations/linear_controller.rb | 15 ++++++++++++--- .../integrations/linear_controller_spec.rb | 7 ++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/controllers/api/v1/accounts/integrations/linear_controller.rb b/app/controllers/api/v1/accounts/integrations/linear_controller.rb index 968f164e0..1c8f5f182 100644 --- a/app/controllers/api/v1/accounts/integrations/linear_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/linear_controller.rb @@ -1,4 +1,6 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::BaseController + before_action :fetch_conversation, only: [:link_issue] + def teams teams = linear_processor_service.teams if teams.is_a?(Hash) && teams[:error] @@ -28,9 +30,8 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::Bas end def link_issue - link = params[:link] issue_id = params[:issue_id] - issue = linear_processor_service.link_issue(link, issue_id) + issue = linear_processor_service.link_issue(conversation_link, issue_id) if issue.is_a?(Hash) && issue[:error] render json: { error: issue[:error] }, status: :unprocessable_entity else @@ -76,11 +77,19 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::Bas private + def conversation_link + "#{ENV.fetch('FRONTEND_URL', nil)}/app/accounts/#{Current.account.id}/conversations/#{@conversation.display_id}" + end + + def fetch_conversation + @conversation = Current.account.conversations.find_by!(display_id: permitted_params[:conversation_id]) + end + def linear_processor_service Integrations::Linear::ProcessorService.new(account: Current.account) end def permitted_params - params.permit(:team_id, :link, :issue_id, :link_id, :title, :description, :assignee_id, :priority, label_ids: []) + params.permit(:team_id, :conversation_id, :issue_id, :link_id, :title, :description, :assignee_id, :priority, label_ids: []) end end diff --git a/spec/controllers/api/v1/accounts/integrations/linear_controller_spec.rb b/spec/controllers/api/v1/accounts/integrations/linear_controller_spec.rb index fcf57915c..cda3e6671 100644 --- a/spec/controllers/api/v1/accounts/integrations/linear_controller_spec.rb +++ b/spec/controllers/api/v1/accounts/integrations/linear_controller_spec.rb @@ -124,8 +124,9 @@ RSpec.describe 'Linear Integration API', type: :request do end describe 'POST /api/v1/accounts/:account_id/integrations/linear/link_issue' do - let(:link) { 'https://linear.app/issue1' } let(:issue_id) { 'issue1' } + let(:conversation) { create(:conversation, account: account) } + let(:link) { "#{ENV.fetch('FRONTEND_URL', nil)}/app/accounts/#{account.id}/conversations/#{conversation.display_id}" } context 'when it is an authenticated user' do context 'when the issue is linked successfully' do @@ -134,7 +135,7 @@ RSpec.describe 'Linear Integration API', type: :request do it 'returns the linked issue' do allow(processor_service).to receive(:link_issue).with(link, issue_id).and_return(linked_issue) post "/api/v1/accounts/#{account.id}/integrations/linear/link_issue", - params: { link: link, issue_id: issue_id }, + params: { conversation_id: conversation.display_id, issue_id: issue_id }, headers: agent.create_new_auth_token, as: :json expect(response).to have_http_status(:ok) @@ -146,7 +147,7 @@ RSpec.describe 'Linear Integration API', type: :request do it 'returns error message' do allow(processor_service).to receive(:link_issue).with(link, issue_id).and_return(error: 'error message') post "/api/v1/accounts/#{account.id}/integrations/linear/link_issue", - params: { link: link, issue_id: issue_id }, + params: { conversation_id: conversation.display_id, issue_id: issue_id }, headers: agent.create_new_auth_token, as: :json expect(response).to have_http_status(:unprocessable_entity)