Class: Atlassian::JiraIssueKeyExtractors::Branch

Inherits:
Object
  • Object
show all
Defined in:
lib/atlassian/jira_issue_key_extractors/branch.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, branch_name) ⇒ Branch

Returns a new instance of Branch.



10
11
12
13
# File 'lib/atlassian/jira_issue_key_extractors/branch.rb', line 10

def initialize(project, branch_name)
  @project = project
  @branch_name = branch_name
end

Class Method Details

.has_keys?(project, branch_name) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
# File 'lib/atlassian/jira_issue_key_extractors/branch.rb', line 6

def self.has_keys?(project, branch_name)
  new(project, branch_name).issue_keys.any?
end

Instance Method Details

#issue_keysObject

Extract Jira issue keys from the branch name and associated open merge request. Use BatchLoader to load this data without N+1 queries when serializing multiple branches in ‘Atlassian::JiraConnect::Serializers::BranchEntity`.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/atlassian/jira_issue_key_extractors/branch.rb', line 18

def issue_keys
  BatchLoader.for(branch_name).batch do |branch_names, loader|
    merge_requests = MergeRequest
      .select(:description, :source_branch, :title)
      .from_project(project)
      .from_source_branches(branch_names)
      .opened

    branch_names.each do |branch_name|
      related_merge_request = merge_requests.find { |mr| mr.source_branch == branch_name }

      key_sources = [branch_name, related_merge_request&.title, related_merge_request&.description].compact
      issue_keys = JiraIssueKeyExtractor.new(key_sources).issue_keys

      loader.call(branch_name, issue_keys)
    end
  end
end