Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-59148] Add option for getting all of the latest dependencies #71

Merged
merged 2 commits into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ java -jar plugin-management-cli/target/jenkins-plugin-manager-*.jar --war /file/
* `--view-security-warnings`: (optional) Set to true to show if any of the user specified plugins have security warnings
* `--view-all-security-warnings`: (optional) Set to true to show all plugins that have security warnings.
* `--available-updates`: (optional) Set to true to show if any requested plugins have newer versions available. If a Jenkins version-specific update center is available, the latest plugin version will be determined based on that update center's data.
* `--latest`: (optional) Set to true to download the latest version of all dependencies, even if the version(s) of the requested plugin(s) are not the latest.
* `--latest-specified`: (optional) Set to true to download latest dependencies of any plugin that is requested to have the latest version. All other plugin dependency versions are determined by the update center metadata or the plugin MANIFEST.MF.
* `--jenkins-update-center`: (optional) Sets the main update center, which can also be set via the JENKINS_UC environment variable. If a CLI option is entered, it will override what is set in the environment variable. If not set via CLI option or environment variable, will default to https://updates.jenkins.io.
* `--jenkins-experimental-update-center`: (optional) Sets the experimental update center, which can also be set via the JENKINS_UC_EXPERIMENTAL environment variable. If a CLI option is entered, it will override what is set in the environment variable. If not set via CLI option or environment variable, will default to https://updates.jenkins.io/experimental.
* `--jenkins-incrementals-repo-mirror`: (optional) Sets the incrementals repository mirror, which can also be set via the JENKINS_INCREMENTALS_REPO_MIRROR environment variable. If a CLI option is entered, it will override what is set in the environment variable. If not set via CLI option or environment variable, will default to https://repo.jenkins-ci.org/incrementals.
Expand Down Expand Up @@ -91,7 +93,7 @@ java -jar plugin-management-cli/target/jenkins-plugin-manager-*.jar -p "workflow
```

#### Other Information
The plugin manager tries to use update center data to get the latest information about a plugin's dependencies. If this information is unavailable, it will use the dependency information from the downloaded plugin's MANIFEST.MF file.
The plugin manager tries to use update center data to get the latest information about a plugin's dependencies. If this information is unavailable, it will use the dependency information from the downloaded plugin's MANIFEST.MF file. By default, the versions of the plugin dependencies are determined by the update center metadata or the plugin MANIFEST.MF file, but the user can specify other behavior using the `latest` or `latest-specified` options.

For plugins listed in a .txt file, each plugin must be listed on a new line. Comments beginning with `#` will be filtered out.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.jenkins.tools.pluginmanager.config.Config;
import io.jenkins.tools.pluginmanager.config.Settings;
import io.jenkins.tools.pluginmanager.impl.Plugin;
import io.jenkins.tools.pluginmanager.impl.PluginDependencyStrategyException;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
Expand Down Expand Up @@ -89,11 +90,17 @@ class CliOptions {
handler = BooleanOptionHandler.class)
private boolean isNoDownload;

