lib/middleware.js
function projectPlugin(req, res, next) {
  var pluginid;
  // if only 3 args, then get pluginid from params ":plugin"
  if (arguments.length === 4) {
    pluginid = req;
    req = res;
    res = next;
    next = arguments[3];
  } else {
    pluginid = req.params.plugin;
  }

  var branch = req.project.branch(req.query.branch);
  var plugin = null;

  if (!branch) {
    return res.status(404).send('Specified branch not found for the project');
  }
  // if it's just mirroring master
  if (branch.mirror_master) {
    return res.status(400).send('Branch not individually configurable');
  }

  for (var i = 0; i < branch.plugins.length; i++) {
    if (branch.plugins[i].id === pluginid) {
      plugin = branch.plugins[i];
      break;
    }
  }

  if (plugin === null) {
    return res.status(404).send('Plugin not enabled for the specified project');
  }

  req.pluginConfig = function (config, next) {
    if (arguments.length === 0) {
      return plugin.config;
    }

    plugin.config = config;
    req.project.markModified('branches');
    req.project.save(function (err) {
      next(err, config);
    });
  };

  req.userConfig = function (config, next) {
    if (!req.user.isProjectCreator) {
      if (arguments.length === 0) {
        return false;
      }

      return next(new Error('Current user is not the creator - cannot set the creator config'));
    }

    if (arguments.length === 0) {
      return req.project.creator.jobplugins[pluginid];
    }

    var schema = common.userConfigs.job && common.userConfigs.job[pluginid];

    if (!schema) {
      return next(new Error(`Plugin ${pluginid} doesn't define any user config`));
    }

    config = utils.validateAgainstSchema(config, schema);
    // TODO: validation
    req.project.creator.jobplugins[pluginid] = config;
    req.project.creator.markModified('jobplugins');

    req.project.creator.save(function (err) {
      next(err, config);
    });
  };

  next();
}
function projectPlugin(req, res, next) {
  var pluginid;
  // if only 3 args, then get pluginid from params ":plugin"
  if (arguments.length === 4) {
    pluginid = req;
    req = res;
    res = next;
    next = arguments[3];
  } else {
    pluginid = req.params.plugin;
  }

  var branch = req.project.branch(req.query.branch);
  var plugin = null;

  if (!branch) {
    return res.status(404).send('Specified branch not found for the project');
  }
  // if it's just mirroring master
  if (branch.mirror_master) {
    return res.status(400).send('Branch not individually configurable');
  }

  for (var i = 0; i < branch.plugins.length; i++) {
    if (branch.plugins[i].id === pluginid) {
      plugin = branch.plugins[i];
      break;
    }
  }

  if (plugin === null) {
    return res.status(404).send('Plugin not enabled for the specified project');
  }

  req.pluginConfig = function (config, next) {
    if (arguments.length === 0) {
      return plugin.config;
    }

    plugin.config = config;
    req.project.markModified('branches');
    req.project.save(function (err) {
      next(err, config);
    });
  };

  req.userConfig = function (config, next) {
    if (!req.user.isProjectCreator) {
      if (arguments.length === 0) {
        return false;
      }

      return next(new Error('Current user is not the creator - cannot set the creator config'));
    }

    if (arguments.length === 0) {
      return req.project.creator.jobplugins[pluginid];
    }

    var schema = common.userConfigs.job && common.userConfigs.job[pluginid];

    if (!schema) {
      return next(new Error(`Plugin ${pluginid} doesn't define any user config`));
    }

    config = utils.validateAgainstSchema(config, schema);
    // TODO: validation
    req.project.creator.jobplugins[pluginid] = config;
    req.project.creator.markModified('jobplugins');

    req.project.creator.save(function (err) {
      next(err, config);
    });
  };

  next();
}
function projectPlugin ( req , res , next ) { var pluginid ; // if only 3 args, then get pluginid from params ":plugin" if ( arguments . length === 4 ) { pluginid = req ; req = res ; res = next ; next = arguments [ 3 ] ; } else { pluginid = req . params . plugin ; } var branch = req . project . branch ( req . query . branch ) ; var plugin = null ; if ( ! branch ) { return res . status ( 404 ) . send ( 'Specified branch not found for the project' ) ; } // if it's just mirroring master if ( branch . mirror_master ) { return res . status ( 400 ) . send ( 'Branch not individually configurable' ) ; } for ( var i = 0 ; i < branch . plugins . length ; i ++ ) { if ( branch . plugins [ i ] . id === pluginid ) { plugin = branch . plugins [ i ] ; break ; } } if ( plugin === null ) { return res . status ( 404 ) . send ( 'Plugin not enabled for the specified project' ) ; } req . pluginConfig = function ( config , next ) { if ( arguments . length === 0 ) { return plugin . config ; } plugin . config = config ; req . project . markModified ( 'branches' ) ; req . project . save ( function ( err ) { next ( err , config ) ; } ) ; } ; req . userConfig = function ( config , next ) { if ( ! req . user . isProjectCreator ) { if ( arguments . length === 0 ) { return false ; } return next ( new Error ( 'Current user is not the creator - cannot set the creator config' ) ) ; } if ( arguments . length === 0 ) { return req . project . creator . jobplugins [ pluginid ] ; } var schema = common . userConfigs . job && common . userConfigs . job [ pluginid ] ; if ( ! schema ) { return next ( new Error ( ` ${ pluginid } ` ) ) ; } config = utils . validateAgainstSchema ( config , schema ) ; // TODO: validation req . project . creator . jobplugins [ pluginid ] = config ; req . project . creator . markModified ( 'jobplugins' ) ; req . project . creator . save ( function ( err ) { next ( err , config ) ; } ) ; } ; next ( ) ; }
Get plugin config. Expects req.project Sets `req.pluginConfig` function pluginConfig() -> return the config pluginConfig(config, next(err)). save the config
Get plugin config . Expects req . project Sets req . pluginConfig function pluginConfig () - > return the config pluginConfig ( config next ( err )) . save the config