Commit 34f77b9f authored by Igor Drozdov's avatar Igor Drozdov

Suggest squash commit messages based on recent commits

This commit loads a safe number of commits in order to
calculate squash commit message
parent d56963e5
......@@ -1066,7 +1066,7 @@ class MergeRequest < ApplicationRecord
# Returns the oldest multi-line commit message, or the MR title if none found
def default_squash_commit_message
strong_memoize(:default_squash_commit_message) do
commits.without_merge_commits.reverse.find(&:description?)&.safe_message || title
recent_commits.without_merge_commits.reverse_each.find(&:description?)&.safe_message || title
end
end
......
......@@ -15,7 +15,7 @@ class MergeRequestPollCachedWidgetEntity < IssuableEntity
expose :target_project_id
expose :squash
expose :rebase_in_progress?, as: :rebase_in_progress
expose :default_squash_commit_message
expose :default_squash_commit_message, if: -> (merge_request, _) { merge_request.mergeable? }
expose :commits_count
expose :merge_ongoing?, as: :merge_ongoing
expose :work_in_progress?, as: :work_in_progress
......@@ -25,8 +25,9 @@ class MergeRequestPollCachedWidgetEntity < IssuableEntity
expose :source_branch_exists?, as: :source_branch_exists
expose :branch_missing?, as: :branch_missing
expose :commits_without_merge_commits, using: MergeRequestWidgetCommitEntity do |merge_request|
merge_request.commits.without_merge_commits
expose :commits_without_merge_commits, using: MergeRequestWidgetCommitEntity,
if: -> (merge_request, _) { merge_request.mergeable? } do |merge_request|
merge_request.recent_commits.without_merge_commits
end
expose :diff_head_sha do |merge_request|
merge_request.diff_head_sha.presence
......
---
title: Suggest squash commit messages based on recent commits
merge_request: 20231
author:
type: performance
......@@ -115,8 +115,24 @@ describe MergeRequest do
let(:multiline_commits) { subject.commits.select(&is_multiline) }
let(:singleline_commits) { subject.commits.reject(&is_multiline) }
it 'returns the oldest multiline commit message' do
expect(subject.default_squash_commit_message).to eq(multiline_commits.last.message)
context 'when the total number of commits is safe' do
it 'returns the oldest multiline commit message' do
expect(subject.default_squash_commit_message).to eq(multiline_commits.last.message)
end
end
context 'when the total number of commits is big' do
let(:safe_number) { 20 }
before do
stub_const('MergeRequestDiff::COMMITS_SAFE_SIZE', safe_number)
end
it 'returns the oldest multiline commit message from safe number of commits' do
expect(subject.default_squash_commit_message).to eq(
"remove emtpy file.(beacase git ignore empty file)\nadd whitespace test file.\n"
)
end
end
it 'returns the merge request title if there are no multiline commits' do
......
......@@ -212,9 +212,29 @@ describe MergeRequestWidgetEntity do
.to eq(resource.default_merge_commit_message(include_description: true))
end
it 'has default_squash_commit_message' do
expect(subject[:default_squash_commit_message])
.to eq(resource.default_squash_commit_message)
describe 'attributes for squash commit message' do
context 'when merge request is mergeable' do
before do
stub_const('MergeRequestDiff::COMMITS_SAFE_SIZE', 20)
end
it 'has default_squash_commit_message and commits_without_merge_commits' do
expect(subject[:default_squash_commit_message])
.to eq(resource.default_squash_commit_message)
expect(subject[:commits_without_merge_commits].size).to eq(12)
end
end
context 'when merge request is not mergeable' do
before do
allow(resource).to receive(:mergeable?).and_return(false)
end
it 'does not have default_squash_commit_message and commits_without_merge_commits' do
expect(subject[:default_squash_commit_message]).to eq(nil)
expect(subject[:commits_without_merge_commits]).to eq(nil)
end
end
end
describe 'new_blob_path' do
......
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