<commit_before>module Travis
  module Addons
    module Webhook
      class WebhookError < StandardError; end

      # Sends build notifications to webhooks as defined in the configuration
      # (`.travis.yml`).
      class Task < Travis::Task
        def targets
          params[:targets]
        end

        private

          def process
            errors = {}

            Array(targets).each do |target|
              begin
                send_webhook(target)
              rescue => e
                error "task=webhook status=failed url=#{target}"
                errors[target] = e.message
              end
            end

            if errors.any?
              raise "task=webhook failures=#{errors.size} build=#{payload[:id]}"
            end
          end

          def send_webhook(target)
            response = http.post(target) do |req|
              req.body = { payload: payload.except(:params).to_json }
              uri = URI(target)
              if uri.user && uri.password
                req.headers['Authorization'] =
                  Faraday::Request::BasicAuthentication.header(
                    URI.unescape(uri.user), URI.unescape(uri.password)
                  )
              else
                req.headers['Authorization'] = authorization
              end
              req.headers['Travis-Repo-Slug'] = repo_slug
            end
            response.success? ? log_success(response) : log_error(response)
          rescue URI::InvalidURIError => e
            error "Invalid webhook URI for build #{repo_slug}##{payload[:id]}: #{target}"
          end

          def authorization
            Digest::SHA2.hexdigest(repo_slug + params[:token].to_s)
          end

          def log_success(response)
            info "Successfully notified #{response.env[:url].to_s}."
          end

          def log_error(response)
            error "Could not notify #{response.env[:url].to_s}. Status: #{response.status} (#{response.body.inspect})"
          end

          def repo_slug
            repository.values_at(:owner_name, :name).join('/')
          end
      end
    end<commit_msg>update webhook logging<commit_after>module Travis
  module Addons
    module Webhook
      class WebhookError < StandardError; end

      # Sends build notifications to webhooks as defined in the configuration
      # (`.travis.yml`).
      class Task < Travis::Task
        def targets
          params[:targets]
        end

        private

          def process
            errors = {}

            Array(targets).each do |target|
              begin
                send_webhook(target)
              rescue => e
                error "task=webhook status=failed url=#{target}"
                errors[target] = e.message
              end
            end

            if errors.any?
              raise "task=webhook failures=#{errors.size} build=#{payload[:id]}"
            end
          end

          def send_webhook(target)
            response = http.post(target) do |req|
              req.body = { payload: payload.except(:params).to_json }
              uri = URI(target)
              if uri.user && uri.password
                req.headers['Authorization'] =
                  Faraday::Request::BasicAuthentication.header(
                    URI.unescape(uri.user), URI.unescape(uri.password)
                  )
              else
                req.headers['Authorization'] = authorization
              end
              req.headers['Travis-Repo-Slug'] = repo_slug
            end
            response.success? ? log_success(response) : log_error(response)
          rescue URI::InvalidURIError => e
            error "task=webhook status=invalid_uri build=#{payload[:id]} slug=#{repo_slug} url=#{target}"
          end

          def authorization
            Digest::SHA2.hexdigest(repo_slug + params[:token].to_s)
          end

          def log_success(response)
            info "task=webhook status=successful url=#{response.env[:url].to_s}"
          end

          def log_error(response)
            error "task=webhook status=error url=#{response.env[:url].to_s} error_code=#{response.status} message=#{response.body.inspec}t"
          end

          def repo_slug
            repository.values_at(:owner_name, :name).join('/')
          end
      end
    end
{'fraction_non_alphanumeric': 0.09842883548983364, 'fraction_numerical': 0.0004621072088724584, 'mean_word_length': 1.6125528062764032, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2626902', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1108, 'n_words': 247}