Class: Bundler::AuditedUpdate

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/audited_update.rb

Constant Summary collapse

CHANGELOG_URLS =
{
  "graphql-pro" => "https://github.com/rmosolgo/graphql-ruby/blob/master/CHANGELOG-pro.md",
  "sidekiq" => "https://github.com/mperham/sidekiq/blob/main/Changes.md",
  "faraday" => "https://github.com/lostisland/faraday/releases"
}

Instance Method Summary collapse

Instance Method Details

#added_gemsObject



244
245
246
# File 'lib/bundler/audited_update.rb', line 244

def added_gems
  @after_specs.reject { |spec| @before_specs.map(&:name).include?(spec.name) }
end

#bundle_updateObject



323
324
325
# File 'lib/bundler/audited_update.rb', line 323

def bundle_update
  Bundler::CLI::Update.new({}, []).run
end

#changed_gemsObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/bundler/audited_update.rb', line 252

def changed_gems
  gems = @after_specs.reject do |after_spec|
    before_spec = @before_specs.find { |before_spec| before_spec && before_spec.name == after_spec.name }
    !before_spec || before_spec.version == after_spec.version
  end

  gems.map! do |the_gem|
    before_gem = @before_specs.find { |before_spec| before_spec.name == the_gem.name }
    after_gem = @after_specs.find { |after_spec| after_spec.name == the_gem.name }
    versions = {
      before: Versionomy.parse(before_gem.version.to_s),
      after: Versionomy.parse(after_gem.version.to_s)
    }
    [the_gem.name, versions]
  end

  gems.to_h
end

#changelog_url_for(source_root, filename) ⇒ Object



294
295
296
297
# File 'lib/bundler/audited_update.rb', line 294

def changelog_url_for(source_root, filename)
  raw_source_root = source_root.gsub('github.com', 'raw.githubusercontent.com')
  url = "#{raw_source_root}/master/#{filename}"
end

#gem_info(name, version) ⇒ Object



305
306
307
308
309
310
311
312
313
# File 'lib/bundler/audited_update.rb', line 305

def gem_info(name, version)
  gem_url = "https://rubygems.org/api/v2/rubygems/#{name}/versions/#{version}"
  response = ::URI.parse(gem_url).read
  JSON.parse(response)
rescue OpenURI::HTTPError => e
  # return nil for 404 - which means the gem doens't exist on rubygems,
  # probably private
  raise unless e.message.include?('404')
end

#gem_output(name, version) ⇒ Object



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
172
173
174
175
176
177
178
179
180
# File 'lib/bundler/audited_update.rb', line 83

