Commit 902dbd2d authored by Sean Arnold's avatar Sean Arnold

WIP Close issue after error resolve

- Extract common test setup into context
- Add specs for update service
- Fix spec by returning issue
parent 6fafb9b4
...@@ -4,6 +4,14 @@ module ErrorTracking ...@@ -4,6 +4,14 @@ module ErrorTracking
class IssueUpdateService < ErrorTracking::BaseService class IssueUpdateService < ErrorTracking::BaseService
private private
def perform
response = fetch
update_related_issue unless parse_errors(response).present?
response
end
def fetch def fetch
project_error_tracking_setting.update_issue( project_error_tracking_setting.update_issue(
issue_id: params[:issue_id], issue_id: params[:issue_id],
...@@ -11,6 +19,37 @@ module ErrorTracking ...@@ -11,6 +19,37 @@ module ErrorTracking
) )
end end
def update_related_issue
issue = related_issue
return unless issue && resolving?
processed_issue = close_issue(issue)
create_system_note(processed_issue)
end
def close_issue(issue)
Issues::CloseService
.new(project, current_user)
.execute(issue, system_note: false)
end
def create_system_note(issue)
return unless issue.reset.closed?
SystemNoteService.close_after_error_tracking_resolve(issue, project, current_user)
end
def related_issue
SentryIssuesFinder
.new(project, current_user)
.find_by_identifier(params[:issue_id])
&.issue
end
def resolving?
update_params[:status] == 'resolved'
end
def update_params def update_params
params.except(:issue_id) params.except(:issue_id)
end end
......
...@@ -99,6 +99,12 @@ module SystemNoteService ...@@ -99,6 +99,12 @@ module SystemNoteService
::SystemNotes::TimeTrackingService.new(noteable: noteable, project: project, author: author).change_time_spent ::SystemNotes::TimeTrackingService.new(noteable: noteable, project: project, author: author).change_time_spent
end end
def close_after_error_tracking_resolve(noteable, project, author)
body = 'automatically closed this as a result of resolving the corresponding Sentry error.'
create_note(NoteSummary.new(noteable, project, author, body, action: 'closed'))
end
def change_status(noteable, project, author, status, source = nil) def change_status(noteable, project, author, status, source = nil)
::SystemNotes::IssuablesService.new(noteable: noteable, project: project, author: author).change_status(status, source) ::SystemNotes::IssuablesService.new(noteable: noteable, project: project, author: author).change_status(status, source)
end end
......
...@@ -3,24 +3,7 @@ ...@@ -3,24 +3,7 @@
require 'spec_helper' require 'spec_helper'
describe ErrorTracking::IssueDetailsService do describe ErrorTracking::IssueDetailsService do
let_it_be(:user) { create(:user) } include_context 'sentry error tracking context'
let_it_be(:project) { create(:project) }
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
let(:result) { subject.execute }
let(:error_tracking_setting) do
create(:project_error_tracking_setting, api_url: sentry_url, token: token, project: project)
end
subject { described_class.new(project, user) }
before do
expect(project).to receive(:error_tracking_setting).at_least(:once).and_return(error_tracking_setting)
project.add_reporter(user)
end
describe '#execute' do describe '#execute' do
context 'with authorized user' do context 'with authorized user' do
......
...@@ -3,24 +3,7 @@ ...@@ -3,24 +3,7 @@
require 'spec_helper' require 'spec_helper'
describe ErrorTracking::IssueLatestEventService do describe ErrorTracking::IssueLatestEventService do
let_it_be(:user) { create(:user) } include_context 'sentry error tracking context'
let_it_be(:project) { create(:project) }
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
let(:result) { subject.execute }
let(:error_tracking_setting) do
create(:project_error_tracking_setting, api_url: sentry_url, token: token, project: project)
end
subject { described_class.new(project, user) }
before do
expect(project).to receive(:error_tracking_setting).at_least(:once).and_return(error_tracking_setting)
project.add_reporter(user)
end
describe '#execute' do describe '#execute' do
context 'with authorized user' do context 'with authorized user' do
......
# frozen_string_literal: true
require 'spec_helper'
describe ErrorTracking::IssueUpdateService do
include_context 'sentry error tracking context'
let(:arguments) { { issue_id: 1234, status: 'resolved' } }
subject { described_class.new(project, user, arguments) }
describe '#execute' do
context 'with authorized user' do
context 'when update_issue returns success' do
let(:update_issue_response) { { updated: true } }
before do
expect(error_tracking_setting)
.to receive(:update_issue).and_return(update_issue_response)
end
it 'returns the response' do
expect(result).to eq(update_issue_response.merge(status: :success))
end
it 'updates any related issue' do
expect(subject).to receive(:update_related_issue)
result
end
context 'related issue and resolving' do
let(:issue) { create(:issue, project: project) }
let(:sentry_issue) { create(:sentry_issue, issue: issue) }
let(:arguments) { { issue_id: sentry_issue.sentry_issue_identifier, status: 'resolved' } }
let(:issue_close_service) { spy(:issue_close_service) }
before do
allow_any_instance_of(SentryIssuesFinder)
.to receive(:find_by_identifier)
.and_return(sentry_issue)
end
after do
result
end
it 'closes the issue' do
close_service = spy(:close_service)
expect(Issues::CloseService)
.to receive(:new)
.and_return(close_service)
expect(close_service)
.to receive(:execute)
.with(issue, system_note: false)
.and_return(issue)
end
it 'creates a system note' do
expect(SystemNoteService).to receive(:close_after_error_tracking_resolve)
end
end
end
include_examples 'error tracking service sentry error handling', :update_issue
end
include_examples 'error tracking service unauthorized user'
include_examples 'error tracking service disabled'
end
end
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
require 'spec_helper' require 'spec_helper'
describe ErrorTracking::ListIssuesService do describe ErrorTracking::ListIssuesService do
let_it_be(:user) { create(:user) } include_context 'sentry error tracking context'
let_it_be(:project) { create(:project) }
let(:params) { { search_term: 'something', sort: 'last_seen', cursor: 'some-cursor' } } let(:params) { { search_term: 'something', sort: 'last_seen', cursor: 'some-cursor' } }
let(:list_sentry_issues_args) do let(:list_sentry_issues_args) do
{ {
...@@ -16,22 +16,8 @@ describe ErrorTracking::ListIssuesService do ...@@ -16,22 +16,8 @@ describe ErrorTracking::ListIssuesService do
} }
end end
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
let(:result) { subject.execute }
let(:error_tracking_setting) do
create(:project_error_tracking_setting, api_url: sentry_url, token: token, project: project)
end
subject { described_class.new(project, user, params) } subject { described_class.new(project, user, params) }
before do
expect(project).to receive(:error_tracking_setting).at_least(:once).and_return(error_tracking_setting)
project.add_reporter(user)
end
describe '#execute' do describe '#execute' do
context 'with authorized user' do context 'with authorized user' do
context 'when list_sentry_issues returns issues' do context 'when list_sentry_issues returns issues' do
......
# frozen_string_literal: true
shared_context 'sentry error tracking context' do
let_it_be(:user) { create(:user) }
let_it_be(:project) { create(:project) }
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
let(:token) { 'test-token' }
let(:result) { subject.execute }
subject { described_class.new(project, user) }
let(:error_tracking_setting) do
create(:project_error_tracking_setting, api_url: sentry_url, token: token, project: project)
end
before do
expect(project).to receive(:error_tracking_setting).at_least(:once).and_return(error_tracking_setting)
project.add_reporter(user)
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