2020-03-13 15:44:24 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ContextCommitsFinder
|
|
|
|
def initialize(project, merge_request, params = {})
|
|
|
|
@project = project
|
|
|
|
@merge_request = merge_request
|
|
|
|
@search = params[:search]
|
2022-10-11 01:57:18 +05:30
|
|
|
@author = params[:author]
|
|
|
|
@committed_before = params[:committed_before]
|
|
|
|
@committed_after = params[:committed_after]
|
2020-03-13 15:44:24 +05:30
|
|
|
@limit = (params[:limit] || 40).to_i
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
commits = init_collection
|
2021-04-29 21:17:54 +05:30
|
|
|
filter_existing_commits(commits)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2022-10-11 01:57:18 +05:30
|
|
|
attr_reader :project, :merge_request, :search, :author, :committed_before, :committed_after, :limit
|
2020-03-13 15:44:24 +05:30
|
|
|
|
|
|
|
def init_collection
|
2021-04-29 21:17:54 +05:30
|
|
|
if search.present?
|
|
|
|
search_commits
|
|
|
|
else
|
2022-10-11 01:57:18 +05:30
|
|
|
project.repository.commits(merge_request.target_branch, { limit: limit })
|
2021-04-29 21:17:54 +05:30
|
|
|
end
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
def filter_existing_commits(commits)
|
|
|
|
commits.select! { |commit| already_included_ids.exclude?(commit.id) }
|
|
|
|
commits
|
|
|
|
end
|
|
|
|
|
|
|
|
def search_commits
|
|
|
|
key = search.strip
|
|
|
|
commits = []
|
|
|
|
if Commit.valid_hash?(key)
|
|
|
|
mr_existing_commits_ids = merge_request.commits.map(&:id)
|
|
|
|
if mr_existing_commits_ids.exclude? key
|
|
|
|
commit_by_sha = project.repository.commit(key)
|
|
|
|
commits = [commit_by_sha] if commit_by_sha
|
|
|
|
end
|
|
|
|
else
|
2022-10-11 01:57:18 +05:30
|
|
|
commits = project.repository.list_commits_by(search, merge_request.target_branch,
|
|
|
|
author: author, before: committed_before, after: committed_after, limit: limit)
|
2020-03-13 15:44:24 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
commits
|
|
|
|
end
|
|
|
|
|
|
|
|
def already_included_ids
|
|
|
|
mr_existing_commits_ids = merge_request.commits.map(&:id)
|
|
|
|
mr_context_commits_ids = merge_request.context_commits.map(&:id)
|
|
|
|
|
|
|
|
mr_existing_commits_ids + mr_context_commits_ids
|
|
|
|
end
|
|
|
|
end
|