summaryrefslogtreecommitdiff
path: root/app/services/issues/related_branches_service.rb
blob: 76af482b7ac4af62888de38d8fec36b88ba3e452 (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
# frozen_string_literal: true

# This service fetches all branches containing the current issue's ID, except for
# those with a merge request open referencing the current issue.
module Issues
  class RelatedBranchesService < Issues::BaseService
    def execute(issue)
      branches_with_iid_of(issue) - branches_with_merge_request_for(issue)
    end

    private

    def branches_with_merge_request_for(issue)
      Issues::ReferencedMergeRequestsService
        .new(project, current_user)
        .referenced_merge_requests(issue)
        .map(&:source_branch)
    end

    def branches_with_iid_of(issue)
      project.repository.branch_names.select do |branch|
        branch =~ /\A#{issue.iid}-(?!\d+-stable)/i
      end
    end
  end
end