closing_issue_extractor.rb 968 Bytes
Newer Older
1 2
# frozen_string_literal: true

3
module Gitlab
4
  class ClosingIssueExtractor
5
    ISSUE_CLOSING_REGEX = begin
6
      link_pattern = Banzai::Filter::AutolinkFilter::LINK_PATTERN
7

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

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

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

20 21 22 23
      closing_statements = []
      message.scan(ISSUE_CLOSING_REGEX) do
        closing_statements << Regexp.last_match[0]
      end
24

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

27
      @extractor.issues.reject do |issue|
28 29
        # Don't extract issues from the project this project was forked from
        @extractor.project.forked_from?(issue.project)
30
      end
31 32 33
    end
  end
end