Commit 1d495ab5 authored by Reuben Pereira's avatar Reuben Pereira

Track when MR merged before code approval rules were synced

Log an error when an MR was merged before the code owner approval
rules could be synced.
parent 9af258ae
......@@ -2,13 +2,15 @@
module MergeRequests
class SyncCodeOwnerApprovalRules
AlreadyMergedError = Class.new(StandardError)
def initialize(merge_request, params = {})
@merge_request = merge_request
@previous_diff = params[:previous_diff]
end
def execute
return if merge_request.merged?
return already_merged if merge_request.merged?
delete_outdated_code_owner_rules
......@@ -45,5 +47,15 @@ module MergeRequests
@code_owner_entries ||= Gitlab::CodeOwners
.entries_for_merge_request(merge_request, merge_request_diff: previous_diff)
end
def already_merged
Gitlab::ErrorTracking.track_exception(
AlreadyMergedError.new('MR already merged before code owner approval rules were synced'),
merge_request_id: merge_request.id,
merge_request_iid: merge_request.iid,
project_id: merge_request.project_id
)
nil
end
end
end
......@@ -3,11 +3,12 @@
require 'spec_helper'
RSpec.describe MergeRequests::SyncCodeOwnerApprovalRules do
let(:merge_request) { create(:merge_request) }
let(:rb_owners) { create_list(:user, 2) }
let(:doc_owners) { create_list(:user, 2) }
let(:rb_group_owners) { create_list(:group, 2) }
let(:doc_group_owners) { create_list(:group, 2) }
let_it_be(:merge_request) { create(:merge_request) }
let_it_be(:rb_owners) { create_list(:user, 2) }
let_it_be(:doc_owners) { create_list(:user, 2) }
let_it_be(:rb_group_owners) { create_list(:group, 2) }
let_it_be(:doc_group_owners) { create_list(:group, 2) }
let(:rb_entry) { build_entry('*.rb', rb_owners, rb_group_owners) }
let(:doc_entry) { build_entry('doc/*', doc_owners, doc_group_owners) }
let(:entries) { [rb_entry, doc_entry] }
......@@ -64,5 +65,24 @@ RSpec.describe MergeRequests::SyncCodeOwnerApprovalRules do
expect(other_rule.reload.users).to eq(rb_owners)
expect(other_rule.reload.groups).to match_array(rb_group_owners)
end
context 'when merge request is already merged' do
let(:merge_request) { build(:merge_request, :merged) }
it 'logs an error' do
expect(Gitlab::ErrorTracking)
.to receive(:track_exception)
.with(
instance_of(described_class::AlreadyMergedError),
hash_including(
merge_request_id: merge_request.id,
merge_request_iid: merge_request.iid,
project_id: merge_request.project_id
)
).and_call_original
expect(service.execute).to eq(nil)
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