Sha256: 8bcd3f4072e91922f145f88e41108f49e35c14f94dee9cbc63afd65c597298e4

Contents?: true

Size: 1.45 KB

Versions: 10

Compression:

Stored size: 1.45 KB

Contents

# frozen_string_literal: true

module Unwrappr
  module Researchers
    # Compares the old version to the new via the Github API:
    # https://developer.github.com/v3/repos/commits/#compare-two-commits
    #
    # Implements the `gem_researcher` interface required by the
    # LockFileAnnotator.
    class GithubComparison
      def initialize(client)
        @client = client
      end

      def research(gem_change, change_info)
        return change_info if github_repo_not_identified?(change_info) ||
                              gem_added_or_removed?(gem_change)

        change_info.merge(
          github_comparison: try_comparing(
            repo: github_repo(change_info),
            base: gem_change.base_version,
            head: gem_change.head_version
          )
        )
      end

      private

      def try_comparing(repo:, base:, head:)
        comparison = compare(repo, "v#{base}", "v#{head}")
        comparison ||= compare(repo, base.to_s, head.to_s)
        comparison
      end

      def compare(repo, base, head)
        @client.compare(repo, base, head)
      rescue Octokit::NotFound
        nil
      end

      def github_repo_not_identified?(gem_change_info)
        github_repo(gem_change_info).nil?
      end

      def github_repo(gem_change_info)
        gem_change_info[:github_repo]
      end

      def gem_added_or_removed?(gem_change)
        gem_change.base_version.nil? || gem_change.head_version.nil?
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
unwrappr-0.8.2 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.8.1 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.8.0 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.7.0 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.6.0 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.5.0 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.4.0 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.3.5 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.3.4 lib/unwrappr/researchers/github_comparison.rb
unwrappr-0.3.3 lib/unwrappr/researchers/github_comparison.rb