Commit 30de0eb5 authored by Stan Hu's avatar Stan Hu

Fix analytics tracking for new merge request notes

When a new note is created for a merge request with no associated
metrics, Sidekiq will error out with `NoMethodError: undefined method
'first_comment_at' for nil:NilClass`.

This commit ensures a merge request metrics exists before attempting to
update it. There is still an outstanding question of why no such metrics
were created, but that can happen for older merge requests.

Closes https://gitlab.com/gitlab-org/gitlab/issues/197464
parent 0008cffa
---
title: Fix analytics tracking for new merge request notes
merge_request: 23273
author:
type: fixed
......@@ -22,9 +22,11 @@ module Analytics
def execute(force: false)
merge_requests.each do |mr|
next if !force && mr.metrics.first_comment_at
metrics = mr.ensure_metrics
mr.metrics.update!(first_comment_at: ProductivityCalculator.new(mr).first_comment_at)
next if !force && metrics.first_comment_at
metrics.update!(first_comment_at: ProductivityCalculator.new(mr).first_comment_at)
end
end
......
......@@ -25,6 +25,19 @@ describe Analytics::RefreshCommentsData do
end.to change { noteable.metrics.first_comment_at }.from(nil).to(be_like_time(note.created_at))
end
context 'when no merge request metric is present' do
before do
noteable.metrics.destroy
noteable.reload
end
it 'creates a new metric and updates the first_comment_at' do
subject.execute
expect(noteable.metrics.reload.first_comment_at).to(be_like_time(note.created_at))
end
end
context 'and first_comment_at is already filled' do
before do
noteable.metrics.update(first_comment_at: 3.days.ago.beginning_of_day)
......
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