From 0fafe376466d6915f66dd896169d26a17a4fc4cb Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 2 May 2024 16:45:57 +0530 Subject: [PATCH] chore: code cleanup --- .../integrations/linear_controller.rb | 22 +++++++++++++++---- lib/linear.rb | 11 +++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/accounts/integrations/linear_controller.rb b/app/controllers/api/v1/accounts/integrations/linear_controller.rb index f2f8792dc..9a8520d7c 100644 --- a/app/controllers/api/v1/accounts/integrations/linear_controller.rb +++ b/app/controllers/api/v1/accounts/integrations/linear_controller.rb @@ -1,9 +1,23 @@ class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::BaseController - before_action :fetch_conversation, only: [:list] + # before_action :fetch_conversation, only: [:list] - def list - issues = linear_processor_service.list_issues - render json: issues, status: :ok + def teams + teams = linear_processor_service.teams + if teams.is_a?(Hash) && teams[:error] + render json: { error: teams[:error] }, status: :unprocessable_entity + else + render json: teams, status: :ok + end + end + + def labels + team_id = params[:team_id] + labels = linear_processor_service.labels(team_id) + if labels.is_a?(Hash) && labels[:error] + render json: { error: labels[:error] }, status: :unprocessable_entity + else + render json: labels, status: :ok + end end private diff --git a/lib/linear.rb b/lib/linear.rb index e8a2031af..e231896d3 100644 --- a/lib/linear.rb +++ b/lib/linear.rb @@ -31,6 +31,8 @@ class Linear end def labels(team_id) + raise ArgumentError, 'Missing team id' if team_id.blank? + query = <<~GRAPHQL query { team(id: "#{team_id}") { @@ -49,7 +51,14 @@ class Linear private def execute_query(query) - @client.query(query).data.to_h + response = @client.query(query) + Rails.logger.debug { "GraphQL Response: #{response.inspect}" } + unless response.data + raise StandardError, "Error retrieving data: #{response.errors.messages}" if response.errors.any? + + return { error: 'No data returned' } + end + response.data.to_h rescue Graphlient::Errors::GraphQLError => e { error: "GraphQL Error: #{e.message}" } rescue Graphlient::Errors::ServerError => e