/** * Calculate the result code of the full process based in what went on during the process * @param results he list of results for the disablement of each plugin * @return the status code. 0 if all plugins disabled. {@link #RETURN_CODE_NOT_DISABLED_DEPENDANTS} if some * dependant plugin is not disabled (with strategy NONE), {@link #RETURN_CODE_NO_SUCH_PLUGIN} if some passed * plugin doesn't exist. Whatever happens first. */ private int getResultCode(List<PluginWrapper.PluginDisableResult> results) { int result = 0; for (PluginWrapper.PluginDisableResult oneResult : results) { result = getResultCode(oneResult); if (result != 0) { break; } } return result; }
/** * Calculate the result code of the disablement of one plugin based in what went on during the process of this one * and its dependant plugins. * @param result the result of the disablement of this plugin * @return the status code */ private int getResultCode(PluginWrapper.PluginDisableResult result) { int returnCode = 0; switch (result.getStatus()){ case NOT_DISABLED_DEPENDANTS: returnCode = RETURN_CODE_NOT_DISABLED_DEPENDANTS; break; case NO_SUCH_PLUGIN: returnCode = RETURN_CODE_NO_SUCH_PLUGIN; } if (returnCode == 0) { for (PluginWrapper.PluginDisableResult oneDependantResult : result.getDependantsDisableStatus()) { returnCode = getResultCode(oneDependantResult); if (returnCode != 0) { break; } } } return returnCode; } }
@Override protected int run() throws Exception { Jenkins jenkins = Jenkins.get(); jenkins.checkPermission(Jenkins.ADMINISTER); // get the strategy as an enum PluginWrapper.PluginDisableStrategy strategyToUse; try { strategyToUse = PluginWrapper.PluginDisableStrategy.valueOf(strategy.toUpperCase()); } catch (IllegalArgumentException iae) { throw new IllegalArgumentException(hudson.cli.Messages.DisablePluginCommand_NoSuchStrategy(strategy, String.format("%s, %s, %s", PluginWrapper.PluginDisableStrategy.NONE, PluginWrapper.PluginDisableStrategy.MANDATORY, PluginWrapper.PluginDisableStrategy.ALL))); } // disable... List<PluginWrapper.PluginDisableResult> results = jenkins.pluginManager.disablePlugins(strategyToUse, pluginNames); // print results ... printResults(results); // restart if it was required and it's necessary (at least one plugin was disabled in this execution) restartIfNecessary(results); // return the appropriate error code return getResultCode(results); }