From 68cb6b19b0c25825124d7ff945779f1483e7fba5 Mon Sep 17 00:00:00 2001 From: Muhsin Keloth Date: Thu, 2 May 2024 16:11:21 +0530 Subject: [PATCH] feat: add teams end point --- config/routes.rb | 3 +- lib/integrations/linear/processor_service.rb | 20 +++++++--- lib/linear.rb | 41 +++++++++++++++----- 3 files changed, 48 insertions(+), 16 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index 33f6ce887..cd0ccd620 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -228,7 +228,8 @@ Rails.application.routes.draw do end resource :linear, controller: 'linear', only: [] do collection do - post :list + get :teams + get :labels end end end diff --git a/lib/integrations/linear/processor_service.rb b/lib/integrations/linear/processor_service.rb index 770c6cf4a..15f5170ef 100644 --- a/lib/integrations/linear/processor_service.rb +++ b/lib/integrations/linear/processor_service.rb @@ -1,17 +1,27 @@ class Integrations::Linear::ProcessorService pattr_initialize [:account!, :conversation!] - def create_issue; end + def teams + response = linear_client.teams + return response if response[:error] - def link_issue; end + response['teams']['nodes'].map(&:as_json) + end - def list_issues - issues = linear_client.list_issues - issues.map(&:as_json) + # get labels for a team + def labels(team_id) + response = linear_client.labels(team_id) + return response if response[:error] + + response['team']['labels']['nodes'].map(&:as_json) end private + def conversation_link + "#{ENV.fetch('FRONTEND_URL', nil)}/app/accounts/#{account.id}/conversations/#{conversation.display_id}" + end + def linear_hook @linear_hook ||= account.hooks.find_by!(app_id: 'linear') end diff --git a/lib/linear.rb b/lib/linear.rb index 715c74fd6..e8a2031af 100644 --- a/lib/linear.rb +++ b/lib/linear.rb @@ -16,24 +16,45 @@ class Linear ) end - def list_issues + def teams query = <<~GRAPHQL query { - issues(first: 5) { + teams { nodes { id - title + name } } } GRAPHQL + execute_query(query) + end - begin - response = @client.query(query) - response.data.issues.nodes - rescue StandardError => e - # return error message - e.message - end + def labels(team_id) + query = <<~GRAPHQL + query { + team(id: "#{team_id}") { + labels { + nodes { + id + name + } + } + } + } + GRAPHQL + execute_query(query) + end + + private + + def execute_query(query) + @client.query(query).data.to_h + rescue Graphlient::Errors::GraphQLError => e + { error: "GraphQL Error: #{e.message}" } + rescue Graphlient::Errors::ServerError => e + { error: "Server Error: #{e.message}" } + rescue StandardError => e + { error: "Unexpected Error: #{e.message}" } end end