Commit 44e01684 authored by Peter Leitzen's avatar Peter Leitzen

Implement publish incident service

parent b0843ed9
# frozen_string_literal: true
module StatusPage
# Delegate work to more specific publishing services.
#
# Use this service for publishing an incident to CDN which calls:
# * StatusPage::PublishDetailsService
# * StatusPage::PublishListService
class PublishIncidentService
include Gitlab::Utils::StrongMemoize
def initialize(project:, issue_id:)
@project = project
@issue_id = issue_id
end
def execute
return error_issue_not_found unless issue
response = publish_details
return response if response.error?
publish_list
end
private
attr_reader :project, :issue_id
def publish_details
PublishDetailsService.new(project: project).execute(issue, user_notes)
end
def publish_list
PublishListService.new(project: project).execute(issues)
end
def issue
strong_memoize(:issue) { issues_finder.find_by_id(issue_id) }
end
def user_notes
strong_memoize(:user_notes) do
IncidentCommentsFinder.new(issue: issue).all
end
end
def issues
strong_memoize(:issues) { issues_finder.all }
end
def issues_finder
strong_memoize(:issues_finder) do
IncidentsFinder.new(project_id: project.id)
end
end
def error_issue_not_found
ServiceResponse.error(message: 'Issue not found')
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe StatusPage::PublishIncidentService do
let_it_be(:project, refind: true) { create(:project) }
let_it_be(:issue) { create(:issue, project: project) }
let_it_be(:settings) { create(:status_page_setting, :enabled, project: project) }
let(:service) { described_class.new(project: project, issue_id: issue.id) }
subject(:result) { service.execute }
describe '#execute' do
before do
stub_licensed_features(status_page: true)
end
context 'when publishing succeeds' do
it 'returns uploads incidents details and list' do
expect_to_upload_details(issue)
expect_to_upload_list
expect(result).to be_success
end
end
context 'when uploading details fails' do
it 'returns error and skip list upload' do
expect_to_upload_details(issue, status: 404)
expect(result).to be_error
path = StatusPage::Storage.details_path(issue.iid)
expect(result.message).to include('NotFound')
expect(result.message).to include(path)
end
end
context 'when uploading list fails' do
it 'returns error and skip list upload' do
expect_to_upload_details(issue)
expect_to_upload_list(status: 404)
expect(result).to be_error
path = StatusPage::Storage.list_path
expect(result.message).to include('NotFound')
expect(result.message).to include(path)
end
end
context 'with unrelated issue' do
let(:issue) { create(:issue) }
it 'returns error issue not found' do
expect(result).to be_error
expect(result.message).to eq('Issue not found')
end
end
end
private
def expect_to_upload_details(issue, **kwargs)
stub_upload_request(StatusPage::Storage.details_path(issue.iid), **kwargs)
end
def expect_to_upload_list(**kwargs)
stub_upload_request(StatusPage::Storage.list_path, **kwargs)
end
def stub_upload_request(path, status: 200)
stub_request(:put, %r{amazonaws.com/#{path}}).to_return(status: status)
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