Commit 42eddfb0 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch '21865-set-first-mentioned-in-commit-when-issue-closes' into 'master'

Set `first_mentioned_in_commit_at` on issue closes

See merge request gitlab-org/gitlab!25503
parents 62397cd6 c7dfc1f6
......@@ -36,6 +36,8 @@ module Issues
execute_hooks(issue, 'close')
invalidate_cache_counts(issue, users: issue.assignees)
issue.update_project_counter_caches
store_first_mentioned_in_commit_at(issue, closed_via) if closed_via.is_a?(MergeRequest)
end
issue
......@@ -46,5 +48,17 @@ module Issues
def create_note(issue, current_commit)
SystemNoteService.change_status(issue, issue.project, current_user, issue.state, current_commit)
end
def store_first_mentioned_in_commit_at(issue, merge_request)
return unless Feature.enabled?(:store_first_mentioned_in_commit_on_issue_close, issue.project)
metrics = issue.metrics
return if metrics.nil? || metrics.first_mentioned_in_commit_at
first_commit_timestamp = merge_request.commits(limit: 1).first&.date
return unless first_commit_timestamp
metrics.update!(first_mentioned_in_commit_at: first_commit_timestamp)
end
end
end
......@@ -98,6 +98,53 @@ describe Issues::CloseService do
expect(body_text).not_to include(closing_merge_request.to_reference)
end
end
context 'updating `metrics.first_mentioned_in_commit_at`' do
subject { described_class.new(project, user).close_issue(issue, closed_via: closing_merge_request) }
context 'when `metrics.first_mentioned_in_commit_at` is not set' do
it 'uses the first commit timestamp' do
expected = closing_merge_request.commits.first.date
subject
expect(issue.metrics.first_mentioned_in_commit_at).to eq(expected)
end
end
context 'when `metrics.first_mentioned_in_commit_at` is already set' do
before do
issue.metrics.update!(first_mentioned_in_commit_at: Time.now)
end
it 'does not update the metrics' do
expect { subject }.not_to change { issue.metrics.first_mentioned_in_commit_at }
end
end
context 'when merge request has no commits' do
let(:closing_merge_request) { create(:merge_request, :without_diffs, source_project: project) }
it 'does not update the metrics' do
subject
expect(issue.metrics.first_mentioned_in_commit_at).to be_nil
end
end
context 'when `store_first_mentioned_in_commit_on_issue_close` feature flag is off' do
before do
stub_feature_flags(store_first_mentioned_in_commit_on_issue_close: { enabled: false, thing: issue.project })
end
it 'does not update the metrics' do
subject
expect(described_class).not_to receive(:store_first_mentioned_in_commit_at)
expect(issue.metrics.first_mentioned_in_commit_at).to be_nil
end
end
end
end
context "closed by a commit", :sidekiq_might_not_need_inline do
......
......@@ -118,7 +118,7 @@ describe MergeRequests::MergeService do
it 'closes GitLab issue tracker issues' do
issue = create :issue, project: project
commit = double('commit', safe_message: "Fixes #{issue.to_reference}")
commit = double('commit', safe_message: "Fixes #{issue.to_reference}", date: Time.now)
allow(merge_request).to receive(:commits).and_return([commit])
merge_request.cache_merge_request_closes_issues!
......
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