@Option(name = "--latest", usage = "Set true to download latest transitive dependencies of any plugin that is " +
"requested to have the latest version. By default, plugin dependency versions will be determined by the " +
"update center metadata or plugin MANIFEST.MF",
@Option(name = "--latest-specified", usage = "Set to true to download latest transitive dependencies of any " +
"plugin that is requested to have the latest version. By default, plugin dependency versions will be " +
"determined by the update center metadata or plugin MANIFEST.MF",
handler = BooleanOptionHandler.class)
private boolean useLatest;
private boolean useLatestSpecified;

@Option(name = "--latest", usage = "Set to true to download the latest version of all dependencies, even " +
"if the version(s) of the requested plugin(s) are not the latest. By default, plugin dependency versions " +
"will be determined by the update center metadata or plugin MANIFEST.MF",
handler = BooleanOptionHandler.class)
private boolean useLatestAll;

@Option(name = "--help", aliases = {"-h"}, help = true)
private boolean showHelp;
Expand All @@ -117,7 +124,8 @@ Config setup() {
.withShowAvailableUpdates(isShowAvailableUpdates())
.withIsVerbose(isVerbose())
.withDoDownload(!isNoDownload())
.withUseLatest(isUseLatest())
.withUseLatestSpecified(isUseLatestSpecified())
.withUseLatestAll(isUseLatestAll())
.build();
}

Expand Down Expand Up @@ -360,13 +368,30 @@ public boolean isShowHelp() {
}

/**
* Returns boolean corresponding to if user wants dependencies of plugins with latest version specified to also be
* the latest version
* Returns the boolean corresponding to if user wants dependencies of plugins with latest version specified to also
* be the latest version
*
* @return true if user wants transitive dependencies of latest version plugins to also have the latest version
*/
public boolean isUseLatest() {
return useLatest;
public boolean isUseLatestSpecified() {
if (useLatestSpecified && useLatestAll) {
throw new PluginDependencyStrategyException("Only one plugin dependency version strategy can be selected " +
"at a time");
}
return useLatestSpecified;
}

/**
* Returns the boolean corresponding to if the user wants all dependencies to be the latest version, even the
* dependencies of a plugin that had a requested version that was not the latest
* @return true if the user wants all transitive dependencies to be the latest version
*/
public boolean isUseLatestAll() {
if (useLatestSpecified && useLatestAll) {
throw new PluginDependencyStrategyException("Only one plugin dependency version strategy can be selected " +
"at a time");
}
return useLatestAll;
}

// visible for testing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.jenkins.tools.pluginmanager.config.Config;
import io.jenkins.tools.pluginmanager.config.Settings;
import io.jenkins.tools.pluginmanager.impl.Plugin;
import io.jenkins.tools.pluginmanager.impl.PluginDependencyStrategyException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -319,18 +320,38 @@ public void downloadTest() throws CmdLineException {
assertEquals(true, cfg.doDownload());
}

@Test
public void useLatestSpecifiedTest() throws CmdLineException {
parser.parseArgument("--latest-specified");
Config cfg = options.setup();
assertEquals(true, cfg.isUseLatestSpecified());
}

@Test
public void useNotLatestSpecifiedTest() throws CmdLineException {
parser.parseArgument();
Config cfg = options.setup();
assertEquals(false, cfg.isUseLatestSpecified());
}

@Test
public void useLatestTest() throws CmdLineException {
parser.parseArgument("--latest");
Config cfg = options.setup();
assertEquals(true, cfg.isUseLatest());
assertEquals(true, cfg.isUseLatestAll());
}

@Test
public void useNotLatestTest() throws CmdLineException {
parser.parseArgument();
Config cfg = options.setup();
assertEquals(false, cfg.isUseLatest());
assertEquals(false, cfg.isUseLatestAll());
}

@Test (expected = PluginDependencyStrategyException.class)
public void useLatestSpecifiedAndLatestAllTest() throws CmdLineException {
parser.parseArgument("--latest", "--latest-specified");
Config cfg = options.setup();
}

@After
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public class Config {
private URL jenkinsUcExperimental;
private URL jenkinsIncrementalsRepoMirror;
private boolean doDownload;
private boolean useLatest;
private boolean useLatestSpecified;
private boolean useLatestAll;

private Config(
File pluginDir,
Expand All @@ -44,7 +45,8 @@ private Config(
URL jenkinsUcExperimental,
URL jenkinsIncrementalsRepoMirror,
boolean doDownload,
boolean useLatest
boolean useLatestSpecified,
boolean useLatestAll
) {
this.pluginDir = pluginDir;
this.showWarnings = showWarnings;
Expand All @@ -58,7 +60,9 @@ private Config(
this.jenkinsUcExperimental = jenkinsUcExperimental;
this.jenkinsIncrementalsRepoMirror = jenkinsIncrementalsRepoMirror;
this.doDownload = doDownload;
this.useLatest = useLatest;
this.useLatestSpecified = useLatestSpecified;
this.useLatestAll = useLatestAll;

}

public File getPluginDir() {
Expand Down Expand Up @@ -109,10 +113,12 @@ public boolean doDownload() {
return doDownload;
}

public boolean isUseLatest() {
return useLatest;
public boolean isUseLatestSpecified() {
return useLatestSpecified;
}

public boolean isUseLatestAll() { return useLatestAll; }

public static Builder builder() {
return new Builder();
}
Expand All @@ -130,7 +136,8 @@ public static class Builder {
private URL jenkinsUcExperimental = Settings.DEFAULT_EXPERIMENTAL_UPDATE_CENTER;
private URL jenkinsIncrementalsRepoMirror = Settings.DEFAULT_INCREMENTALS_REPO_MIRROR;
private boolean doDownload;
private boolean useLatest;
private boolean useLatestSpecified;
private boolean useLatestAll;

private Builder() {
}
Expand Down Expand Up @@ -195,8 +202,13 @@ public Builder withDoDownload(boolean doDownload) {
return this;
}

public Builder withUseLatest(boolean useLatest) {
this.useLatest = useLatest;
public Builder withUseLatestSpecified(boolean useLatestSpecifed) {
this.useLatestSpecified = useLatestSpecifed;
return this;
}

public Builder withUseLatestAll(boolean useLatestAll) {
this.useLatestAll = useLatestAll;
return this;
}

Expand All @@ -214,7 +226,8 @@ public Config build() {
jenkinsUcExperimental,
jenkinsIncrementalsRepoMirror,
doDownload,
useLatest
useLatestSpecified,
useLatestAll
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.jenkins.tools.pluginmanager.impl;

public class PluginDependencyStrategyException extends RuntimeException {

public PluginDependencyStrategyException(String message) {
super(message);
}

public PluginDependencyStrategyException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ public class PluginManager {
private JSONObject pluginInfoJson;
private JSONObject latestPlugins;
private boolean verbose;
private boolean useLatest;
private boolean useLatestSpecified;
private boolean useLatestAll;

public static final String SEPARATOR = File.separator;

Expand All @@ -84,7 +85,8 @@ public PluginManager(Config cfg) {
allPluginsAndDependencies = new HashMap<>();
verbose = cfg.isVerbose();
jenkinsUcLatest = cfg.getJenkinsUc().toString();
useLatest = cfg.isUseLatest();
useLatestSpecified = cfg.isUseLatestSpecified();
useLatestAll = cfg.isUseLatestAll();
}

/**
Expand All @@ -100,6 +102,11 @@ public void start() {
}
}

if (useLatestSpecified && useLatestAll) {
throw new PluginDependencyStrategyException("Only one plugin dependency version strategy can be selected " +
"at a time");
}

jenkinsVersion = getJenkinsVersionFromWar();
checkAndSetLatestUpdateCenter();
getUCJson();
Expand Down Expand Up @@ -589,7 +596,7 @@ public List<Plugin> resolveDependenciesFromManifest(Plugin plugin) {
String pluginName = pluginInfo[0];
String pluginVersion = pluginInfo[1];
Plugin dependentPlugin = new Plugin(pluginName, pluginVersion, null, null);
if (useLatest && plugin.isLatest()) {
if (useLatestSpecified && plugin.isLatest() || useLatestAll) {
VersionNumber latestPluginVersion = getLatestPluginVersion(pluginName);
dependentPlugin.setVersion(latestPluginVersion);
dependentPlugin.setLatest(true);
Expand Down Expand Up @@ -637,7 +644,7 @@ public List<Plugin> resolveDependenciesFromJson(Plugin plugin, JSONObject plugin
String pluginName = dependency.getString("name");
String pluginVersion = dependency.getString("version");
Plugin dependentPlugin = new Plugin(pluginName, pluginVersion, null, null);
if (useLatest && plugin.isLatest()) {
if (useLatestSpecified && plugin.isLatest() || useLatestAll) {
VersionNumber latestPluginVersion = getLatestPluginVersion(pluginName);
dependentPlugin.setVersion(latestPluginVersion);
dependentPlugin.setLatest(true);
Expand Down
Loading