def gem_output(name, version)
  # gems that are continuously released and therefore have no helpful
  # changelog
  continuously_released_gems = %w[
    aws-partitions aws-sdk-core sorbet sorbet-runtime sorbet-static
                                  sorbet-static-and-runtime]

  if continuously_released_gems.include?(name)
    puts "\n\n\n"
    puts '--------------------------------'
    puts "#{name} updated"
    puts '--------------------------------'

    if version.is_a? Hash
      info = gem_info(name, version[:after])
      version_string = "#{version[:before]} -> #{version[:after]}"
    else
      info = gem_info(name, version)
      version_string = version
    end

    guessed_source = gem_source_url(info)
    change_detail = guessed_source

    puts 'This gem is continuously updated, with no meaningful changelog.'

    impact = nil
    while impact.nil?
      puts "Does #{name} #{version_string} impact your application? (y/n/[o]pen in browser)"
      answer = gets
      answer = answer.downcase.strip
      case answer
      when 'y'
        puts "What's a short description of the impact?"
        impact = gets
      when 'n'
        impact = 'No impact'
      when 'o'
        Launchy.open(guessed_source)
      else
        puts 'Invalid answer'
      end
    end

    change_detail = impact

  elsif version.is_a? Hash
    version_string = "#{version[:before]} -> #{version[:after]}"
    info = gem_info(name, version[:after])

    changelog_text, changelog_url = guess_changelog(
      name, gem_source_url(info)
    )

    if changelog_text && !changelog_text.empty?
      puts "\n\n\n"
      puts '--------------------------------'
      puts "#{name} changes from #{version_string}"
      puts '--------------------------------'

      # Output the changelog text from top until the line that contains the previous version
      changelog_output = changelog_text.split(/^.*#{Regexp.escape(version[:before].to_s)}/, 2).first

      # Max 200 lines
      changelog_output = changelog_output.lines.to_a[0...200].join

      puts changelog_output
      impact = nil
      while impact.nil?
        puts "Does #{name} #{version_string} impact your application? (y/n/[o]pen in browser)"
        answer = gets
        answer = answer.downcase.strip
        case answer
        when 'y'
          puts "What's a short description of the impact?"
          impact = gets
        when 'n'
          impact = 'No impact'
        when 'o'
          Launchy.open(changelog_url)
        else
          puts 'Invalid answer'
        end
      end

      change_detail = impact
    end

  else
    version_string = version
    info = gem_info(name, version)
    change_detail = gem_source_url(info)
  end

  change_detail ||= 'Unsupported source URL, cannot search for changelog'

  @output += "* #{name} (#{version_string}): #{change_detail}\n"
end

#gem_source_url(info) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
# File 'lib/bundler/audited_update.rb', line 232

def gem_source_url(info)
  return nil unless info

  source_url_guess = info['source_code_uri'] || info['homepage_uri']
  if source_url_guess&.include?('github.com')
    source_url_guess
  else
    # Unsupported source URL
    nil
  end
end

#gem_specsObject



315
316
317
318
319
320
321
# File 'lib/bundler/audited_update.rb', line 315

def gem_specs
  root     = File.expand_path(Dir.pwd)
  lockfile = Bundler::LockfileParser.new(
    File.read(File.join(root, 'Gemfile.lock'))
  )
  lockfile.specs
end

#github_releases_bodies(source_root) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/bundler/audited_update.rb', line 276

def github_releases_bodies(source_root)
  response = ::URI.parse(github_releases_url(source_root)).read
  releases = JSON.parse(response)
  release_notes = ''
  releases.each do |release|
    next unless release['body']

    release_notes += release['name']
    release_notes += "\n"
    release_notes += release['body']
    release_notes += "\n"
    release_notes += "\n"
  end
  release_notes
rescue OpenURI::HTTPError
  nil
end

#github_releases_url(source_root) ⇒ Object



271
272
273
274
# File 'lib/bundler/audited_update.rb', line 271

def github_releases_url(source_root)
  api_source_root = source_root.gsub('github.com/', 'api.github.com/repos/')
  api_source_root.end_with?("/releases") ? api_source_root : "#{api_source_root}/releases"
end

#guess_changelog(name, root_url) ⇒ Object



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
# File 'lib/bundler/audited_update.rb', line 182

def guess_changelog(name, root_url)
  # There are always going to be exceptions, so just hardcode those.
  root_url = CHANGELOG_URLS[name] if CHANGELOG_URLS.key?(name)

  return nil unless root_url

  filenames = %w[
    CHANGELOG
    CHANGELOG.md
    Changelog
    Changelog.md
    History
    History.md
    HISTORY.md
    History.rdoc
    Changes
    CHANGES
    CHANGES.md
    NEWS
  ]
  changelog_text = nil
  changelog_url = nil

  root_url_is_releases_page = root_url.end_with?("/releases")

  changelog_url = root_url if root_url_is_releases_page

  unless changelog_url
    filenames.each do |filename|
      changelog_text = try_changelog_url(root_url, filename)
      if changelog_text
        changelog_url = changelog_url_for(root_url, filename)
        break
      end
    end
  end

  unless changelog_text
    changelog_text = github_releases_bodies(root_url)
    changelog_url = github_releases_url(root_url) if changelog_text
  end

  unless changelog_text
    changelog_text = 'Could not find changelog URL, try manually'
    changelog_url = root_url
  end

  [changelog_text, changelog_url]
end

#output_changed_gems(gems) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/bundler/audited_update.rb', line 54

def output_changed_gems(gems)
  return if gems.empty?

  major_upgrades = gems.reject { |_, versions| versions[:before].major == versions[:after].major }
  minor_upgrades = gems.select do |name, versions|
    !major_upgrades.keys.include?(name) && versions[:before].minor != versions[:after].minor
  end
  point_upgrades = gems.reject do |name, _|
    major_upgrades.keys.include?(name) || minor_upgrades.keys.include?(name)
  end

  @output += "## Upgraded Gems\n"
  @output += "\n"

  output_changed_gems_section('Major', major_upgrades)
  output_changed_gems_section('Minor', minor_upgrades)
  output_changed_gems_section('Point', point_upgrades)

  @output += "\n"
end

#output_changed_gems_section(title, gems) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/bundler/audited_update.rb', line 75

def output_changed_gems_section(title, gems)
  @output += "### #{title} Upgrades\n"
  @output += "\n"
  gems.each { |name, versions| gem_output(name, versions) }

  @output += "\n"
end

#output_gems(title, gems) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/bundler/audited_update.rb', line 44

def output_gems(title, gems)
  return if gems.empty?

  @output += "## #{title}\n"
  @output += "\n"
  gems.each { |the_gem| gem_output(the_gem.name, the_gem.version) }

  @output += "\n"
end

#removed_gemsObject



248
249
250
# File 'lib/bundler/audited_update.rb', line 248

def removed_gems
  @before_specs.reject { |spec| @after_specs.map(&:name).include?(spec.name) }
end

#run!Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/bundler/audited_update.rb', line 21

def run!
  @before_specs = gem_specs
  bundle_update
  @after_specs = gem_specs

  @output = ''
  @output += "# Gem Changes\n"
  @output += "\n"

  output_gems('Added Gems', added_gems)
  output_gems('Removed Gems', removed_gems)
  output_changed_gems(changed_gems)

  puts "\n\n\n\n\n"

  puts '--------------------------------'
  puts 'Upgraded Gems'
  puts '(Generated with bundler-audited_updated https://github.com/bmulholland/audited_bundle_update)'
  puts '--------------------------------'

  puts @output
end

#try_changelog_url(source_root, filename) ⇒ Object



299
300
301
302
303
# File 'lib/bundler/audited_update.rb', line 299

def try_changelog_url(source_root, filename)
  ::URI.parse(changelog_url_for(source_root, filename)).read
rescue OpenURI::HTTPError
  nil
end