feat: unlink issue api

This commit is contained in:
Muhsin Keloth
2024-05-09 21:44:42 +05:30
parent 3e4f43d7da
commit df29b43540
8 changed files with 113 additions and 4 deletions
@@ -38,6 +38,17 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::Bas
end
end
def unlink_issue
link_id = params[:link_id]
issue = linear_processor_service.unlink_issue(link_id)
if issue.is_a?(Hash) && issue[:error]
render json: { error: issue[:error] }, status: :unprocessable_entity
else
render json: issue, status: :ok
end
end
private
def linear_processor_service
@@ -45,6 +56,6 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::Bas
end
def permitted_params
params.permit(:team_id, :link, :issue_id, :title, :description, :assignee_id, :priority, label_ids: [])
params.permit(:team_id, :link, :issue_id, :link_id, :title, :description, :assignee_id, :priority, label_ids: [])
end
end
+1
View File
@@ -233,6 +233,7 @@ Rails.application.routes.draw do
get :team_entites
post :create_issue
post :link_issue
post :unlink_issue
end
end
end
+11 -1
View File
@@ -36,7 +36,17 @@ class Integrations::Linear::ProcessorService
{
id: issue_id,
link: link
link: link,
link_id: response.with_indifferent_access[:attachmentLinkURL][:attachment][:id]
}
end
def unlink_issue(link_id)
response = linear_client.unlink_issue(link_id)
return response if response[:error]
{
link_id: link_id
}
end
+6
View File
@@ -53,6 +53,12 @@ class Linear
execute_mutation(LinearMutations.issue_link(issue_id, link))
end
def unlink_issue(link_id)
raise ArgumentError, 'Missing link id' if link_id.blank?
execute_mutation(LinearMutations.unlink_issue(link_id))
end
private
def validate_team_and_title(params)
+10
View File
@@ -44,4 +44,14 @@ module LinearMutations
}
GRAPHQL
end
def self.unlink_issue(link_id)
<<~GRAPHQL
mutation {
attachmentDelete(id: "#{link_id}") {
success
}
}
GRAPHQL
end
end
@@ -155,4 +155,36 @@ RSpec.describe 'Linear Integration API', type: :request do
end
end
end
describe 'POST /api/v1/accounts/:account_id/integrations/linear/unlink_issue' do
let(:link_id) { 'attachment1' }
context 'when it is an authenticated user' do
context 'when the issue is unlinked successfully' do
let(:unlinked_issue) { { 'id' => 'issue1', 'link' => 'https://linear.app/issue1' } }
it 'returns the unlinked issue' do
allow(processor_service).to receive(:unlink_issue).with(link_id).and_return(unlinked_issue)
post "/api/v1/accounts/#{account.id}/integrations/linear/unlink_issue",
params: { link_id: link_id },
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:ok)
expect(response.body).to include('https://linear.app/issue1')
end
end
context 'when issue unlinking fails' do
it 'returns error message' do
allow(processor_service).to receive(:unlink_issue).with(link_id).and_return(error: 'error message')
post "/api/v1/accounts/#{account.id}/integrations/linear/unlink_issue",
params: { link_id: link_id },
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
@@ -107,11 +107,12 @@ describe Integrations::Linear::ProcessorService do
describe '#link_issue' do
let(:link) { 'https://example.com' }
let(:issue_id) { 'issue1' }
let(:link_response) { { id: issue_id, link: link } }
let(:link_issue_response) { { id: issue_id, link: link, 'attachmentLinkURL': { 'attachment': { 'id': 'attachment1' } } } }
let(:link_response) { { id: issue_id, link: link, link_id: 'attachment1' } }
context 'when Linear client returns valid data' do
it 'returns parsed link data' do
allow(linear_client).to receive(:link_issue).with(link, issue_id).and_return(link_response)
allow(linear_client).to receive(:link_issue).with(link, issue_id).and_return(link_issue_response)
result = service.link_issue(link, issue_id)
expect(result).to eq(link_response)
end
+38
View File
@@ -211,4 +211,42 @@ describe Linear do
end
end
end
context 'when unlinking an issue' do
let(:link_id) { 'attachment1' }
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: { attachmentLinkURL: { id: 'attachment1' } } }.to_json)
end
it 'unlinks an issue' do
response = linear_client.unlink_issue(link_id)
expect(response).to eq({ 'attachmentLinkURL' => { 'id' => 'attachment1' } })
end
context 'when the link_id is missing' do
let(:link_id) { '' }
it 'raises an exception' do
expect { linear_client.unlink_issue(link_id) }.to raise_error(ArgumentError, 'Missing link id')
end
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 unlinking issue' }] }.to_json)
end
it 'raises an exception' do
response = linear_client.unlink_issue(link_id)
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
end
end
end
end