Sha256: 8ea6363030d3747ccf6ced70e3fa33a891fbd2a48d01882c708f03404f5253f0

Contents?: true

Size: 1.92 KB

Versions: 2

Compression:

Stored size: 1.92 KB

Contents

module Danger
  class MergedPullRequestFinder
    def initialize(specific_pull_request_id, git_logs)
      @specific_pull_request_id = specific_pull_request_id
      @git_logs = git_logs
    end

    def call
      check_if_any_merged_pull_request!

      [merged_pull_request_id, merged_pull_request_sha]
    end

    private

    attr_reader :specific_pull_request_id, :git_logs

    # @return [String] "#42"
    def pull_request_ref
      specific_pull_request_id ? "##{specific_pull_request_id}" : "".freeze
    end

    # @return [String] Log line of format: "Merge pull request #42"
    def most_recent_merged_pull_request
      @most_recent_merged_pull_request ||= begin
        git_logs.lines.grep(/Merge pull request #{pull_request_ref}/)[0]
      end
    end

    # @return [String] Log line of format: "description (#42)"
    def most_recent_squash_and_merged_pull_request
      @most_recent_squash_and_merged_pull_request ||= begin
        git_logs.lines.grep(/#{pull_request_ref}/)[0]
      end
    end

    # @return [String] Log line of most recent merged Pull Request
    def merged_pull_request
      return if pull_request_ref.empty?

      if most_recent_merged_pull_request
        most_recent_merged_pull_request
      elsif most_recent_squash_and_merged_pull_request
        most_recent_squash_and_merged_pull_request
      end
    end

    def check_if_any_merged_pull_request!
      if merged_pull_request.to_s.empty?
        if specific_pull_request_id
          raise "Could not find the Pull Request (#{specific_pull_request_id}) inside the git history for this repo."
        else
          raise "No recent Pull Requests found for this repo, danger requires at least one Pull Request for the local mode.".freeze
        end
      end
    end

    def merged_pull_request_id
      merged_pull_request.match(/#(?<id>[0-9]+)/)[:id]
    end

    def merged_pull_request_sha
      merged_pull_request.split(" ".freeze).first
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
danger-3.3.2 lib/danger/ci_source/support/merged_pull_request_finder.rb
danger-3.3.1 lib/danger/ci_source/support/merged_pull_request_finder.rb