Sha256: e0c7930d115efe69f7686b11af4c0d52f566205c23e42d6d5b016f09f4be7490

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

module Stash
  class PullRequest
    class Diff
      def initialize(diff)
        @diff = diff
      end

      def filenames_with_added_lines
        diffs_with_added_lines.map do |diff|
          diff['destination']['toString']
        end
      end

      def added_line?(file, line)
        added_lines(file).include?(line)
      end

      private

      def diffs_with_added_lines
        @diff['diffs'].select do |diff|
          diff['destination'] &&
          diff['hunks'] &&
          diff['hunks'].find do |hunk|
            hunk['segments'].find do |segment|
              segment['type'] == 'ADDED'
            end
          end
        end
      end

      def added_lines(file)
        @added_lines ||= {}
        @added_lines[file] ||= begin
          file_diff(file)['hunks'].map do |hunk|
            hunk['segments'].select{|segment| segment['type'] == 'ADDED' }.map do |segment|
              segment['lines'].map do |line|
                line['destination']
              end
            end
          end.flatten
        end
      end

      def file_diff(file)
        @diff['diffs'].detect{|diff| diff['destination'] && diff['destination']['toString'] == file } || {'hunks' => []}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
face_control-1.0.0 lib/stash/pull_request/diff.rb
face_control-0.9.1 lib/stash/pull_request/diff.rb
face_control-0.8.6 lib/stash/pull_request/diff.rb
face_control-0.9.0 lib/stash/pull_request/diff.rb
face_control-0.8.5 lib/stash/pull_request/diff.rb