Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2020-01-29 18:08:59 +0300
committerGitLab Bot <gitlab-bot@gitlab.com>2020-01-29 18:08:59 +0300
commit23288f62da73fb0e30d8e7ce306665e8fda1b932 (patch)
tree2baf1339e4d7c7c35d6b8a52cfb90597a5d4cdf1 /app/finders
parent7cc6872401eb487ed20dbb9d455f8bb9c97d9e39 (diff)
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/finders')
-rw-r--r--app/finders/context_commits_finder.rb62
1 files changed, 62 insertions, 0 deletions
diff --git a/app/finders/context_commits_finder.rb b/app/finders/context_commits_finder.rb
new file mode 100644
index 00000000000..f1b3eb43e84
--- /dev/null
+++ b/app/finders/context_commits_finder.rb
@@ -0,0 +1,62 @@
+# frozen_string_literal: true
+
+class ContextCommitsFinder
+ def initialize(project, merge_request, params = {})
+ @project = project
+ @merge_request = merge_request
+ @search = params[:search]
+ @limit = (params[:limit] || 40).to_i
+ @offset = (params[:offset] || 0).to_i
+ end
+
+ def execute
+ commits = init_collection
+ commits = filter_existing_commits(commits)
+
+ commits
+ end
+
+ private
+
+ attr_reader :project, :merge_request, :search, :limit, :offset
+
+ def init_collection
+ commits =
+ if search.present?
+ search_commits
+ else
+ project.repository.commits(merge_request.source_branch, { limit: limit, offset: offset })
+ end
+
+ commits
+ 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
+ commits = project.repository.find_commits_by_message(search, nil, nil, 20)
+ 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