Skip to content

Commit

Permalink
[JENKINS-42262] Adapts the code to use the new APIs so that config-fi…
Browse files Browse the repository at this point in the history
…le-provider could provide the same content file that this plugin produces.
  • Loading branch information
nfalco79 committed May 22, 2017
1 parent 821832d commit 5b7754f
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 99 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -49,7 +49,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>config-file-provider</artifactId>
<version>2.15.5</version>
<version>2.16.0</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/jenkins/plugins/nodejs/NodeJSBuildWrapper.java
@@ -1,14 +1,18 @@
package jenkins.plugins.nodejs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.jenkinsci.Symbol;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ConfigFile;
import org.jenkinsci.lib.configprovider.model.ConfigFileManager;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

Expand Down Expand Up @@ -114,9 +118,11 @@ public void setUp(final Context context, Run<?, ?> build, FilePath workspace, La
EnvVars env = initialEnvironment.overrideAll(context.getEnv());

// add npmrc config
FilePath configFile = NodeJSUtils.supplyConfig(configId, build, workspace, listener, env);
if (configFile != null) {
if (configId != null) {
ConfigFile cf = new ConfigFile(configId, null, true);
FilePath configFile = ConfigFileManager.provisionConfigFile(cf, env, build, workspace, listener, new ArrayList<String>());
context.env(NodeJSConstants.NPM_USERCONFIG, configFile.getRemote());
build.addAction(new CleanTempFilesAction(configFile.getRemote()));
}
}

Expand Down
14 changes: 10 additions & 4 deletions src/main/java/jenkins/plugins/nodejs/NodeJSCommandInterpreter.java
@@ -1,12 +1,16 @@
package jenkins.plugins.nodejs;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import javax.annotation.CheckForNull;

import org.jenkinsci.Symbol;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ConfigFile;
import org.jenkinsci.lib.configprovider.model.ConfigFileManager;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

Expand Down Expand Up @@ -108,10 +112,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, TaskListene
}
}

