diff --git a/app/controllers/api/v1/accounts/integrations/linear_controller.rb b/app/controllers/api/v1/accounts/integrations/linear_controller.rb index 93195e23f..7aa09fc6a 100644 --- a/app/controllers/api/v1/accounts/integrations/linear_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/linear_controller.rb @@ -18,6 +18,18 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::Bas end end + def create_issue + team_id = params[:team_id] + title = params[:title] + description = 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 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..b7b8e9add 100644 --- a/lib/linear.rb +++ b/lib/linear.rb @@ -27,6 +27,14 @@ 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? + + execute_mutation(LinearMutations::ISSUE_CREATE, input: { teamId: team_id, title: title, description: description }) + end + private def execute_query(query) @@ -41,6 +49,12 @@ class Linear log_and_return_error("Unexpected Error: #{e.message}") end + def execute_mutation(query, variables) + response = @client.query(query, variables: variables) + 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..ec62f761c --- /dev/null +++ b/lib/linear_mutations.rb @@ -0,0 +1,15 @@ +module LinearMutations + ISSUE_CREATE = <<~GRAPHQL.freeze + mutation IssueCreate($input: IssueCreateInput!) { + issueCreate( + input: $input + ) { + success + issue { + id + title + } + } + } + GRAPHQL +end