<commit_before>const $RefParser = require('json-schema-ref-parser');
const SwaggerParser = require('swagger-parser');
const maybe = require('call-me-maybe');
const traverse = require('traverse');
const _ = require('lodash');

function swaggerCombine(config = 'config/swagger.json', cb) {
  let combinedSchema;
  let apis;

  function filterPaths(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].paths) {
        if (apis[idx].paths.exclude && apis[idx].paths.exclude.length > 0) {
          schema.paths = _.omit(schema.paths, apis[idx].paths.exclude);
        } else if (apis[idx].paths.include && apis[idx].paths.include.length > 0) {
          schema.paths = _.pick(schema.paths, apis[idx].paths.include);
        }
      }

      return schema;
    });
  }

  function replacePaths(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].paths && apis[idx].paths.replace && Object.keys(apis[idx].paths.replace).length > 0) {
        _.forIn(apis[idx].paths.replace, (replacePath, pathToReplace) => {
          schema.paths = _.mapKeys(schema.paths, (value, curPath) => {
            if (pathToReplace === curPath) {
              return replacePath;
            }

            return curPath;
          });
        });
      }

      return schema;
    })
  }

  function addSecurityToPaths(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].paths && apis[idx].paths.security && Object.keys(apis[idx].paths.security).length > 0) {
        _.forIn(apis[idx].paths.security, (securityDefinitions, pathForSecurity) => {
          const pathComponents = pathForSecurity.split('.');
          const pathInSchema = _.get(schema.paths, pathForSecurity);

          if (pathInSchema) {
            if (pathComponents.length === 1) {
              _.forIn(pathInSchema, (properties, method) => {
                _.forIn(securityDefinitions, (scope, type) => {
                  pathInSchema[method].security = pathInSchema[method].security || [];
                  pathInSchema[method].security.push({ [type]: scope });
                });
              });
            } else if (pathComponents.length === 2) {
              _.forIn(securityDefinitions, (scope, type) => {
                pathInSchema.security = pathInSchema.security || [];
                pathInSchema.security.push({ [type]: scope });
              });
            }
          }
        });
      }

      return schema;
    });
  }

  function replaceTags(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].tags && apis[idx].tags.replace && Object.keys(apis[idx].tags.replace).length > 0) {
        _.forIn(apis[idx].tags.replace, (replaceTag, tagToReplace) => {
          traverse(schema).forEach((function traverseSchema() {
            if (this.key === 'tags' && Array.isArray(this.node) && this.node.includes(tagToReplace)) {
              this.update(this.node.map(tag => tag === tagToReplace ? replaceTag : tag)); // eslint-disable-line
            }
          }));
        });
      }

      return schema;
    });
  }<commit_msg>Refactor path security addition<commit_after>const $RefParser = require('json-schema-ref-parser');
const SwaggerParser = require('swagger-parser');
const maybe = require('call-me-maybe');
const traverse = require('traverse');
const _ = require('lodash');

function swaggerCombine(config = 'config/swagger.json', cb) {
  let combinedSchema;
  let apis;

  function filterPaths(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].paths) {
        if (apis[idx].paths.include && apis[idx].paths.include.length > 0) {
          schema.paths = _.pick(schema.paths, apis[idx].paths.include);
        } else if (apis[idx].paths.exclude && apis[idx].paths.exclude.length > 0) {
          schema.paths = _.omit(schema.paths, apis[idx].paths.exclude);
        }
      }

      return schema;
    });
  }

  function replacePaths(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].paths && apis[idx].paths.replace && Object.keys(apis[idx].paths.replace).length > 0) {
        _.forIn(apis[idx].paths.replace, (replacePath, pathToReplace) => {
          schema.paths = _.mapKeys(schema.paths, (value, curPath) => {
            if (pathToReplace === curPath) {
              return replacePath;
            }

            return curPath;
          });
        });
      }

      return schema;
    })
  }

  function addSecurityToPaths(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].paths && apis[idx].paths.security && Object.keys(apis[idx].paths.security).length > 0) {
        _.forIn(apis[idx].paths.security, (securityDefinitions, pathForSecurity) => {
          const hasHttpMethod = /\.(get|put|post|delete|options|head|patch)$/i.test(pathForSecurity);
          const pathInSchema = _.get(schema.paths, pathForSecurity);

          if (pathInSchema) {
            if (hasHttpMethod) {
              _.forIn(securityDefinitions, (scope, type) => {
                pathInSchema.security = pathInSchema.security || [];
                pathInSchema.security.push({ [type]: scope });
              });
            } else {
              _.forIn(pathInSchema, (properties, method) => {
                _.forIn(securityDefinitions, (scope, type) => {
                  pathInSchema[method].security = pathInSchema[method].security || [];
                  pathInSchema[method].security.push({ [type]: scope });
                });
              });
            }
          }
        });
      }

      return schema;
    });
  }

  function replaceTags(schemas) {
    return schemas.map((schema, idx) => {
      if (apis[idx].tags && apis[idx].tags.replace && Object.keys(apis[idx].tags.replace).length > 0) {
        _.forIn(apis[idx].tags.replace, (replaceTag, tagToReplace) => {
          traverse(schema).forEach((function traverseSchema() {
            if (this.key === 'tags' && Array.isArray(this.node) && this.node.includes(tagToReplace)) {
              this.update(this.node.map(tag => tag === tagToReplace ? replaceTag : tag)); // eslint-disable-line
            }
          }));
        });
      }

      return schema;
    });
  }
{'fraction_non_alphanumeric': 0.15735056542810985, 'fraction_numerical': 0.0019386106623586429, 'mean_word_length': 2.287838555496548, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 37, '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': '318673', 'n_tokens_mistral': 1849, 'n_tokens_neox': 1779, 'n_words': 299}