Sha256: 86967214068bdff7c0a0ed452764c801c515f58112836512f4cc966054023d1f

Contents?: true

Size: 1.6 KB

Versions: 7

Compression:

Stored size: 1.6 KB

Contents

module CodeClimate
  module TestReporter
    InvalidPayload = Class.new(StandardError)

    class PayloadValidator
      def initialize(payload)
        @payload = payload
      end

      def self.validate(payload)
        new(payload).validate
      end

      def validate
        raise InvalidPayload, "A git commit sha was not found in the test report payload" unless commit_sha
        raise InvalidPayload, "A git commit timestamp was not found in the test report payload" unless committed_at
        raise InvalidPayload, "A run at timestamp was not found in the test report payload" unless run_at
        raise InvalidPayload, "No source files were found in the test report payload" unless source_files?
        raise InvalidPayload, "Invalid source files were found in the test report payload" unless valid_source_files?
        true
      end

      private

      def commit_sha
        commit_sha_from_git || commit_sha_from_ci_service
      end

      def committed_at
        @payload[:git] && @payload[:git][:committed_at]
      end

      def run_at
        @payload[:run_at]
      end

      def source_files?
        @payload[:source_files] && @payload[:source_files].any?
      end

      def valid_source_files?
        @payload[:source_files].all? { |s| valid_source_file?(s) }
      end

      def valid_source_file?(file)
        file.is_a?(Hash) && file[:coverage] && file[:name]
      end

      def commit_sha_from_git
        @payload[:git] && @payload[:git][:head]
      end

      def commit_sha_from_ci_service
        @payload[:ci_service] && @payload[:ci_service][:commit_sha]
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
codeclimate-test-reporter-1.0.4 lib/code_climate/test_reporter/payload_validator.rb
codeclimate-test-reporter-1.0.3 lib/code_climate/test_reporter/payload_validator.rb
codeclimate-test-reporter-1.0.2 lib/code_climate/test_reporter/payload_validator.rb
codeclimate-test-reporter-1.0.1 lib/code_climate/test_reporter/payload_validator.rb
codeclimate-test-reporter-1.0.0 lib/code_climate/test_reporter/payload_validator.rb
codeclimate-test-reporter-1.0.0.pre.rc2 lib/code_climate/test_reporter/payload_validator.rb
codeclimate-test-reporter-1.0.0.pre.rc1 lib/code_climate/test_reporter/payload_validator.rb