Class: Dependabot::Nix::FlakeNixParser

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/nix/flake_nix_parser.rb

Overview

Parses flake.nix input URL declarations and updates their refs.

Defined Under Namespace

Classes: InputUrl

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(content, input_name) ⇒ FlakeNixParser

Returns a new instance of FlakeNixParser.



56
57
58
59
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 56

def initialize(content, input_name)
  @content = content
  @input_name = input_name
end

Class Method Details

.find_input_url(content, input_name) ⇒ Object



46
47
48
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 46

def self.find_input_url(content, input_name)
  new(content, input_name).find
end

.update_input_ref(content, input_name, new_ref) ⇒ Object



51
52
53
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 51

def self.update_input_ref(content, input_name, new_ref)
  new(content, input_name).update_ref(new_ref)
end

Instance Method Details

#build_git_input(url_str, match) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 75

def build_git_input(url_str, match)
  uri = URI.parse(url_str)
  return unless uri.scheme&.match?(GENERIC_GIT_SCHEME)

  ref = URI.decode_www_form(uri.query || "").find { |key, _value| key == "ref" }&.last

  InputUrl.new(
    full_url: url_str,
    scheme: "git",
    owner: "",
    repo: "",
    ref: ref,
    query: uri.query,
    match_start: match[:url_start],
    match_end: match[:url_end]
  )
rescue URI::InvalidURIError
  nil
end

#findObject



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 62

def find
  match = find_url_match
  return unless match

  url_str = match[:url]

  build_shorthand_input(url_str, match) ||
    build_tarball_input(url_str, match) ||
    build_git_input(url_str, match) ||
    build_indirect_input(url_str, match)
end

#update_ref(new_ref) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 96

def update_ref(new_ref)
  input_url = find
  return unless input_url
  return unless input_url.ref # nothing to update if no ref

  old_url = input_url.full_url
  new_url = build_updated_url(input_url, new_ref)

  updated = @content.dup
  # Replace within the known match boundaries to avoid accidental matches elsewhere
  updated[input_url.match_start...input_url.match_end] =
    T.must(updated[input_url.match_start...input_url.match_end]).sub(old_url, new_url)
  updated
end