// add npmrc config
FilePath configFile = NodeJSUtils.supplyConfig(configId, build, build.getWorkspace(), listener, env);
if (configFile != null) {
newEnv.put(NodeJSConstants.NPM_USERCONFIG, configFile.getRemote());
if (configId != null) {
// add npmrc config
ConfigFile cf = new ConfigFile(configId, null, true);
FilePath configFile = ConfigFileManager.provisionConfigFile(cf, env, build, build.getWorkspace(), listener, new ArrayList<String>());
newEnv.put(NodeJSConstants.NPM_USERCONFIG, configFile.getRemote());
build.addAction(new CleanTempFilesAction(configFile.getRemote()));
}

// add an Environment so when the in super class is called build.getEnviroment() gets the enhanced env
Expand Down
85 changes: 0 additions & 85 deletions src/main/java/jenkins/plugins/nodejs/NodeJSUtils.java
@@ -1,34 +1,12 @@
package jenkins.plugins.nodejs;

import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.util.List;
import java.util.Map;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import jenkins.model.Jenkins;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.NPMRegistry;
import jenkins.plugins.nodejs.configfiles.RegistryHelper;
import jenkins.plugins.nodejs.configfiles.VerifyConfigProviderException;
import jenkins.plugins.nodejs.tools.NodeJSInstallation;
import jenkins.plugins.nodejs.tools.NodeJSInstallation.DescriptorImpl;

import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.ConfigFiles;
import org.jenkinsci.plugins.configfiles.buildwrapper.ManagedFileUtil;
import org.jenkinsci.plugins.configfiles.common.CleanTempFilesAction;

import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;

/*package*/final class NodeJSUtils {

private NodeJSUtils() {
Expand Down Expand Up @@ -68,67 +46,4 @@ public static NodeJSInstallation[] getInstallations() {
return descriptor.getInstallations();
}

/**
* Create a copy of the given configuration in a no accessible folder for
* the user.
* <p>
* This file will be deleted at the end of job also in case of user
* interruption.
* </p>
*
* @param configId the configuration identification
* @param build a build being run
* @param workspace a workspace of the build
* @param listener a way to report progress
* @param env the environment variables set at the outset
* @throws AbortException in case the provided configId is not valid
*/
@CheckForNull
public static FilePath supplyConfig(String configId, Run<?, ?> build, FilePath workspace, TaskListener listener, EnvVars env) throws AbortException {
if (StringUtils.isNotBlank(configId)) {
Config c = ConfigFiles.getByIdOrNull(build, configId);

if (c == null) {
throw new AbortException("this NodeJS build is setup to use a config with id " + configId + " but can not be find");
} else {
NPMConfig config;
if (c instanceof NPMConfig) {
config = (NPMConfig) c;
} else {
config = new NPMConfig(c.id, c.name, c.comment, c.content, c.getProviderId(), null);
}

listener.getLogger().println("using user config with name " + config.name);
String fileContent = config.content;

List<NPMRegistry> registries = config.getRegistries();
RegistryHelper helper = new RegistryHelper(registries);
if (!registries.isEmpty()) {
listener.getLogger().println("Adding all registry entries");
Map<String, StandardUsernameCredentials> registry2Credentials = helper.resolveCredentials(build);
fileContent = helper.fillRegistry(fileContent, registry2Credentials);
}

try {
if (StringUtils.isNotBlank(fileContent)) { // NOSONAR
config.doVerify();

FilePath workDir = ManagedFileUtil.tempDir(workspace);
final FilePath f = workDir.createTextTempFile(".npmrc", "", Util.replaceMacro(fileContent, env), true);
listener.getLogger().printf("Created %s", f);

build.addAction(new CleanTempFilesAction(f.getRemote()));
return f;
}
} catch (VerifyConfigProviderException e) {
throw new AbortException("Invalid user config: " + e.getMessage());
} catch (Exception e) {
throw new IllegalStateException("the npmrc user config could not be supplied for the current build", e);
}
}
}

return null;
}

}
43 changes: 38 additions & 5 deletions src/main/java/jenkins/plugins/nodejs/configfiles/NPMConfig.java
@@ -1,24 +1,32 @@
package jenkins.plugins.nodejs.configfiles;

import hudson.Extension;
import hudson.Util;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Nonnull;

import jenkins.model.Jenkins;
import jenkins.plugins.nodejs.Messages;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.AbstractConfigProviderImpl;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ContentType;
import org.kohsuke.stapler.DataBoundConstructor;

import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;

import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import jenkins.plugins.nodejs.Messages;

/**
* A config/provider to handle the special case of a npmrc config file
*
Expand Down Expand Up @@ -105,5 +113,30 @@ protected String loadTemplateContent() {
}
}

@Override
public String supplyContent(Config configFile, Run<?, ?> build, FilePath workDir, TaskListener listener, List<String> tempFiles) throws IOException {
String fileContent = configFile.content;
if (configFile instanceof NPMConfig) {
NPMConfig config = (NPMConfig) configFile;

List<NPMRegistry> registries = config.getRegistries();
RegistryHelper helper = new RegistryHelper(registries);
if (!registries.isEmpty()) {
listener.getLogger().println("Adding all registry entries");
Map<String, StandardUsernameCredentials> registry2Credentials = helper.resolveCredentials(build);
fileContent = helper.fillRegistry(fileContent, registry2Credentials);
}

try {
if (StringUtils.isNotBlank(fileContent)) { // NOSONAR
config.doVerify();
}
} catch (VerifyConfigProviderException e) {
throw new AbortException("Invalid user config: " + e.getMessage());
}
}
return fileContent;
}

}
}
Expand Up @@ -68,7 +68,7 @@ public void test_creation_of_config() throws Exception {
public void test_inject_path_variable() throws Exception {
FreeStyleProject job = j.createFreeStyleProject("free");

final Config config = createSetting("my-config-id", null, null);
final Config config = createSetting("my-config-id", "", null);

NodeJSInstallation installation = new NodeJSInstallation("test", getTestHome(), null);
NodeJSBuildWrapper spy = mockWrapper(installation, config);
Expand Down
Expand Up @@ -10,6 +10,8 @@
import java.util.List;

import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.lib.configprovider.model.ConfigFile;
import org.jenkinsci.lib.configprovider.model.ConfigFileManager;
import org.jenkinsci.plugins.configfiles.GlobalConfigFiles;
import org.junit.Rule;
import org.junit.Test;
Expand Down Expand Up @@ -70,7 +72,7 @@ public void test_supply_npmrc_with_registry() throws Exception {

FreeStyleBuild build = new MockBuild(j.createFreeStyleProject(), folder.newFolder());

FilePath npmrcFile = NodeJSUtils.supplyConfig(config.id, build, build.getWorkspace(), j.createTaskListener(), new EnvVars());
FilePath npmrcFile = ConfigFileManager.provisionConfigFile(new ConfigFile(config.id, null, true), null, build, build.getWorkspace(), j.createTaskListener(), new ArrayList<String>(1));
assertTrue(npmrcFile.exists());
assertTrue(npmrcFile.length() > 0);

Expand Down

0 comments on commit 5b7754f

Please sign in to comment.