<commit_before>import path from 'path';

const BasePathForRules = "rules";

export default class KeysRepository {
  constructor(gitTransactionManager) {
    this._gitTransactionManager = gitTransactionManager;
  }

  async getAllKeys() {
    return await this._gitTransactionManager.read(async gitRepo => {
      const keyFiles = await gitRepo.listFiles(BasePathForRules);

      return keyFiles.map(getKeyFromJPadPath);
    });
  }

  async getKeyDetails(keyPath, { revision } = {}) {
    return await this._gitTransactionManager.read(async gitRepo => {
      let pathForJPad = getPathForJPad(keyPath);
      let pathForMeta = getPathForMeta(keyPath);

      let jpadSource = await gitRepo.readFile(pathForJPad, { revision });
      jpadSource = getNewJpadFormatSourceIfNeeded(jpadSource);

      let meta = null;
      try {
        meta = JSON.parse(await gitRepo.readFile(pathForMeta, { revision }));
      } catch (exp) {
        console.warn('failed getting meta file', exp.message);
      }

      let revisionHistory = await gitRepo.getFileDetails(pathForJPad, { revision });
      return {
        keyDef: {
          type: path.extname(pathForJPad).substring(1),
          source: jpadSource,
          revisionHistory
        },
        meta,
      }
    });
  }

  async updateKey(keyPath, keyMetaSource, keyRulesSource, author) {
    await this._gitTransactionManager.write(async gitRepo => {
      await gitRepo.updateFile(getPathForMeta(keyPath), keyMetaSource);
      await gitRepo.updateFile(getPathForJPad(keyPath), keyRulesSource);

      await gitRepo.commitAndPush("BackOffice - updating " + keyPath, author);
    });
  }

  async deleteKey(keyPath, author) {
    await this._gitTransactionManager.write(async gitRepo => {
      await gitRepo.deleteFile(getPathForMeta(keyPath));
      await gitRepo.deleteFile(getPathForJPad(keyPath));

      await gitRepo.commitAndPush("BackOffice - deleting " + keyPath, author)
    });
  }
}

function getNewJpadFormatSourceIfNeeded(originalJpadSource) {<commit_msg>always get all revision from latest commit<commit_after>import path from 'path';

const BasePathForRules = "rules";

export default class KeysRepository {
  constructor(gitTransactionManager) {
    this._gitTransactionManager = gitTransactionManager;
  }

  async getAllKeys() {
    return await this._gitTransactionManager.read(async gitRepo => {
      const keyFiles = await gitRepo.listFiles(BasePathForRules);

      return keyFiles.map(getKeyFromJPadPath);
    });
  }

  async getKeyDetails(keyPath, { revision } = {}) {
    return await this._gitTransactionManager.read(async gitRepo => {
      let pathForJPad = getPathForJPad(keyPath);
      let pathForMeta = getPathForMeta(keyPath);

      let jpadSource = await gitRepo.readFile(pathForJPad, { revision });
      jpadSource = getNewJpadFormatSourceIfNeeded(jpadSource);

      let meta = null;
      try {
        meta = JSON.parse(await gitRepo.readFile(pathForMeta, { revision }));
      } catch (exp) {
        console.warn('failed getting meta file', exp.message);
      }

      let revisionHistory = await gitRepo.getFileDetails(pathForJPad);
      return {
        keyDef: {
          type: path.extname(pathForJPad).substring(1),
          source: jpadSource,
          revisionHistory
        },
        meta,
      }
    });
  }

  async updateKey(keyPath, keyMetaSource, keyRulesSource, author) {
    await this._gitTransactionManager.write(async gitRepo => {
      await gitRepo.updateFile(getPathForMeta(keyPath), keyMetaSource);
      await gitRepo.updateFile(getPathForJPad(keyPath), keyRulesSource);

      await gitRepo.commitAndPush("BackOffice - updating " + keyPath, author);
    });
  }

  async deleteKey(keyPath, author) {
    await this._gitTransactionManager.write(async gitRepo => {
      await gitRepo.deleteFile(getPathForMeta(keyPath));
      await gitRepo.deleteFile(getPathForJPad(keyPath));

      await gitRepo.commitAndPush("BackOffice - deleting " + keyPath, author)
    });
  }
}

function getNewJpadFormatSourceIfNeeded(originalJpadSource) {
{'fraction_non_alphanumeric': 0.1015990159901599, 'fraction_numerical': 0.0004920049200492004, 'mean_word_length': 3.517777777777778, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, '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': '6986364', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1219, 'n_words': 225}