Class: Homebrew::DevCmd::BumpFormulaPr Private

Inherits:
AbstractCommand show all
Defined in:
dev-cmd/bump-formula-pr.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/bump_formula_pr.rbi

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Defined Under Namespace

Classes: Args

Instance Method Summary collapse

Methods inherited from AbstractCommand

command, command_name, dev_cmd?, #initialize, parser, ruby_cmd?

Constructor Details

This class inherits a constructor from Homebrew::AbstractCommand

Instance Method Details

#argsHomebrew::DevCmd::BumpFormulaPr::Args

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



10
# File 'sorbet/rbi/dsl/homebrew/dev_cmd/bump_formula_pr.rbi', line 10

def args; end

#runvoid

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

This method returns an undefined value.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
# File 'dev-cmd/bump-formula-pr.rb', line 95

def run
  if args.revision.present? && args.tag.nil? && args.version.nil?
    raise UsageError, "`--revision` must be passed with either `--tag` or `--version`!"
  end

  # As this command is simplifying user-run commands then let's just use a
  # user path, too.
  ENV["PATH"] = PATH.new(ORIGINAL_PATHS).to_s

  # Use the user's browser, too.
  ENV["BROWSER"] = Homebrew::EnvConfig.browser

  formula = args.named.to_formulae.first
  raise FormulaUnspecifiedError if formula.blank?

  odie "This formula is disabled!" if formula.disabled?
  odie "This formula is deprecated and does not build!" if formula.deprecation_reason == :does_not_build
  tap = formula.tap
  odie "This formula is not in a tap!" if tap.blank?
  odie "This formula's tap is not a Git repository!" unless tap.git?

  odie <<~EOS unless tap.allow_bump?(formula.name)
    Whoops, the #{formula.name} formula has its version update
    pull requests automatically opened by BrewTestBot every ~3 hours!
    We'd still love your contributions, though, so try another one
    that is excluded from autobump list (i.e. it has 'no_autobump!'
    method or 'livecheck' block with 'skip'.)
  EOS

  odie "You have too many PRs open: close or merge some first!" if GitHub.too_many_open_prs?(tap)

  formula_spec = formula.stable
  odie "#{formula}: no stable specification found!" if formula_spec.blank?

  # This will be run by `brew audit` later so run it first to not start
  # spamming during normal output.
  Homebrew.install_bundler_gems!(groups: ["audit", "style"]) unless args.no_audit?

  tap_remote_repo = T.must(tap.remote_repository)
  remote = "origin"
  remote_branch = tap.git_repository.origin_branch_name
  previous_branch = "-"

  check_pull_requests(formula, tap_remote_repo, state: "open")

  all_formulae = []
  if args.bump_synced.present?
    Array(args.bump_synced).each do |formula_name|
      all_formulae << formula_name
    end
  else
    all_formulae << args.named.first.to_s
  end

  return if all_formulae.empty?

  commits = all_formulae.filter_map do |formula_name|
    commit_formula = Formula[formula_name]
    raise FormulaUnspecifiedError if commit_formula.blank?

    commit_formula_spec = commit_formula.stable
    odie "#{commit_formula}: no stable specification found!" if commit_formula_spec.blank?

    formula_pr_message = ""

    new_url = args.url
    new_version = args.version
    check_new_version(commit_formula, tap_remote_repo, version: new_version) if new_version.present?

    opoo "This formula has patches that may be resolved upstream." if commit_formula.patchlist.present?
    if commit_formula.resources.any? { |resource| !resource.name.start_with?("homebrew-") }
      opoo "This formula has resources that may need to be updated."
    end

    old_mirrors = commit_formula_spec.mirrors
    new_mirrors ||= args.mirror
    if new_url.present? && (new_mirror = determine_mirror(new_url))
      new_mirrors ||= [new_mirror]
      check_for_mirrors(commit_formula.name, old_mirrors, new_mirrors)
    end

    old_hash = commit_formula_spec.checksum&.hexdigest
    new_hash = args.sha256
    new_tag = args.tag
    new_revision = args.revision
    old_url = T.must(commit_formula_spec.url)
    old_tag = commit_formula_spec.specs[:tag]
    old_formula_version = formula_version(commit_formula)
    old_version = old_formula_version.to_s
    forced_version = new_version.present?
    new_url_hash = if new_url.present? && new_hash.present?
      check_new_version(commit_formula, tap_remote_repo, url: new_url) if new_version.blank?
      true
    elsif new_tag.present? && new_revision.present?
      check_new_version(commit_formula, tap_remote_repo, url: old_url, tag: new_tag) if new_version.blank?
      false
    elsif old_hash.blank?
      if new_tag.blank? && new_version.blank? && new_revision.blank?
        raise UsageError, "#{formula}: no `--tag` or `--version` argument specified!"
      end

      if old_tag.present?
        new_tag ||= old_tag.gsub(old_version, new_version)
        if new_tag == old_tag
          odie <<~EOS
            You need to bump this formula manually since the new tag
            and old tag are both #{new_tag}.
          EOS
        end
        check_new_version(commit_formula, tap_remote_repo, url: old_url, tag: new_tag) if new_version.blank?
        resource_path, forced_version = fetch_resource_and_forced_version(commit_formula, new_version, old_url,
                                                                          tag: new_tag)
        new_revision = Utils.popen_read("git", "-C", resource_path.to_s, "rev-parse", "-q", "--verify", "HEAD")
        new_revision = new_revision.strip
      elsif new_revision.blank?
        odie "#{commit_formula}: the current URL requires specifying a `--revision=` argument."
      end
      false
    elsif new_url.blank? && new_version.blank?
      raise UsageError, "#{commit_formula}: no `--url` or `--version` argument specified!"
    else
      next unless new_version.present?

      new_url ||= PyPI.update_pypi_url(old_url, new_version)
      if new_url.blank?
        new_url = update_url(old_url, old_version, new_version)
        if new_mirrors.blank? && old_mirrors.present?
          new_mirrors = old_mirrors.map do |old_mirror|
            update_url(old_mirror, old_version, new_version)
          end
        end
      end
      if new_url == old_url
        odie <<~EOS
          You need to bump this formula manually since the new URL
          and old URL are both:
            #{new_url}
        EOS
      end
      check_new_version(commit_formula, tap_remote_repo, url: new_url) if new_version.blank?
      resource_path, forced_version = fetch_resource_and_forced_version(commit_formula, new_version, new_url)
      Utils::Tar.validate_file(resource_path)
      new_hash = resource_path.sha256
    end

    replacement_pairs = []
    if commit_formula.revision.nonzero?
      replacement_pairs << [
        /^  revision \d+\n(\n(  head "))?/m,
        "\\2",
      ]
    end

    replacement_pairs += commit_formula_spec.mirrors.map do |mirror|
      [
        / +mirror "#{Regexp.escape(mirror)}"\n/m,
        "",
      ]
    end

    replacement_pairs += if new_url_hash.present?
      [
        [
          /#{Regexp.escape(T.must(commit_formula_spec.url))}/,
          new_url,
        ],
        [
          old_hash,
          new_hash,
        ],
      ]
    elsif new_tag.present?
      [
        [
          /tag:(\s+")#{commit_formula_spec.specs[:tag]}(?=")/,
          "tag:\\1#{new_tag}\\2",
        ],
        [
          commit_formula_spec.specs[:revision],
          new_revision,
        ],
      ]
    elsif new_url.present?
      [
        [
          /#{Regexp.escape(T.must(commit_formula_spec.url))}/,
          new_url,
        ],
        [
          commit_formula_spec.specs[:revision],
          new_revision,
        ],
      ]
    else
      [
        [
          commit_formula_spec.specs[:revision],
          new_revision,
        ],
      ]
    end

    old_contents = commit_formula.path.read

    if new_mirrors.present? && new_url.present?
      replacement_pairs << [
        /^( +)(url "#{Regexp.escape(new_url)}"[^\n]*?\n)/m,
        "\\1\\2\\1mirror \"#{new_mirrors.join("\"\n\\1mirror \"")}\"\n",
      ]
    end

    if forced_version && new_version != "0"
      replacement_pairs << if old_contents.include?("version \"#{old_formula_version}\"")
        [
          "version \"#{old_formula_version}\"",
          "version \"#{new_version}\"",
        ]
      elsif new_mirrors.present?
        [
          /^( +)(mirror "#{Regexp.escape(new_mirrors.last)}"\n)/m,
          "\\1\\2\\1version \"#{new_version}\"\n",
        ]
      elsif new_url.present?
        [
          /^( +)(url "#{Regexp.escape(new_url)}"[^\n]*?\n)/m,
          "\\1\\2\\1version \"#{new_version}\"\n",
        ]
      elsif new_revision.present?
        [
          /^( {2})( +)(:revision => "#{new_revision}"\n)/m,
          "\\1\\2\\3\\1version \"#{new_version}\"\n",
        ]
      end
    elsif forced_version && new_version == "0"
      replacement_pairs << [
        /^  version "[\w.\-+]+"\n/m,
        "",
      ]
    end
    new_contents = Utils::Inreplace.inreplace_pairs(commit_formula.path,
                                                    replacement_pairs.uniq.compact,
                                                    read_only_run: args.dry_run?,
                                                    silent:        args.quiet?)

    new_formula_version = formula_version(commit_formula, new_contents)

    if new_formula_version < old_formula_version
      commit_formula.path.atomic_write(old_contents) unless args.dry_run?
      odie <<~EOS
        You need to bump this formula manually since changing the version
        from #{old_formula_version} to #{new_formula_version} would be a downgrade.
      EOS
    elsif new_formula_version == old_formula_version
      commit_formula.path.atomic_write(old_contents) unless args.dry_run?
      odie <<~EOS
        You need to bump this formula manually since the new version
        and old version are both #{new_formula_version}.
      EOS
    end

    alias_rename = alias_update_pair(commit_formula, new_formula_version)
    if alias_rename.present?
      ohai "Renaming alias #{alias_rename.first} to #{alias_rename.last}"
      alias_rename.map! { |a| tap.alias_dir/a }
    end

    unless args.dry_run?
      resources_checked = PyPI.update_python_resources! formula,
                                                        version:                  new_formula_version.to_s,
                                                        package_name:             args.python_package_name,
                                                        extra_packages:           args.python_extra_packages,
                                                        exclude_packages:         args.python_exclude_packages,
                                                        install_dependencies:     args.install_dependencies?,
                                                        silent:                   args.quiet?,
                                                        ignore_non_pypi_packages: true

      update_matching_version_resources! commit_formula,
                                         version: new_formula_version.to_s
    end

    if resources_checked.nil? && commit_formula.resources.any? do |resource|
      resource.livecheck.formula != :parent && !resource.name.start_with?("homebrew-")
    end
      formula_pr_message += <<~EOS


        - [ ] `resource` blocks have been checked for updates.
      EOS
    end

    if new_url =~ %r{^https://github\.com/([\w-]+)/([\w-]+)/archive/refs/tags/(v?[.0-9]+)\.tar\.}
      owner = Regexp.last_match(1)
      repo = Regexp.last_match(2)
      tag = Regexp.last_match(3)
      github_release_data = begin
        GitHub::API.open_rest("#{GitHub::API_URL}/repos/#{owner}/#{repo}/releases/tags/#{tag}")
      rescue GitHub::API::HTTPNotFoundError
        # If this is a 404: we can't do anything.
        nil
      end

      if github_release_data.present?
        pre = "pre" if github_release_data["prerelease"].present?
        formula_pr_message += <<~XML
          <details>
            <summary>#{pre}release notes</summary>
            <pre>#{github_release_data["body"]}</pre>
          </details>
        XML
      end
    end

    {
      sourcefile_path:    commit_formula.path,
      old_contents:,
      commit_message:     "#{commit_formula.name} #{args.version}",
      additional_files:   alias_rename,
      formula_pr_message:,
      formula_name:       commit_formula.name,
      new_version:        new_formula_version,
    }
  end

  commits.each do |commit|
    commit_formula = Formula[commit[:formula_name]]
    # For each formula, run `brew audit` to check for any issues.
    audit_result = run_audit(commit_formula, commit[:additional_files],
                             skip_synced_versions: args.bump_synced.present?)

    next unless audit_result

    # If `brew audit` fails, revert the changes made to any formula.
    commits.each do |revert|
      revert_formula = Formula[revert[:formula_name]]
      revert_formula.path.atomic_write(revert[:old_contents]) unless args.dry_run?
      revert_alias_rename = revert[:additional_files]
      if revert_alias_rename && (source = revert_alias_rename.first) && (destination = revert_alias_rename.last)
        FileUtils.mv source, destination
      end
    end

    odie "`brew audit` failed for #{commit[:formula_name]}!"
  end

  new_formula_version = T.must(commits.first)[:new_version]
  pr_title = if args.bump_synced.nil?
    "#{formula.name} #{new_formula_version}"
  else
    "#{Array(args.bump_synced).join(" ")} #{new_formula_version}"
  end

  pr_message = "Created with `brew bump-formula-pr`."
  commits.each do |commit|
    next if commit[:formula_pr_message].empty?

    pr_message += "<h4>#{commit[:formula_name]}</h4>" if commits.length != 1
    pr_message += "#{commit[:formula_pr_message]}<hr>"
  end

  pr_info = {
    commits:,
    remote:,
    remote_branch:,
    branch_name:     "bump-#{formula.name}-#{new_formula_version}",
    pr_title:,
    previous_branch:,
    tap:             tap,
    tap_remote_repo:,
    pr_message:,
  }
  GitHub.create_bump_pr(pr_info, args:)
end