<commit_before>"use strict";

var _ = require("lodash");

var api = require("../api");
var logger = require("../logger");
var utils = require("../utils");

var API_VERSION = "v1";

function _handleErrorResponse(response) {
  if (response.body && response.body.error) {
    return utils.reject(response.body.error, { code: 2 });
  }

  logger.debug("[rules] error:", response.status, response.body);
  return utils.reject("Unexpected error encountered with rules.", {
    code: 2,
  });
}

/**
 * Gets the latest ruleset name on the project.
 * @param {String} projectId Project from which you want to get the ruleset.
 * @param {String} service Service for the ruleset (ex: cloud.firestore or firebase.storage).
 * @returns {String} Name of the latest ruleset.
 */
function getLatestRulesetName(projectId, service) {
  return api
    .request("GET", "/" + API_VERSION + "/projects/" + projectId + "/releases", {
      auth: true,
      origin: api.rulesOrigin,
    })
    .then(function(response) {
      if (response.status == 200) {
        if (response.body.releases && response.body.releases.length > 0) {
          var releases = _.orderBy(response.body.releases, ["updateTime"], ["desc"]);

          var prefix = "projects/" + projectId + "/releases/" + service;
          var release = _.find(releases, function(r) {
            return r.name.indexOf(prefix) == 0;
          });

          if (!release) {
            return null;
          }
          return release.rulesetName;
        }
      }

      return _handleErrorResponse(response);
    });
}

/**
 * Gets the full contents of a ruleset.
 * @param {String} name Name of the ruleset.
 * @return {Array<Object>} Array of files in the ruleset. Each entry has form { content, name }.
 */
function getRulesetContent(name) {
  return api
    .request("GET", "/" + API_VERSION + "/" + name, {
      auth: true,<commit_msg>Fix firebase init when Rules never used on a project (#850)<commit_after>"use strict";

var _ = require("lodash");

var api = require("../api");
var logger = require("../logger");
var utils = require("../utils");

var API_VERSION = "v1";

function _handleErrorResponse(response) {
  if (response.body && response.body.error) {
    return utils.reject(response.body.error, { code: 2 });
  }

  logger.debug("[rules] error:", response.status, response.body);
  return utils.reject("Unexpected error encountered with rules.", {
    code: 2,
  });
}

/**
 * Gets the latest ruleset name on the project.
 * @param {String} projectId Project from which you want to get the ruleset.
 * @param {String} service Service for the ruleset (ex: cloud.firestore or firebase.storage).
 * @returns {String} Name of the latest ruleset.
 */
function getLatestRulesetName(projectId, service) {
  return api
    .request("GET", "/" + API_VERSION + "/projects/" + projectId + "/releases", {
      auth: true,
      origin: api.rulesOrigin,
    })
    .then(function(response) {
      if (response.status == 200) {
        if (response.body.releases && response.body.releases.length > 0) {
          var releases = _.orderBy(response.body.releases, ["updateTime"], ["desc"]);

          var prefix = "projects/" + projectId + "/releases/" + service;
          var release = _.find(releases, function(r) {
            return r.name.indexOf(prefix) == 0;
          });

          if (!release) {
            return null;
          }
          return release.rulesetName;
        }

        // In this case it's likely that Firestore has not been used on this project before.
        return null;
      }

      return _handleErrorResponse(response);
    });
}

/**
 * Gets the full contents of a ruleset.
 * @param {String} name Name of the ruleset.
 * @return {Array<Object>} Array of files in the ruleset. Each entry has form { content, name }.
 */
function getRulesetContent(name) {
  return api
    .request("GET", "/" + API_VERSION + "/" + name, {
      auth: true,
{'fraction_non_alphanumeric': 0.14398371915543118, 'fraction_numerical': 0.004833375731366065, 'mean_word_length': 2.8211856171039846, 'pattern_counts': {'":': 0, '<': 5, '<?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': '181794', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1140, 'n_words': 346}