Class: Homebrew::DevCmd::BumpCaskPr Private

Inherits:
AbstractCommand show all
Defined in:
dev-cmd/bump-cask-pr.rb,
sorbet/rbi/dsl/homebrew/dev_cmd/bump_cask_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::BumpCaskPr::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_cask_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.



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
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
# File 'dev-cmd/bump-cask-pr.rb', line 60

def run
  # This will be run by `brew audit` or `brew style` later so run it first to
  # not start spamming during normal output.
  gem_groups = []
  gem_groups << "style" if !args.no_audit? || !args.no_style?
  gem_groups << "audit" unless args.no_audit?
  Homebrew.install_bundler_gems!(groups: gem_groups) unless gem_groups.empty?

  # 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"] = EnvConfig.browser

  cask = args.named.to_casks.first

  odie "This cask is not in a tap!" if cask.tap.blank?
  odie "This cask's tap is not a Git repository!" unless cask.tap.git?

  odie <<~EOS unless cask.tap.allow_bump?(cask.token)
    Whoops, the #{cask.token} cask 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's not in the autobump list:
      #{Formatter.url("#{cask.tap.remote}/blob/master/.github/autobump.txt")}
  EOS

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

  new_version = BumpVersionParser.new(
    general: args.version,
    intel:   args.version_intel,
    arm:     args.version_arm,
  )

  new_hash = unless (new_hash = args.sha256).nil?
    raise UsageError, "`--sha256` must not be empty." if new_hash.blank?

    ["no_check", ":no_check"].include?(new_hash) ? :no_check : new_hash
  end

  new_base_url = unless (new_base_url = args.url).nil?
    raise UsageError, "`--url` must not be empty." if new_base_url.blank?

    begin
      URI(new_base_url)
    rescue URI::InvalidURIError
      raise UsageError, "`--url` is not valid."
    end
  end

  if new_version.blank? && new_base_url.nil? && new_hash.nil?
    raise UsageError, "No `--version`, `--url` or `--sha256` argument specified!"
  end

  check_pull_requests(cask, new_version:)

  replacement_pairs ||= []
  branch_name = "bump-#{cask.token}"
  commit_message = nil

  old_contents = File.read(cask.sourcefile_path)

  if new_base_url
    commit_message ||= "#{cask.token}: update URL"

    m = /^ +url "(.+?)"\n/m.match(old_contents)
    odie "Could not find old URL in cask!" if m.nil?

    old_base_url = m.captures.fetch(0)

    replacement_pairs << [
      /#{Regexp.escape(old_base_url)}/,
      new_base_url.to_s,
    ]
  end

  if new_version.present?
    # For simplicity, our naming defers to the arm version if we multiple architectures are specified
    branch_version = new_version.arm || new_version.general
    if branch_version.is_a?(Cask::DSL::Version)
      commit_version = shortened_version(branch_version, cask:)
      branch_name = "bump-#{cask.token}-#{branch_version.tr(",:", "-")}"
      commit_message ||= "#{cask.token} #{commit_version}"
    end
    replacement_pairs = replace_version_and_checksum(cask, new_hash, new_version, replacement_pairs)
  end
  # Now that we have all replacement pairs, we will replace them further down

  commit_message ||= "#{cask.token}: update checksum" if new_hash

  # Remove nested arrays where elements are identical
  replacement_pairs = replacement_pairs.reject { |pair| pair[0] == pair[1] }.uniq.compact
  Utils::Inreplace.inreplace_pairs(cask.sourcefile_path,
                                   replacement_pairs,
                                   read_only_run: args.dry_run?,
                                   silent:        args.quiet?)

  run_cask_audit(cask, old_contents)
  run_cask_style(cask, old_contents)

  pr_info = {
    branch_name:,
    commit_message:,
    old_contents:,
    pr_message:      "Created with `brew bump-cask-pr`.",
    sourcefile_path: cask.sourcefile_path,
    tap:             cask.tap,
  }
  GitHub.create_bump_pr(pr_info, args:)
end