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

context_commits_finder.rb « finders « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f1b3eb43e84c277b5a3c0621107b49c7490d8107 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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