summaryrefslogtreecommitdiff
path: root/lib/gitlab/closing_issue_extractor.rb
blob: 4ba921569ad7b54b1cf43690ceb0a31f472e420b (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
# frozen_string_literal: true

module Gitlab
  class ClosingIssueExtractor
    ISSUE_CLOSING_REGEX = begin
      link_pattern = Banzai::Filter::AutolinkFilter::LINK_PATTERN

      pattern = Gitlab.config.gitlab.issue_closing_pattern
      pattern = pattern.sub('%{issue_ref}', "(?:(?:#{link_pattern})|(?:#{Issue.reference_pattern}))")
      Regexp.new(pattern).freeze
    end

    def initialize(project, current_user = nil)
      @extractor = Gitlab::ReferenceExtractor.new(project, current_user)
    end

    def closed_by_message(message)
      return [] if message.nil?

      closing_statements = []
      message.scan(ISSUE_CLOSING_REGEX) do
        closing_statements << Regexp.last_match[0]
      end

      @extractor.analyze(closing_statements.join(" "))

      @extractor.issues.reject do |issue|
        # Don't extract issues from the project this project was forked from
        @extractor.project.forked_from?(issue.project)
      end
    end
  end
end