Sha256: 50a32083ffc13b0aab6268e1f084cf87f8d2ec096b71240ed3502e6ab1f11cf3

Contents?: true

Size: 1.88 KB

Versions: 13

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

module Unwrappr
  module Writers
    # Inform of the number of commits included in the change. Annotate several
    # commits, and link to the Github compare page on which we can see all the
    # commits and file changes.
    #
    # Implements the `annotation_writer` interface required by the
    # LockFileAnnotator.
    class GithubCommitLog
      MAX_COMMITS = 10
      MAX_MESSAGE = 60
      SHA_LENGTH = 7

      def self.write(gem_change, gem_change_info)
        new(gem_change, gem_change_info).write
      end

      def initialize(gem_change, gem_change_info)
        @gem_change = gem_change
        @gem_change_info = gem_change_info
      end

      def write
        return nil if comparison.nil?

        collapsed_section('Commits', <<~MESSAGE)
          A change of **#{comparison.total_commits}** commits. See the full changes on [the compare page](#{comparison.html_url}).

          #{list_commits_introduction}
          #{commit_messages.join("\n")}
        MESSAGE
      end

      private

      def list_commits_introduction
        if comparison.commits.length > MAX_COMMITS
          "These are the first #{MAX_COMMITS} commits:"
        else
          'These are the individual commits:'
        end
      end

      def commit_messages
        comparison.commits.first(MAX_COMMITS).map(&method(:commit_message))
      end

      def commit_message(commit)
        message = commit.commit.message.lines.first.strip
        message = "#{message[0, MAX_MESSAGE]}…" if message.length > MAX_MESSAGE
        "- (#{commit.sha[0, SHA_LENGTH]}) [#{message}](#{commit.html_url})"
      end

      def comparison
        @gem_change_info[:github_comparison]
      end

      def collapsed_section(summary, body)
        <<~MESSAGE
          <details>
          <summary>#{summary}</summary>

          #{body}

          </details>
        MESSAGE
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
unwrappr-0.8.2 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.8.1 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.8.0 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.7.0 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.6.0 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.5.0 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.4.0 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.3.5 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.3.4 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.3.3 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.3.2 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.3.1 lib/unwrappr/writers/github_commit_log.rb
unwrappr-0.3.0 lib/unwrappr/writers/github_commit_log.rb