diff --git a/app/controllers/api/v1/accounts/integrations/linear_controller.rb b/app/controllers/api/v1/accounts/integrations/linear_controller.rb index 93195e23f..de0d4b0b4 100644 --- a/app/controllers/api/v1/accounts/integrations/linear_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/linear_controller.rb @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 483d87d3b..66c822ebc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -231,6 +231,7 @@ Rails.application.routes.draw do collection do get :teams get :team_entites + post :create_issue end end end diff --git a/lib/integrations/linear/processor_service.rb b/lib/integrations/linear/processor_service.rb index 9df62c5e9..5443ea628 100644 --- a/lib/integrations/linear/processor_service.rb +++ b/lib/integrations/linear/processor_service.rb @@ -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 diff --git a/lib/linear.rb b/lib/linear.rb index 1169b3c9e..c9ffa3839 100644 --- a/lib/linear.rb +++ b/lib/linear.rb @@ -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 } diff --git a/lib/linear_mutations.rb b/lib/linear_mutations.rb new file mode 100644 index 000000000..f2f60f605 --- /dev/null +++ b/lib/linear_mutations.rb @@ -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 diff --git a/spec/lib/linear_spec.rb b/spec/lib/linear_spec.rb index cac08e25f..b758ea806 100644 --- a/spec/lib/linear_spec.rb +++ b/spec/lib/linear_spec.rb @@ -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