Commit acf34313 authored by syasonik's avatar syasonik

Add sync support for sentry plugin integrations

Sentry has two types of GitLab integrations implemented - a
global integration and a plugin integration. When an issue
corresponding to a Sentry issue is created in GitLab, this
logic adds a reciprocal link to the GitLab issue in Sentry
if the Sentry instance has the plugin-type integration
enabled.
parent 4850d381
...@@ -20,17 +20,11 @@ class ErrorTrackingIssueLinkWorker ...@@ -20,17 +20,11 @@ class ErrorTrackingIssueLinkWorker
def perform(issue_id) def perform(issue_id)
@issue = Issue.find_by_id(issue_id) @issue = Issue.find_by_id(issue_id)
return unless issue && error_tracking && sentry_issue_id return unless valid?
try_obtain_lease do try_obtain_lease do
logger.info("Linking Sentry issue #{sentry_issue_id} to GitLab issue #{issue.id}") logger.info("Linking Sentry issue #{sentry_issue_id} to GitLab issue #{issue.id}")
if integration_id.nil?
logger.info("Sentry integration unavailable for #{error_tracking.api_url}")
break
end
sentry_client.create_issue_link(integration_id, sentry_issue_id, issue) sentry_client.create_issue_link(integration_id, sentry_issue_id, issue)
rescue Sentry::Client::Error rescue Sentry::Client::Error
logger.info("Failed to link Sentry issue #{sentry_issue_id} to GitLab issue #{issue.id}") logger.info("Failed to link Sentry issue #{sentry_issue_id} to GitLab issue #{issue.id}")
...@@ -39,6 +33,10 @@ class ErrorTrackingIssueLinkWorker ...@@ -39,6 +33,10 @@ class ErrorTrackingIssueLinkWorker
private private
def valid?
issue && error_tracking && sentry_issue_id
end
def error_tracking def error_tracking
strong_memoize(:error_tracking) do strong_memoize(:error_tracking) do
issue.project.error_tracking_setting issue.project.error_tracking_setting
......
---
title: Sync GitLab issues with Sentry plugin integration
merge_request: 23355
author:
type: added
...@@ -54,6 +54,12 @@ module Sentry ...@@ -54,6 +54,12 @@ module Sentry
end end
end end
def http_post(url, params = {})
http_request do
Gitlab::HTTP.post(url, **request_params.merge(body: params.to_json))
end
end
def http_request def http_request
response = handle_request_exceptions do response = handle_request_exceptions do
yield yield
......
...@@ -3,8 +3,22 @@ ...@@ -3,8 +3,22 @@
module Sentry module Sentry
class Client class Client
module IssueLink module IssueLink
def create_issue_link(integration_id, sentry_issue_identifier, issue) # Creates a link in Sentry corresponding to the provided
issue_link_url = issue_link_api_url(integration_id, sentry_issue_identifier) # Sentry issue and GitLab issue
# @param integration_id [Integer, nil] Representing a global
# GitLab integration in Sentry. Nil for plugins.
# @param sentry_issue_id [Integer] Id for an issue from Sentry
# @param issue [Issue] Issue for which the link should be created
def create_issue_link(integration_id, sentry_issue_id, issue)
return create_plugin_link(sentry_issue_id, issue) unless integration_id
create_global_integration_link(integration_id, sentry_issue_id, issue)
end
private
def create_global_integration_link(integration_id, sentry_issue_id, issue)
issue_link_url = global_integration_link_api_url(integration_id, sentry_issue_id)
params = { params = {
project: issue.project.id, project: issue.project.id,
...@@ -14,11 +28,22 @@ module Sentry ...@@ -14,11 +28,22 @@ module Sentry
http_put(issue_link_url, params) http_put(issue_link_url, params)
end end
private def global_integration_link_api_url(integration_id, sentry_issue_id)
issue_link_url = URI(url)
issue_link_url.path = "/api/0/groups/#{sentry_issue_id}/integrations/#{integration_id}/"
issue_link_url
end
def create_plugin_link(sentry_issue_id, issue)
issue_link_url = plugin_link_api_url(sentry_issue_id)
http_post(issue_link_url, issue_id: issue.iid)
end
def issue_link_api_url(integration_id, sentry_issue_identifier) def plugin_link_api_url(sentry_issue_id)
issue_link_url = URI(url) issue_link_url = URI(url)
issue_link_url.path = "/api/0/groups/#{sentry_issue_identifier}/integrations/#{integration_id}/" issue_link_url.path = "/api/0/issues/#{sentry_issue_id}/plugins/gitlab/link/"
issue_link_url issue_link_url
end end
......
{
"message": "Successfully linked issue.",
"link": "https://gitlab.com/test/tanuki-inc/issues/3",
"id": 3,
"label": "GL-3"
}
...@@ -5,18 +5,18 @@ require 'spec_helper' ...@@ -5,18 +5,18 @@ require 'spec_helper'
describe Sentry::Client::IssueLink do describe Sentry::Client::IssueLink do
include SentryClientHelpers include SentryClientHelpers
let(:error_tracking_setting) { create(:project_error_tracking_setting, api_url: sentry_url) } let_it_be(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' } let_it_be(:error_tracking_setting) { create(:project_error_tracking_setting, api_url: sentry_url) }
let(:client) { error_tracking_setting.sentry_client } let_it_be(:issue) { create(:issue, project: error_tracking_setting.project) }
let(:issue_link_sample_response) { JSON.parse(fixture_file('sentry/issue_link_sample_response.json')) } let(:client) { error_tracking_setting.sentry_client }
let(:sentry_issue_id) { 11111111 }
describe '#create_issue_link' do describe '#create_issue_link' do
let(:sentry_issue_link_url) { "https://sentrytest.gitlab.com/api/0/groups/#{sentry_issue_id}/integrations/#{integration_id}/" }
let(:integration_id) { 44444 } let(:integration_id) { 44444 }
let(:sentry_issue_id) { 11111111 }
let(:issue) { create(:issue, project: error_tracking_setting.project) }
let(:sentry_issue_link_url) { "https://sentrytest.gitlab.com/api/0/groups/#{sentry_issue_id}/integrations/#{integration_id}/" } let(:issue_link_sample_response) { JSON.parse(fixture_file('sentry/global_integration_link_sample_response.json')) }
let(:sentry_api_response) { issue_link_sample_response } let(:sentry_api_response) { issue_link_sample_response }
let!(:sentry_api_request) { stub_sentry_request(sentry_issue_link_url, :put, body: sentry_api_response, status: 201) } let!(:sentry_api_request) { stub_sentry_request(sentry_issue_link_url, :put, body: sentry_api_response, status: 201) }
...@@ -37,5 +37,29 @@ describe Sentry::Client::IssueLink do ...@@ -37,5 +37,29 @@ describe Sentry::Client::IssueLink do
it_behaves_like 'maps Sentry exceptions', :put it_behaves_like 'maps Sentry exceptions', :put
end end
context 'when integration_id is not provided' do
let(:sentry_issue_link_url) { "https://sentrytest.gitlab.com/api/0/issues/#{sentry_issue_id}/plugins/gitlab/link/" }
let(:integration_id) { nil }
let(:issue_link_sample_response) { JSON.parse(fixture_file('sentry/plugin_link_sample_response.json')) }
let!(:sentry_api_request) { stub_sentry_request(sentry_issue_link_url, :post, body: sentry_api_response) }
it_behaves_like 'calls sentry api'
it { is_expected.to be_present }
context 'redirects' do
let(:sentry_api_url) { sentry_issue_link_url }
it_behaves_like 'no Sentry redirects', :post
end
context 'when exception is raised' do
let(:sentry_request_url) { sentry_issue_link_url }
it_behaves_like 'maps Sentry exceptions', :post
end
end
end end
end end
...@@ -40,14 +40,17 @@ describe ErrorTrackingIssueLinkWorker do ...@@ -40,14 +40,17 @@ describe ErrorTrackingIssueLinkWorker do
end end
end end
shared_examples_for 'terminates after one API request' do shared_examples_for 'attempts to create a link via plugin' do
it 'takes no action' do it 'takes no action' do
expect_next_instance_of(Sentry::Client) do |client| expect_next_instance_of(Sentry::Client) do |client|
expect(client).to receive(:repos).with('sentry-org').and_return([repo]) expect(client).to receive(:repos).with('sentry-org').and_return([repo])
expect(client)
.to receive(:create_issue_link)
.with(nil, sentry_issue.sentry_issue_identifier, issue)
.and_return(true)
end end
expect_any_instance_of(Sentry::Client).not_to receive(:create_issue_link)
expect(subject).to be nil expect(subject).to be true
end end
end end
...@@ -78,7 +81,7 @@ describe ErrorTrackingIssueLinkWorker do ...@@ -78,7 +81,7 @@ describe ErrorTrackingIssueLinkWorker do
) )
end end
it_behaves_like 'terminates after one API request' it_behaves_like 'attempts to create a link via plugin'
end end
context 'when Sentry the GitLab integration is for another project' do context 'when Sentry the GitLab integration is for another project' do
...@@ -90,7 +93,7 @@ describe ErrorTrackingIssueLinkWorker do ...@@ -90,7 +93,7 @@ describe ErrorTrackingIssueLinkWorker do
) )
end end
it_behaves_like 'terminates after one API request' it_behaves_like 'attempts to create a link via plugin'
end end
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment