<commit_before>'use strict';

const { eachSeries } = require('async');
const { get, assignWith, cloneDeep, isNil, defaultsDeep } = require('lodash');

module.exports = (callCIDriver, localConfig) => {
  return (branch, jobs, deployConfig,  cb) => {

    const ciJobsConfig = deployConfig.ciJobs;
    const ciServicesConfig = deployConfig.ciServices;

    eachSeries(jobs, (job, nextJob) => {
      // TODO: ensure we check for ci jobs, services and drivers references in the preview step
      const ciJobConfig = get(ciJobsConfig, job.name);
      if (!ciJobConfig) return nextJob();

      const settings = combineSettings(ciJobConfig.ciService, ciServicesConfig, localConfig);
      const params = combineParams(job, branch, ciJobConfig, localConfig);

      callCIDriver(ciJobConfig.ciService, settings, params, nextJob);
      // TODO: return job execution result
    }, cb);
  }

  function combineSettings(ciService, ciServicesConfig, localConfig) {
    const repoSettings = get(ciServicesConfig, `${ciService}.settings`);
    const localSettings = get(localConfig, `deploy.ciServices.${ciService}.settings`);
    const settings = defaultsDeep({}, repoSettings, localSettings);
    return settings;
  }

  function combineParams(job, branch, repoCIJobConfig, localConfig) {
    const localCIJobConfig = get(localConfig, `deploy.ciJobs.['${job.name}']`);
    const finalCIJobConfig = defaultsDeep({}, repoCIJobConfig, localCIJobConfig);
    const params = applyBooleanDefaults(job.params, finalCIJobConfig.defaultParams);

    if (finalCIJobConfig.servicesParam) {
      params[finalCIJobConfig.servicesParam.paramName] = job.deployTo.join(finalCIJobConfig.servicesParam.separator);
    }

    if (finalCIJobConfig.sourceVersionParam) {
      params[finalCIJobConfig.sourceVersionParam.paramName] = branch;
    }

    return params
  }

  function applyBooleanDefaults(originalObj, defaultObj) {
    const finalObj = cloneDeep(defaultObj);
    return assignWith(finalObj, originalObj, (defaultProp, originalProp) => {
      return isNil(originalProp) ? defaultProp : originalProp;
    });
  }
}<commit_msg>Ensure ciServices names can have special characters<commit_after>'use strict';

const { eachSeries } = require('async');
const { get, assignWith, cloneDeep, isNil, defaultsDeep } = require('lodash');

module.exports = (callCIDriver, localConfig) => {
  return (branch, jobs, deployConfig,  cb) => {

    const ciJobsConfig = deployConfig.ciJobs;
    const ciServicesConfig = deployConfig.ciServices;

    eachSeries(jobs, (job, nextJob) => {
      // TODO: ensure we check for ci jobs, services and drivers references in the preview step
      const ciJobConfig = get(ciJobsConfig, job.name);
      if (!ciJobConfig) return nextJob();

      const settings = combineSettings(ciJobConfig.ciService, ciServicesConfig, localConfig);
      const params = combineParams(job, branch, ciJobConfig, localConfig);

      callCIDriver(ciJobConfig.ciService, settings, params, nextJob);
      // TODO: return job execution result
    }, cb);
  }

  function combineSettings(ciService, ciServicesConfig, localConfig) {
    const repoSettings = get(ciServicesConfig, `${ciService}.settings`);
    const localSettings = get(localConfig, `deploy.ciServices.['${ciService}'].settings`);
    const settings = defaultsDeep({}, repoSettings, localSettings);
    return settings;
  }

  function combineParams(job, branch, repoCIJobConfig, localConfig) {
    const localCIJobConfig = get(localConfig, `deploy.ciJobs.['${job.name}']`);
    const finalCIJobConfig = defaultsDeep({}, repoCIJobConfig, localCIJobConfig);
    const params = applyBooleanDefaults(job.params, finalCIJobConfig.defaultParams);

    if (finalCIJobConfig.servicesParam) {
      params[finalCIJobConfig.servicesParam.paramName] = job.deployTo.join(finalCIJobConfig.servicesParam.separator);
    }

    if (finalCIJobConfig.sourceVersionParam) {
      params[finalCIJobConfig.sourceVersionParam.paramName] = branch;
    }

    return params
  }

  function applyBooleanDefaults(originalObj, defaultObj) {
    const finalObj = cloneDeep(defaultObj);
    return assignWith(finalObj, originalObj, (defaultProp, originalProp) => {
      return isNil(originalProp) ? defaultProp : originalProp;
    });
  }
}
{'fraction_non_alphanumeric': 0.11064228785747773, 'fraction_numerical': 0.0, 'mean_word_length': 4.636723910171731, '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': '2188251', 'n_tokens_mistral': 1217, 'n_tokens_neox': 1188, 'n_words': 276}