-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add APIs for linear integration (#9346)
- Loading branch information
Showing
16 changed files
with
1,307 additions
and
23 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
app/controllers/api/v1/accounts/integrations/linear_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
class Api::V1::Accounts::Integrations::LinearController < Api::V1::Accounts::BaseController | ||
before_action :fetch_conversation, only: [:link_issue, :linked_issues] | ||
|
||
def teams | ||
teams = linear_processor_service.teams | ||
if teams[:error] | ||
render json: { error: teams[:error] }, status: :unprocessable_entity | ||
else | ||
render json: teams[:data], status: :ok | ||
end | ||
end | ||
|
||
def team_entities | ||
team_id = permitted_params[:team_id] | ||
team_entities = linear_processor_service.team_entities(team_id) | ||
if team_entities[:error] | ||
render json: { error: team_entities[:error] }, status: :unprocessable_entity | ||
else | ||
render json: team_entities[:data], status: :ok | ||
end | ||
end | ||
|
||
def create_issue | ||
issue = linear_processor_service.create_issue(permitted_params) | ||
if issue[:error] | ||
render json: { error: issue[:error] }, status: :unprocessable_entity | ||
else | ||
render json: issue[:data], status: :ok | ||
end | ||
end | ||
|
||
def link_issue | ||
issue_id = permitted_params[:issue_id] | ||
title = permitted_params[:title] | ||
issue = linear_processor_service.link_issue(conversation_link, issue_id, title) | ||
if issue[:error] | ||
render json: { error: issue[:error] }, status: :unprocessable_entity | ||
else | ||
render json: issue[:data], status: :ok | ||
end | ||
end | ||
|
||
def unlink_issue | ||
link_id = permitted_params[:link_id] | ||
issue = linear_processor_service.unlink_issue(link_id) | ||
|
||
if issue[:error] | ||
render json: { error: issue[:error] }, status: :unprocessable_entity | ||
else | ||
render json: issue[:data], status: :ok | ||
end | ||
end | ||
|
||
def linked_issues | ||
issues = linear_processor_service.linked_issues(conversation_link) | ||
|
||
if issues[:error] | ||
render json: { error: issues[:error] }, status: :unprocessable_entity | ||
else | ||
render json: issues[:data], status: :ok | ||
end | ||
end | ||
|
||
def search_issue | ||
render json: { error: 'Specify search string with parameter q' }, status: :unprocessable_entity if params[:q].blank? && return | ||
|
||
term = params[:q] | ||
issues = linear_processor_service.search_issue(term) | ||
if issues[:error] | ||
render json: { error: issues[:error] }, status: :unprocessable_entity | ||
else | ||
render json: issues[:data], status: :ok | ||
end | ||
end | ||
|
||
private | ||
|
||
def conversation_link | ||
"#{ENV.fetch('FRONTEND_URL', nil)}/app/accounts/#{Current.account.id}/conversations/#{@conversation.display_id}" | ||
end | ||
|
||
def fetch_conversation | ||
@conversation = Current.account.conversations.find_by!(display_id: permitted_params[:conversation_id]) | ||
end | ||
|
||
def linear_processor_service | ||
Integrations::Linear::ProcessorService.new(account: Current.account) | ||
end | ||
|
||
def permitted_params | ||
params.permit(:team_id, :conversation_id, :issue_id, :link_id, :title, :description, :assignee_id, :priority, label_ids: []) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,3 +83,5 @@ | |
- name: help_center_embedding_search | ||
enabled: false | ||
premium: true | ||
- name: linear_integration | ||
enabled: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
class Integrations::Linear::ProcessorService | ||
pattr_initialize [:account!] | ||
|
||
def teams | ||
response = linear_client.teams | ||
return { error: response[:error] } if response[:error] | ||
|
||
{ data: response['teams']['nodes'].map(&:as_json) } | ||
end | ||
|
||
def team_entities(team_id) | ||
response = linear_client.team_entities(team_id) | ||
return response if response[:error] | ||
|
||
{ | ||
data: { | ||
users: response['users']['nodes'].map(&:as_json), | ||
projects: response['projects']['nodes'].map(&:as_json), | ||
states: response['workflowStates']['nodes'].map(&:as_json), | ||
labels: response['issueLabels']['nodes'].map(&:as_json) | ||
} | ||
} | ||
end | ||
|
||
def create_issue(params) | ||
response = linear_client.create_issue(params) | ||
return response if response[:error] | ||
|
||
{ | ||
data: { id: response['issueCreate']['issue']['id'], | ||
title: response['issueCreate']['issue']['title'] } | ||
} | ||
end | ||
|
||
def link_issue(link, issue_id, title) | ||
response = linear_client.link_issue(link, issue_id, title) | ||
return response if response[:error] | ||
|
||
{ | ||
data: { | ||
id: issue_id, | ||
link: link, | ||
link_id: response.with_indifferent_access[:attachmentLinkURL][:attachment][:id] | ||
} | ||
} | ||
end | ||
|
||
def unlink_issue(link_id) | ||
response = linear_client.unlink_issue(link_id) | ||
return response if response[:error] | ||
|
||
{ | ||
data: { link_id: link_id } | ||
} | ||
end | ||
|
||
def search_issue(term) | ||
response = linear_client.search_issue(term) | ||
|
||
return response if response[:error] | ||
|
||
{ data: response['searchIssues']['nodes'].map(&:as_json) } | ||
end | ||
|
||
def linked_issues(url) | ||
response = linear_client.linked_issues(url) | ||
return response if response[:error] | ||
|
||
{ data: response['attachmentsForURL']['nodes'].map(&:as_json) } | ||
end | ||
|
||
private | ||
|
||
def linear_hook | ||
@linear_hook ||= account.hooks.find_by!(app_id: 'linear') | ||
end | ||
|
||
def linear_client | ||
credentials = linear_hook.settings | ||
@linear_client ||= Linear.new(credentials['api_key']) | ||
end | ||
end |
Oops, something went wrong.