Class: Dependabot::PullRequestCreator::MessageBuilder::IssueLinker

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/pull_request_creator/message_builder/issue_linker.rb

Constant Summary collapse

REPO_REGEX =
%r{(?<repo>[\w.-]+/(?:(?!\.git|\.\s)[\w.-])+)}.freeze
TAG_REGEX =
/(?<tag>(?:\#|GH-)\d+)/i.freeze
[
  /
    (?:(?<=[^A-Za-z0-9\[\\]|^)\\*#{TAG_REGEX}(?=[^A-Za-z0-9\-]|$))|
    (?:(?<=\s|^)#{REPO_REGEX}#{TAG_REGEX}(?=[^A-Za-z0-9\-]|$))
  /x.freeze,
  /\[#{TAG_REGEX}\](?=[^A-Za-z0-9\-\(])/.freeze,
  /\[(?<tag>(?:\#|GH-)?\d+)\]\(\)/i.freeze
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_url:) ⇒ IssueLinker

Returns a new instance of IssueLinker.


22
23
24
# File 'lib/dependabot/pull_request_creator/message_builder/issue_linker.rb', line 22

def initialize(source_url:)
  @source_url = source_url
end

Instance Attribute Details

#source_urlObject (readonly)

Returns the value of attribute source_url


20
21
22
# File 'lib/dependabot/pull_request_creator/message_builder/issue_linker.rb', line 20

def source_url
  @source_url
end

Instance Method Details


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/dependabot/pull_request_creator/message_builder/issue_linker.rb', line 26

def link_issues(text:)
  # Loop through each of the issue link regexes, replacing any instances
  # of them with an absolute link that uses the source URL
  ISSUE_LINK_REGEXS.reduce(text) do |updated_text, regex|
    updated_text.gsub(regex) do |issue_link|
      tag = issue_link.
            match(/(?<tag>(?:\#|GH-)?\d+)/i).
            named_captures.fetch("tag")
      number = tag.match(/\d+/).to_s

      repo = issue_link.
             match("#{REPO_REGEX}#{TAG_REGEX}")&.
             named_captures&.
             fetch("repo", nil)
      source = repo ? "https://github.com/#{repo}" : source_url

      "[#{repo ? (repo + tag) : tag}](#{source}/issues/#{number})"
    end
  end
end