unwrappr 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. checksums.yaml +7 -0
  2. data/.buildkite/pipeline.yml +38 -0
  3. data/.buildkite/steps/rspec.sh +9 -0
  4. data/.buildkite/steps/rubocop.sh +9 -0
  5. data/.gitignore +14 -0
  6. data/.rspec +3 -0
  7. data/.rubocop.yml +25 -0
  8. data/.travis.yml +5 -0
  9. data/CHANGELOG.md +9 -0
  10. data/CODE_OF_CONDUCT.md +74 -0
  11. data/Gemfile +8 -0
  12. data/Guardfile +16 -0
  13. data/LICENSE.txt +21 -0
  14. data/README.md +118 -0
  15. data/Rakefile +10 -0
  16. data/bin/console +11 -0
  17. data/bin/setup +8 -0
  18. data/exe/unwrappr +11 -0
  19. data/lib/unwrappr/bundler_command_runner.rb +25 -0
  20. data/lib/unwrappr/cli.rb +45 -0
  21. data/lib/unwrappr/gem_change.rb +54 -0
  22. data/lib/unwrappr/gem_version.rb +57 -0
  23. data/lib/unwrappr/git_command_runner.rb +81 -0
  24. data/lib/unwrappr/github/client.rb +75 -0
  25. data/lib/unwrappr/github/pr_sink.rb +28 -0
  26. data/lib/unwrappr/github/pr_source.rb +59 -0
  27. data/lib/unwrappr/lock_file_annotator.rb +65 -0
  28. data/lib/unwrappr/lock_file_comparator.rb +28 -0
  29. data/lib/unwrappr/lock_file_diff.rb +71 -0
  30. data/lib/unwrappr/octokit.rb +8 -0
  31. data/lib/unwrappr/researchers/composite.rb +21 -0
  32. data/lib/unwrappr/researchers/github_comparison.rb +43 -0
  33. data/lib/unwrappr/researchers/github_repo.rb +25 -0
  34. data/lib/unwrappr/researchers/ruby_gems_info.rb +17 -0
  35. data/lib/unwrappr/researchers/security_vulnerabilities.rb +50 -0
  36. data/lib/unwrappr/ruby_gems.rb +39 -0
  37. data/lib/unwrappr/spec_version_comparator.rb +19 -0
  38. data/lib/unwrappr/version.rb +5 -0
  39. data/lib/unwrappr/writers/composite.rb +21 -0
  40. data/lib/unwrappr/writers/github_commit_log.rb +72 -0
  41. data/lib/unwrappr/writers/project_links.rb +45 -0
  42. data/lib/unwrappr/writers/security_vulnerabilities.rb +109 -0
  43. data/lib/unwrappr/writers/title.rb +32 -0
  44. data/lib/unwrappr/writers/version_change.rb +58 -0
  45. data/lib/unwrappr.rb +32 -0
  46. data/unwrappr.gemspec +56 -0
  47. metadata +299 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 79e5cc78888cbad5bd38668addf51e451fb15074c2db7193e663950d77513fe2
