function prepareJob(emitter, job) {
  Project.findOne({ name: job.project }).populate('creator').exec(function (err, project) {
    if (err || !project) return debug('job.prepare - failed to get project', job.project, err);
    // ok so the project is real, we can go ahead and save this job
    var provider = common.extensions.provider[project.provider.id];

    if (!provider) {
      return debug('job.prepare - provider not found for project', job.project, project.provider.id);
    }

    Job.create(job, function (err, mjob) {
      if (err) return debug('job.prepare - failed to save job', job, err);

      var jjob = mjob.toJSON();

      jjob.project = project;
      jjob.providerConfig = project.provider.config;
      jjob.fromStriderJson = true;

      striderJson(provider, project, job.ref, function (err, config) {
        if (err) {
          if (err.status === 403 || err.statusCode === 403) {
            debug('job.prepare - access to strider.json is forbidden, skipping config merge');
            config = {};
            jjob.fromStriderJson = false;
          } else if (err.status === 404 || err.statusCode === 404) {
            debug('job.prepare - strider.json not found, skipping config merge');
            config = {};
            jjob.fromStriderJson = false;
          } else {
            debug('job.prepare - error opening/processing project\'s `strider.json` file: ', err);
            config = {};
            jjob.fromStriderJson = false;
          }
        } else {
          debug('Using configuration from "strider.json".');
        }

        var branch = project.branch(job.ref.branch || 'master');

        if (!branch) {
          return debug('job.prepare - branch not found', job.ref.branch || 'master', project.name);
        }

        branch = branch.mirror_master ? project.branch('master') : branch;
        jjob.providerConfig = _.extend({}, project.provider.config, config.provider || {});
        config.runner = config.runner || branch.runner;

        if (!common.extensions.runner[config.runner.id]) {
          debug(`Error: A job was registered for a runner that doesn't exist, i.e. "${config.runner.id}". This job will never run!`);
        }

        if (config) {
          delete config.provider;
          config = utils.mergeConfigs(branch, config);
        }

        emitter.emit('job.new', jjob, config);

        if (!mjob.runner) mjob.runner = {};

        mjob.runner.id = config.runner.id;
        mjob.save()
          .then(() => debug('job saved'))
          .catch(e => debug(e));
      });
    });
  });
}
function prepareJob(emitter, job) {
  Project.findOne({ name: job.project }).populate('creator').exec(function (err, project) {
    if (err || !project) return debug('job.prepare - failed to get project', job.project, err);
    // ok so the project is real, we can go ahead and save this job
    var provider = common.extensions.provider[project.provider.id];

    if (!provider) {
      return debug('job.prepare - provider not found for project', job.project, project.provider.id);
    }

    Job.create(job, function (err, mjob) {
      if (err) return debug('job.prepare - failed to save job', job, err);

      var jjob = mjob.toJSON();

      jjob.project = project;
      jjob.providerConfig = project.provider.config;
      jjob.fromStriderJson = true;

      striderJson(provider, project, job.ref, function (err, config) {
        if (err) {
          if (err.status === 403 || err.statusCode === 403) {
            debug('job.prepare - access to strider.json is forbidden, skipping config merge');
            config = {};
            jjob.fromStriderJson = false;
          } else if (err.status === 404 || err.statusCode === 404) {
            debug('job.prepare - strider.json not found, skipping config merge');
            config = {};
            jjob.fromStriderJson = false;
          } else {
            debug('job.prepare - error opening/processing project\'s `strider.json` file: ', err);
            config = {};
            jjob.fromStriderJson = false;
          }
        } else {
          debug('Using configuration from "strider.json".');
        }

        var branch = project.branch(job.ref.branch || 'master');

        if (!branch) {
          return debug('job.prepare - branch not found', job.ref.branch || 'master', project.name);
        }

        branch = branch.mirror_master ? project.branch('master') : branch;
        jjob.providerConfig = _.extend({}, project.provider.config, config.provider || {});
        config.runner = config.runner || branch.runner;

        if (!common.extensions.runner[config.runner.id]) {
          debug(`Error: A job was registered for a runner that doesn't exist, i.e. "${config.runner.id}". This job will never run!`);
        }

        if (config) {
          delete config.provider;
          config = utils.mergeConfigs(branch, config);
        }

        emitter.emit('job.new', jjob, config);

        if (!mjob.runner) mjob.runner = {};

        mjob.runner.id = config.runner.id;
        mjob.save()
          .then(() => debug('job saved'))
          .catch(e => debug(e));
      });
    });
  });
}
function prepareJob ( emitter , job ) { Project . findOne ( { name : job . project } ) . populate ( 'creator' ) . exec ( function ( err , project ) { if ( err || ! project ) return debug ( 'job.prepare - failed to get project' , job . project , err ) ; // ok so the project is real, we can go ahead and save this job var provider = common . extensions . provider [ project . provider . id ] ; if ( ! provider ) { return debug ( 'job.prepare - provider not found for project' , job . project , project . provider . id ) ; } Job . create ( job , function ( err , mjob ) { if ( err ) return debug ( 'job.prepare - failed to save job' , job , err ) ; var jjob = mjob . toJSON ( ) ; jjob . project = project ; jjob . providerConfig = project . provider . config ; jjob . fromStriderJson = true ; striderJson ( provider , project , job . ref , function ( err , config ) { if ( err ) { if ( err . status === 403 || err . statusCode === 403 ) { debug ( 'job.prepare - access to strider.json is forbidden, skipping config merge' ) ; config = { } ; jjob . fromStriderJson = false ; } else if ( err . status === 404 || err . statusCode === 404 ) { debug ( 'job.prepare - strider.json not found, skipping config merge' ) ; config = { } ; jjob . fromStriderJson = false ; } else { debug ( 'job.prepare - error opening/processing project\'s `strider.json` file: ' , err ) ; config = { } ; jjob . fromStriderJson = false ; } } else { debug ( 'Using configuration from "strider.json".' ) ; } var branch = project . branch ( job . ref . branch || 'master' ) ; if ( ! branch ) { return debug ( 'job.prepare - branch not found' , job . ref . branch || 'master' , project . name ) ; } branch = branch . mirror_master ? project . branch ( 'master' ) : branch ; jjob . providerConfig = _ . extend ( { } , project . provider . config , config . provider || { } ) ; config . runner = config . runner || branch . runner ; if ( ! common . extensions . runner [ config . runner . id ] ) { debug ( ` ${ config . runner . id } ` ) ; } if ( config ) { delete config . provider ; config = utils . mergeConfigs ( branch , config ) ; } emitter . emit ( 'job.new' , jjob , config ) ; if ( ! mjob . runner ) mjob . runner = { } ; mjob . runner . id = config . runner . id ; mjob . save ( ) . then ( ( ) => debug ( 'job saved' ) ) . catch ( e => debug ( e ) ) ; } ) ; } ) ; } ) ; }
Prepare the job for execution, save to database, and fire off a `job.new` event.

job is expected to be populated with:
- a trigger
- a ref
- the project name
- the user id (if applicable)
- type
- created timestamp

XXX: should this function go in a different file? idk. We'll
definitely move it around when we make strider OO.
Prepare the job for execution save to database and fire off a job . new event .