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 content to locate input URL declarations and extract their components (scheme, owner, repo, ref). Only handles the shorthand URL schemes (github:, gitlab:, sourcehut:) since those are the ones where the ref appears inline in the URL string.

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.



53
54
55
56
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 53

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

Class Method Details

.find_input_url(content, input_name) ⇒ Object



43
44
45
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 43

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

.update_input_ref(content, input_name, new_ref) ⇒ Object



48
49
50
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 48

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

Instance Method Details

#findObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/dependabot/nix/flake_nix_parser.rb', line 59

def find
  match = find_url_match
  return unless match

  url_str = match[:url]

  # Try shorthand scheme first (github:, gitlab:, sourcehut:)
  url_match = FLAKE_URL_PATTERN.match(url_str)
  if url_match
    return InputUrl.new(
      full_url: url_str,
      scheme: T.must(url_match[:scheme]),
      owner: T.must(url_match[:owner]),
      repo: T.must(url_match[:repo]),
      ref: url_match[:ref],
      query: url_match[:query],
      match_start: match[:url_start],
      match_end: match[:url_end]
    )
  end

  # Try indirect/registry shorthand (e.g. nixpkgs/nixos-24.11)
  indirect_match = INDIRECT_URL_PATTERN.match(url_str)
  return unless indirect_match

  InputUrl.new(
    full_url: url_str,
    scheme: "indirect",
    owner: T.must(indirect_match[:id]),
    repo: "",
    ref: indirect_match[:ref],
    query: nil,
    match_start: match[:url_start],
    match_end: match[:url_end]
  )
end

#update_ref(new_ref) ⇒ Object



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

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