<commit_before>module Travis::API::V3
  class Services::Requests::Create < Service
    TIME_FRAME = 1.hour
    LIMIT      = 10
    private_constant :TIME_FRAME, :LIMIT

    result_type :request
    params "request", "user", :config, :message, :branch, :token

    def run
      raise LoginRequired unless access_control.logged_in? or access_control.full_access?
      raise NotFound      unless repository = find(:repository)
      access_control.permissions(repository).create_request!

      user      = find(:user) if access_control.full_access? and params_for? 'user'.freeze
      user    ||= access_control.user
      remaining = remaining_requests(repository)

      raise RequestLimitReached, repository: repository if remaining == 0

      payload = query.schedule(repository, user)
      accepted(remaining_requests: remaining, repository: repository, request: payload)
    end

    def limit(repository)
      if repository.settings.nil?
        LIMIT
      else
        repository.settings["api_builds_rate_limit"] || Travis.config.requests_create_api_limit || LIMIT
      end
    end

    def remaining_requests(repository)
      api_builds_rate_limit = limit(repository)
      return api_builds_rate_limit if access_control.full_access?
      count = query(:requests).count(repository, TIME_FRAME)
      count > api_builds_rate_limit ? 0 : api_builds_rate_limit - count
    end
  end
end
<commit_msg>Use Travis.config to retrieve default values for api_builds_rate_limit<commit_after>module Travis::API::V3
  class Services::Requests::Create < Service
    TIME_FRAME = 1.hour
    private_constant :TIME_FRAME

    result_type :request
    params "request", "user", :config, :message, :branch, :token

    def run
      raise LoginRequired unless access_control.logged_in? or access_control.full_access?
      raise NotFound      unless repository = find(:repository)
      access_control.permissions(repository).create_request!

      user      = find(:user) if access_control.full_access? and params_for? 'user'.freeze
      user    ||= access_control.user
      remaining = remaining_requests(repository)

      raise RequestLimitReached, repository: repository if remaining == 0

      payload = query.schedule(repository, user)
      accepted(remaining_requests: remaining, repository: repository, request: payload)
    end

    def limit(repository)
      if repository.settings.nil?
        Travis.config.requests_create_api_limit
      else
        repository.settings["api_builds_rate_limit"] || Travis.config.requests_create_api_limit
      end
    end

    def remaining_requests(repository)
      api_builds_rate_limit = limit(repository)
      return api_builds_rate_limit if access_control.full_access?
      count = query(:requests).count(repository, TIME_FRAME)
      count > api_builds_rate_limit ? 0 : api_builds_rate_limit - count
    end
  end
end

{'fraction_non_alphanumeric': 0.07777777777777778, 'fraction_numerical': 0.003472222222222222, 'mean_word_length': 3.6467741935483873, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2757689', 'n_tokens_mistral': 812, 'n_tokens_neox': 795, 'n_words': 189}