4
+ data.tar.gz: '0024689eefd6ef862cf9249f833f2965a9d850263d0315624d61b998332afbb0'
5
+ SHA512:
6
+ metadata.gz: cd069f4dd6de94d0060e13b0ca118b1091965777bb4ebd0a57e3c1e445d32c278cacf419ce971aeb8b59be59ed5257a6df0c77b42ffcd8602a7ad80cba6ed0b6
7
+ data.tar.gz: 2b33c94c7896797288b6945368c4c0f5502ed26bd48d64dc9eb7cf032fe28d0b9fa61d97ad05534e04c55cc6670d8050dff2ad799672bb7ec665758e23626927
@@ -0,0 +1,38 @@
1
+ ---
2
+
3
+ steps:
4
+ - label: ":ruby: 2.5 :rspec:"
5
+ command: ".buildkite/steps/rspec.sh"
6
+ plugins:
7
+ docker#v1.2.1:
8
+ image: "ruby:2.5"
9
+ agents:
10
+ queue: "platform-docker-spot"
11
+ timeout_in_minutes: 5
12
+
13
+ - label: ":ruby: 2.4 :rspec:"
14
+ command: ".buildkite/steps/rspec.sh"
15
+ plugins:
16
+ docker#v1.2.1:
17
+ image: "ruby:2.4"
18
+ agents:
19
+ queue: "platform-docker-spot"
20
+ timeout_in_minutes: 5
21
+
22
+ - label: ":ruby: 2.3 :rspec:"
23
+ command: ".buildkite/steps/rspec.sh"
24
+ plugins:
25
+ docker#v1.2.1:
26
+ image: "ruby:2.3"
27
+ agents:
28
+ queue: "platform-docker-spot"
29
+ timeout_in_minutes: 5
30
+
31
+ - label: ":rubocop:"
32
+ command: ".buildkite/steps/rubocop.sh"
33
+ plugins:
34
+ docker#v1.2.1:
35
+ image: "ruby"
36
+ agents:
37
+ queue: "platform-docker-spot"
38
+ timeout_in_minutes: 5
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+
5
+ echo "--- :bundler: Bundling"
6
+ bundle install --path .bundle
7
+
8
+ echo "+++ :rspec: Running RSpec"
9
+ bundle exec rspec
@@ -0,0 +1,9 @@
1
+ #!/bin/bash
2
+
3
+ set -euo pipefail
4
+
5
+ echo "--- :bundler: Bundling"
6
+ bundle install --path .bundle
7
+
8
+ echo "+++ :rubocop: Running Rubocop"
9
+ bundle exec rubocop
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /error.txt
8
+ /pkg/
9
+ /spec/reports/
10
+ /stdout.txt
11
+ /tmp/
12
+
13
+ # rspec failure tracking
14
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,25 @@
1
+ ---
2
+ AllCops:
3
+ TargetRubyVersion: 2.5.1
4
+ Exclude:
5
+ - 'spike/*.rb'
6
+
7
+ Metrics/BlockLength:
8
+ Exclude:
9
+ - 'spec/**/*'
10
+ - 'test/**/*'
11
+
12
+ Metrics/LineLength:
13
+ Exclude:
14
+ - 'spec/**/*'
15
+ - 'test/**/*'
16
+
17
+ Metrics/ModuleLength:
18
+ Exclude:
19
+ - 'spec/**/*'
20
+ - 'test/**/*'
21
+
22
+ Style/Documentation:
23
+ Exclude:
24
+ - 'spec/**/*'
25
+ - 'test/**/*'
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,9 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+ ### Changed
9
+ - Preparing for release!
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at pete.johns@envato.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in unwrappr.gemspec
8
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ guard :rspec, cmd: 'bundle exec rspec' do
4
+ require 'guard/rspec/dsl'
5
+ dsl = Guard::RSpec::Dsl.new(self)
6
+
7
+ # RSpec files
8
+ rspec = dsl.rspec
9
+ watch(rspec.spec_helper) { rspec.spec_dir }
10
+ watch(rspec.spec_support) { rspec.spec_dir }
11
+ watch(rspec.spec_files)
12
+
13
+ # Ruby files
14
+ ruby = dsl.ruby
15
+ dsl.watch_spec_files_for(ruby.lib_files)
16
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Pete Johns
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # ![logo](https://user-images.githubusercontent.com/20217279/37953358-6847ed8a-31ee-11e8-9d3f-492e2574d7dc.png)
2
+
3
+ > `bundle update` PRs: Automated. Annotated.
4
+
5
+ Keeping dependencies up-to-date requires regular work. Some teams automate this,
6
+ others do it manually. This project seeks to reduce manual and cerebral labor
7
+ to get regular dependency updates into production.
8
+
9
+ ## Features
10
+
11
+ - Saves your team time in keeping dependencies up-to-date and understanding what's changed
12
+ - `unwrappr` runs `bundle update`, creates a GitHub Pull Request with the changes and annotates the differences in your project's `Gemfile.lock`
13
+ - Annotations include:
14
+ - Major, minor and patch-level changes
15
+ - Upgrades versus downgrades
16
+ - Vulnerability advisory information using [bundler-audit](https://github.com/rubysec/bundler-audit)
17
+ - Links to the home page, source code and change log (where available) of each gem
18
+
19
+ ## Development status [![Build status](https://badge.buildkite.com/d7db34f910131ff2a03d31dcc0ee960a3bc5f0df2c42ec4eb4.svg?branch=master&style=flat-square)](https://buildkite.com/envato-marketplaces/unwrappr)
20
+
21
+ `unwrappr` is used in many projects around [Envato][envato]
22
+ However, it is still undergoing development and features are likely to change
23
+ over time.
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run
26
+ `rake spec` to run the tests. You can also run `bin/console` for an interactive
27
+ prompt that will allow you to experiment.
28
+
29
+ To install this gem onto your local machine, run `bundle exec rake install`. To
30
+ release a new version, update the version number in `version.rb`, and then run
31
+ `bundle exec rake release`, which will create a git tag for the version, push
32
+ git commits and tags, and push the `.gem` file to
33
+ [rubygems.org](https://rubygems.org).
34
+
35
+
36
+ ## Getting started [![Gem version](https://img.shields.io/gem/v/unwrappr.svg?style=flat-square)](https://github.com/envato/unwrappr) [![Gem downloads](https://img.shields.io/gem/dt/unwrappr.svg?style=flat-square)](https://rubygems.org/gems/unwrappr)
37
+
38
+ ```
39
+ $ gem install unwrappr
40
+ ```
41
+
42
+ ## Configuration
43
+
44
+ `unwrappr` needs a [GitHub Personal Access Token](https://github.com/settings/tokens), stored in the environment as `GITHUB_TOKEN`.
45
+
46
+ To run `unwrappr` in the current working directory use...
47
+
48
+ ```bash
49
+ export GITHUB_TOKEN=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
50
+ unwrappr
51
+ ```
52
+
53
+ See https://github.com/settings/tokens to set up personal access tokens.
54
+
55
+ ## Requirements
56
+
57
+ - Ruby (tested against v2.4 and above)
58
+ - GitHub access (see Configuration section)
59
+
60
+ ## Contact ![Join the chat at https://gitter.im/envato/unwrappr](https://badges.gitter.im/Join%20Chat.svg)
61
+
62
+ - [GitHub project](https://github.com/envato/unwrappr)
63
+ - [Gitter chat room](https://gitter.im/envato/unwrappr)
64
+ - Bug reports and feature requests are welcome via [GitHub Issues](https://github.com/envato/unwrappr/issues)
65
+
66
+ ## Maintainers
67
+
68
+ - [Pete Johns](https://github.com/johnsyweb)
69
+ - [Joe Sustaric](https://github.com/joesustaric)
70
+
71
+ ## Authors
72
+
73
+ - [Pete Johns](https://github.com/johnsyweb)
74
+ - [Orien Madgwick](https://github.com/orien)
75
+ - [Joe Sustaric](https://github.com/joesustaric)
76
+ - [Vladimir Chervanev](https://github.com/vchervanev)
77
+ - [Em Esc](https://github.com/emesc)
78
+ - [Chun-wei Kuo](https://github.com/Domon)
79
+
80
+ ## License [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/envato/unwrappr/blob/master/LICENSE.txt)
81
+
82
+ `unwrappr` uses MIT license. See
83
+ [`LICENSE.txt`](https://github.com/envato/unwrappr/blob/master/LICENSE.txt) for
84
+ details.
85
+
86
+ ## Code of Conduct
87
+
88
+ We welcome contribution from everyone. Read more about it in
89
+ [`CODE_OF_CONDUCT.md`](https://github.com/envato/unwrappr/blob/master/CODE_OF_CONDUCT.md)
90
+
91
+ ## Contributing [![PRs welcome](https://img.shields.io/badge/PRs-welcome-orange.svg?style=flat-square)](https://github.com/envato/unwrappr/issues)
92
+
93
+ For bug fixes, documentation changes, and features:
94
+
95
+ 1. [Fork it](./fork)
96
+ 1. Create your feature branch (`git checkout -b my-new-feature`)
97
+ 1. Commit your changes (`git commit -am 'Add some feature'`)
98
+ 1. Push to the branch (`git push origin my-new-feature`)
99
+ 1. Create a new Pull Request
100
+
101
+ For larger new features: Do everything as above, but first also make contact with the project maintainers to be sure your change fits with the project direction and you won't be wasting effort going in the wrong direction.
102
+
103
+ ## About [![code with heart by Envato](https://img.shields.io/badge/%3C%2F%3E%20with%20%E2%99%A5%20by-Envato-ff69b4.svg?style=flat-square)](https://github.com/envato/unwrappr)
104
+
105
+ This project is maintained by the [Envato engineering team][webuild] and funded by [Envato][envato].
106
+
107
+ [<img src="http://opensource.envato.com/images/envato-oss-readme-logo.png" alt="Envato logo">][envato]
108
+
109
+ Encouraging the use and creation of open source software is one of the ways we
110
+ serve our community. See [our other projects][oss] or [come work with
111
+ us][careers] where you'll find an incredibly diverse, intelligent and capable
112
+ group of people who help make our company succeed and make our workplace fun,
113
+ friendly and happy.
114
+
115
+ [webuild]: https://webuild.envato.com?utm_source=github
116
+ [envato]: https://envato.com?utm_source=github
117
+ [oss]: https://opensource.envato.com/?utm_source=github
118
+ [careers]: https://envato.com/careers/?utm_source=github
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+ require 'rubocop/rake_task'
6
+
7
+ RuboCop::RakeTask.new
8
+ RSpec::Core::RakeTask.new(:spec)
9
+
10
+ task default: %i[rubocop spec]
data/bin/console ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'unwrappr'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ require 'pry'
11
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/unwrappr ADDED
@@ -0,0 +1,11 @@
1
+ #! /usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ $LOAD_PATH << File.expand_path('../lib', __dir__)
5
+
6
+ require 'unwrappr'
7
+
8
+ $stdout.sync = true
9
+ $stderr.sync = true
10
+
11
+ Unwrappr::CLI.run
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'safe_shell'
4
+
5
+ module Unwrappr
6
+ # Runs the bundle command. No surprises.
7
+ module BundlerCommandRunner
8
+ class << self
9
+ def bundle_update!
10
+ raise 'bundle update failed' unless updated_gems?
11
+ end
12
+
13
+ private
14
+
15
+ def updated_gems?
16
+ SafeShell.execute?(
17
+ 'bundle',
18
+ 'update',
19
+ stdout: 'stdout.txt',
20
+ stderr: 'error.txt'
21
+ )
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'clamp'
4
+
5
+ module Unwrappr
6
+ # Entry point for the app
7
+ class CLI < Clamp::Command
8
+ self.default_subcommand = 'all'
9
+
10
+ option ['--version', '-v'], :flag, 'Show version' do
11
+ puts "unwrappr v#{Unwrappr::VERSION}"
12
+ exit(0)
13
+ end
14
+
15
+ subcommand 'all', 'run bundle update, push to github, '\
16
+ 'create a pr and annotate changes' do
17
+ def execute
18
+ puts 'Doing the unwrappr thing...'
19
+ GitCommandRunner.create_branch!
20
+ BundlerCommandRunner.bundle_update!
21
+ GitCommandRunner.commit_and_push_changes!
22
+ GitHub::Client.make_pull_request!
23
+ end
24
+ end
25
+
26
+ subcommand 'annotate-pull-request',
27
+ 'Annotate Gemfile.lock changes in a Github pull request' do
28
+
29
+ option '--repo', 'REPO',
30
+ 'The repo in github <owner/project>',
31
+ required: true
32
+
33
+ option '--pr', 'PR',
34
+ 'The github PR number',
35
+ required: true
36
+
37
+ def execute
38
+ LockFileAnnotator.annotate_github_pull_request(
39
+ repo: repo,
40
+ pr_number: pr.to_i
41
+ )
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Unwrappr
6
+ # Represents a gem change in a Gemfile.lock diff.
7
+ class GemChange
8
+ extend Forwardable
9
+
10
+ def initialize(
11
+ name:, head_version:, base_version:, line_number:, lock_file_diff:
12
+ )
13
+ @name = name
14
+ @head_version = head_version
15
+ @base_version = base_version
16
+ @line_number = line_number
17
+ @lock_file_diff = lock_file_diff
18
+ end
19
+
20
+ attr_reader :name, :head_version, :base_version, :line_number
21
+ def_delegators :@lock_file_diff, :filename, :sha
22
+
23
+ def added?
24
+ (head_version && base_version.nil?)
25
+ end
26
+
27
+ def removed?
28
+ (base_version && head_version.nil?)
29
+ end
30
+
31
+ def major?
32
+ head_version && base_version &&
33
+ head_version.major_difference?(base_version)
34
+ end
35
+
36
+ def minor?
37
+ head_version && base_version &&
38
+ head_version.minor_difference?(base_version)
39
+ end
40
+
41
+ def patch?
42
+ head_version && base_version &&
43
+ head_version.patch_difference?(base_version)
44
+ end
45
+
46
+ def upgrade?
47
+ head_version && base_version && (head_version > base_version)
48
+ end
49
+
50
+ def downgrade?
51
+ head_version && base_version && (head_version < base_version)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Unwrappr
4
+ # Represents the version of a gem. Helps in comparing two versions to
5
+ # identify differences and extracting the major, minor and patch components
6
+ # that make up semantic versioning. https://semver.org/
7
+ class GemVersion
8
+ include Comparable
9
+
10
+ def initialize(version_string)
11
+ @version_string = version_string
12
+ @version = Gem::Version.create(version_string)
13
+ @major = segment(0)
14
+ @minor = segment(1)
15
+ @patch = segment(2)
16
+ end
17
+
18
+ attr_reader :major, :minor, :patch, :version
19
+
20
+ def major_difference?(other)
21
+ (major != other.major)
22
+ end
23
+
24
+ def minor_difference?(other)
25
+ (major == other.major) &&
26
+ (minor != other.minor)
27
+ end
28
+
29
+ def patch_difference?(other)
30
+ (major == other.major) &&
31
+ (minor == other.minor) &&
32
+ (patch != other.patch)
33
+ end
34
+
35
+ def <=>(other)
36
+ @version <=> other.version
37
+ end
38
+
39
+ def to_s
40
+ @version_string
41
+ end
42
+
43
+ private
44
+
45
+ def segment(index)
46
+ segment = @version.canonical_segments[index] || 0
47
+ (segment.is_a?(Numeric) ? segment : nil)
48
+ rescue NoMethodError
49
+ abort(<<~MESSAGE)
50
+ Unwrappr requires RubyGems v2.7.0 or newer.
51
+
52
+ To upgrade to the latest RubyGems visit https://rubygems.org/pages/download
53
+
54
+ MESSAGE
55
+ end
56
+ end
57
+ end