feat: add create issue API
This commit is contained in:
@@ -18,9 +18,25 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::Bas
|
||||
end
|
||||
end
|
||||
|
||||
def create_issue
|
||||
team_id = permitted_params[:team_id]
|
||||
title = permitted_params[:title]
|
||||
description = permitted_params[:description]
|
||||
issue = linear_processor_service.create_issue(team_id, title, description)
|
||||
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
|
||||
Integrations::Linear::ProcessorService.new(account: Current.account)
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.permit(:team_id, :title, :description)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -231,6 +231,7 @@ Rails.application.routes.draw do
|
||||
collection do
|
||||
get :teams
|
||||
get :team_entites
|
||||
post :create_issue
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -20,6 +20,13 @@ class Integrations::Linear::ProcessorService
|
||||
}
|
||||
end
|
||||
|
||||
def create_issue(team_id, title, description)
|
||||
response = linear_client.create_issue(team_id, title, description)
|
||||
return response if response[:error]
|
||||
|
||||
response
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def linear_hook
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
require 'graphlient'
|
||||
|
||||
require_relative 'linear_queries'
|
||||
require_relative 'linear_mutations'
|
||||
class Linear
|
||||
BASE_URL = 'https://api.linear.app/graphql'.freeze
|
||||
|
||||
@@ -27,6 +28,20 @@ class Linear
|
||||
execute_query(LinearQueries.team_entites_query(team_id))
|
||||
end
|
||||
|
||||
def create_issue(team_id, title, description)
|
||||
raise ArgumentError, 'Missing team id' if team_id.blank?
|
||||
raise ArgumentError, 'Missing title' if title.blank?
|
||||
raise ArgumentError, 'Missing description' if description.blank?
|
||||
|
||||
variables = {
|
||||
title: title,
|
||||
description: description,
|
||||
teamId: team_id
|
||||
}
|
||||
|
||||
execute_mutation(LinearMutations.issue_create(variables))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def execute_query(query)
|
||||
@@ -41,6 +56,12 @@ class Linear
|
||||
log_and_return_error("Unexpected Error: #{e.message}")
|
||||
end
|
||||
|
||||
def execute_mutation(query)
|
||||
response = @client.query(query)
|
||||
log_and_return_error("Error creating issue: #{response.errors.messages}") if response.data.nil? && response.errors.any?
|
||||
response.data.to_h
|
||||
end
|
||||
|
||||
def log_and_return_error(message)
|
||||
Rails.logger.error message
|
||||
{ error: message }
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
module LinearMutations
|
||||
def self.issue_create(input)
|
||||
graphql_input = input.map { |key, value| "#{key}: \"#{value}\"" }.join(', ')
|
||||
<<~GRAPHQL
|
||||
mutation {
|
||||
issueCreate(input: { #{graphql_input} }) {
|
||||
success
|
||||
issue {
|
||||
id
|
||||
title
|
||||
}
|
||||
}
|
||||
}
|
||||
GRAPHQL
|
||||
end
|
||||
end
|
||||
@@ -36,7 +36,7 @@ describe Linear do
|
||||
|
||||
it 'raises an exception' do
|
||||
response = linear_client.teams
|
||||
expect(response).to eq({ :error => 'Server Error: the server responded with status 422' })
|
||||
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -76,7 +76,7 @@ describe Linear do
|
||||
|
||||
it 'raises an exception' do
|
||||
response = linear_client.team_entites(team_id)
|
||||
expect(response).to eq({ :error => 'Server Error: the server responded with status 422' })
|
||||
expect(response).to eq({ :error => 'Error: the server responded with status 422' })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user