Skip to content

Commit

Permalink
[JENKINS-49469] Add support for managed file configured in a folder.
Browse files Browse the repository at this point in the history
  • Loading branch information
nfalco79 committed Mar 10, 2018
1 parent 0986335 commit 39b10a4
Show file tree
Hide file tree
Showing 16 changed files with 122 additions and 83 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Expand Up @@ -3,7 +3,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>2.32</version>
<version>2.37</version>
</parent>

<artifactId>nodejs</artifactId>
Expand Down Expand Up @@ -50,7 +50,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>config-file-provider</artifactId>
<version>2.16.0</version>
<version>2.16.4</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
Expand Down
40 changes: 22 additions & 18 deletions src/main/java/jenkins/plugins/nodejs/NodeJSBuildWrapper.java
Expand Up @@ -2,17 +2,16 @@

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

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

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.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -25,14 +24,15 @@
import hudson.Util;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.model.Node;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import hudson.util.FormValidation;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.NPMConfig.NPMConfigProvider;
import jenkins.plugins.nodejs.configfiles.VerifyConfigProviderException;
import hudson.util.ListBoxModel;
import jenkins.plugins.nodejs.tools.NodeJSInstallation;
import jenkins.tasks.SimpleBuildWrapper;

Expand Down Expand Up @@ -159,20 +159,24 @@ public NodeJSInstallation[] getInstallations() {
return NodeJSUtils.getInstallations();
}

public Collection<Config> getConfigs() {
return GlobalConfigFiles.get().getConfigs(NPMConfigProvider.class);
/**
* Gather all defined npmrc config files.
*
* @return a collection of user npmrc files.
*/
public ListBoxModel doFillConfigIdItems(@AncestorInPath ItemGroup<?> context) {
return NodeJSDescriptorUtils.getConfigs(context);
}

public FormValidation doCheckConfigId(@CheckForNull @QueryParameter final String configId) {
NPMConfig config = (NPMConfig) GlobalConfigFiles.get().getById(configId);
if (config != null) {
try {
config.doVerify();
} catch (VerifyConfigProviderException e) { // NOSONAR I need only message
return FormValidation.error(e.getMessage());
}
}
return FormValidation.ok();
/**
* Verify that the given configId exists in the given context.
*
* @param context where lookup
* @param configId the identifier of an npmrc file
* @return an validation form for the given npmrc file identifier.
*/
public FormValidation doCheckConfigId(@Nullable @AncestorInPath ItemGroup<?> context, @CheckForNull @QueryParameter final String configId) {
return NodeJSDescriptorUtils.checkConfig(context, configId);
}

}
Expand Down
48 changes: 23 additions & 25 deletions src/main/java/jenkins/plugins/nodejs/NodeJSCommandInterpreter.java
Expand Up @@ -2,15 +2,15 @@

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

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

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.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
Expand All @@ -24,16 +24,15 @@
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Environment;
import hudson.model.ItemGroup;
import hudson.model.Node;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.tasks.CommandInterpreter;
import hudson.util.ArgumentListBuilder;
import hudson.util.FormValidation;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.NPMConfig.NPMConfigProvider;
import jenkins.plugins.nodejs.configfiles.VerifyConfigProviderException;
import hudson.util.ListBoxModel;
import jenkins.plugins.nodejs.tools.NodeJSInstallation;
import jenkins.plugins.nodejs.tools.Platform;

Expand Down Expand Up @@ -199,6 +198,12 @@ public void setConfigId(String configId) {
@Symbol("nodejsci")
@Extension
public static final class NodeJsDescriptor extends BuildStepDescriptor<Builder> {

@Override
public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends AbstractProject> jobType) {
return true;
}

/**
* Customise the name of this job step.
*
Expand Down Expand Up @@ -226,28 +231,21 @@ public NodeJSInstallation[] getInstallations() {
/**
* Gather all defined npmrc config files.
*
* @return a collection of user npmrc files or {@code empty} if no one
* defined.
* @return a collection of user npmrc files.
*/
public Collection<Config> getConfigs() {
return GlobalConfigFiles.get().getConfigs(NPMConfigProvider.class);
}

public FormValidation doCheckConfigId(@CheckForNull @QueryParameter final String configId) {
NPMConfig config = (NPMConfig) GlobalConfigFiles.get().getById(configId);
if (config != null) {
try {
config.doVerify();
} catch (VerifyConfigProviderException e) {
return FormValidation.error(e.getMessage());
}
}
return FormValidation.ok();
public ListBoxModel doFillConfigIdItems(@AncestorInPath ItemGroup<?> context) {
return NodeJSDescriptorUtils.getConfigs(context);
}

@Override
public boolean isApplicable(@SuppressWarnings("rawtypes") Class<? extends AbstractProject> jobType) {
return true;
/**
* Verify that the given configId exists in the given context.
*
* @param context where lookup
* @param configId the identifier of an npmrc file
* @return an validation form for the given npmrc file identifier.
*/
public FormValidation doCheckConfigId(@Nullable @AncestorInPath ItemGroup<?> context, @CheckForNull @QueryParameter final String configId) {
return NodeJSDescriptorUtils.checkConfig(context, configId);
}

}
Expand Down
60 changes: 60 additions & 0 deletions src/main/java/jenkins/plugins/nodejs/NodeJSDescriptorUtils.java
@@ -0,0 +1,60 @@
package jenkins.plugins.nodejs;

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

import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.ConfigFiles;

import hudson.model.ItemGroup;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.plugins.nodejs.configfiles.NPMConfig;
import jenkins.plugins.nodejs.configfiles.NPMConfig.NPMConfigProvider;
import jenkins.plugins.nodejs.configfiles.VerifyConfigProviderException;

/*package*/ final class NodeJSDescriptorUtils {

private NodeJSDescriptorUtils() {
}

/**
* Get all NPMConfig defined for the given context.
*
* @param context the context where lookup the config files
* @return a collection of user npmrc files found for the given context
* always including a system default.
*/
@Nonnull
public static ListBoxModel getConfigs(@Nullable ItemGroup<?> context) {
ListBoxModel items = new ListBoxModel();
items.add(Messages.NPMConfig_default(), "");
for (Config config : ConfigFiles.getConfigsInContext(context, NPMConfigProvider.class)) {
items.add(config.name, config.id);
}
return items;
}

/**
* Verify that the given configId exists in the given context.
*
* @param context where lookup
* @param configId the identifier of an npmrc file
* @return an validation form for the given npmrc file identifier, otherwise
* returns {@code ok} if the identifier does not exists for the
* given context.
*/
public static FormValidation checkConfig(@Nullable ItemGroup<?> context, @CheckForNull String configId) {
Config config = ConfigFiles.getByIdOrNull(context, configId);
if (config != null) {
try {
((NPMConfig) config).doVerify();
} catch (VerifyConfigProviderException e) {
return FormValidation.error(e.getMessage());
}
}
return FormValidation.ok();
}

}
16 changes: 0 additions & 16 deletions src/main/java/jenkins/plugins/nodejs/configfiles/NPMConfig.java
Expand Up @@ -24,7 +24,6 @@
import hudson.Util;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.model.Jenkins;
import jenkins.plugins.nodejs.Messages;

/**
Expand Down Expand Up @@ -73,23 +72,8 @@ public void doVerify() throws VerifyConfigProviderException {
}
}

/*
* (non-Javadoc)
* @see org.jenkinsci.lib.configprovider.model.Config#getDescriptor()
*/
@Override
public ConfigProvider getDescriptor() {
// boilerplate template
return (ConfigProvider) Jenkins.getActiveInstance().getDescriptorOrDie(getClass());
}

@Extension
public static class NPMConfigProvider extends AbstractConfigProviderImpl {

public NPMConfigProvider() {
load();
}

@Override
public ContentType getContentType() {
return null;
Expand Down
Expand Up @@ -22,8 +22,6 @@
import com.cloudbees.plugins.credentials.CredentialsMatcher;
import com.cloudbees.plugins.credentials.CredentialsMatchers;
import com.cloudbees.plugins.credentials.CredentialsProvider;
import com.cloudbees.plugins.credentials.common.IdCredentials;
import com.cloudbees.plugins.credentials.common.StandardCredentials;
import com.cloudbees.plugins.credentials.common.StandardListBoxModel;
import com.cloudbees.plugins.credentials.common.StandardUsernameCredentials;
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
Expand All @@ -35,7 +33,6 @@
import hudson.model.AbstractDescribableImpl;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Queue;
import hudson.model.queue.Tasks;
import hudson.security.ACL;
Expand Down
Expand Up @@ -30,6 +30,7 @@ NodeJSBuilders.noInstallationFound=No installation {0} found. Please define one
NodeJSBuilders.nodeOffline=Cannot get installation for node, since it is not online
NPMConfig.displayName=Npm config file
NPMConfig.verifyTooGlobalRegistry=Too many registries configured as global (no scope assigned), at most one is allowed.
NPMConfig.default=-use system default-
NPMRegistry.DescriptorImpl.emptyCredentialsId=Credentials is required
NPMRegistry.DescriptorImpl.invalidCredentialsId=Current credentials does not exists
NPMRegistry.DescriptorImpl.emptyRegistryURL=Registry URL is required
Expand Down
Expand Up @@ -29,4 +29,5 @@ NodeJSCommandInterpreter.noExecutable=Impossible de trouver un ex\uFFFDcutable d
NodeJSCommandInterpreter.noInstallation=Aucune installation {0} trouv\uFFFDe. Veuillez en d\uFFFDfinir un dans le manager Jenkins.
NodeJSCommandInterpreter.nodeOffline=Impossible d''obtenir pour l'installation du noeud, car il est pas en ligne
NPMConfig.displayName=Fichier Npm de configuration
NPMConfig.verifyTooGlobalRegistry=Trop de registres configur\uFFFDs comme globaux (pas de port\uFFFDe attribu\uFFFDe), au maximum un est autoris\uFFFD.
NPMConfig.verifyTooGlobalRegistry=Trop de registres configur\uFFFDs comme globaux (pas de port\uFFFDe attribu\uFFFDe), au maximum un est autoris\uFFFD.
NPMConfig.default=utiliser le syst\u00E8me par d\uFFFDfaut
Expand Up @@ -27,6 +27,7 @@ NodeJSBuildWrapper.displayName=Aggiungi la cartella Node & npm bin/ alla variabi
NodeJSCommandInterpreter.displayName=Esegui script NodeJS
NodeJSCommandInterpreter.noExecutable=Nessun eseguibile trovato per l''installazione NodeJS scelta "{0}"
NodeJSCommandInterpreter.noInstallation=Nessuna installazione trovata in {0}. Per favore definiscine una in Configura Jenkins.
NodeJSCommandInterpreter.nodeOffline=Non posso prendere il node in quanto non � online.
NodeJSCommandInterpreter.nodeOffline=Non posso prendere il node in quanto non \u00E8 online.
NPMConfig.displayName=Npm config file
NPMConfig.verifyTooGlobalRegistry=Ci sono troppi registry npm impostati come globali (cioè senza uno scope assegnato).
NPMConfig.verifyTooGlobalRegistry=Ci sono troppi registry npm impostati come globali (cio\u00E8 senza uno scope assegnato).
NPMConfig.default=- usa il default di sistema -
Expand Up @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:cf="/lib/nodejs">
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<f:entry title="${%nodeJSInstallationName.title}" description="${%nodeJSInstallationName.description}">
<select class="setting-input" name="_.nodeJSInstallationName">
Expand All @@ -33,7 +33,6 @@ THE SOFTWARE.
</f:entry>

<f:entry title="${%configId.title}">
<cf:select field="configId" configs="${descriptor.configs}" allowsEmpty="true" emptyValue="" emptyLabel="${%configId.emptyValue}" />
<f:select field="configId" />
</f:entry>

</j:jelly>
Expand Up @@ -22,5 +22,4 @@

nodeJSInstallationName.title=NodeJS Installation
nodeJSInstallationName.description=Specify needed nodejs installation where npm installed packages will be provided to the PATH
configId.title=npmrc file
configId.emptyValue=- use system default -
configId.title=npmrc file
Expand Up @@ -22,5 +22,5 @@

nodeJSInstallationName.title=Installazione NodeJS
nodeJSInstallationName.description=Selezionando l''installazione NodeJS dove sono presenti i packages npm di cui hai bisogno, questi verranno aggiunti al PATH di sistema
configId.title=npmrc file
configId.emptyValue=- usa il default del sistema -
nodeJSInstallationName.emptyValue=- usa il default di sistema -
configId.title=npmrc file
Expand Up @@ -22,7 +22,7 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:cf="/lib/nodejs">
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry title="${%nodeJSInstallationName.title}" description="${%nodeJSInstallationName.description}">
<select class="setting-input" name="_.nodeJSInstallationName">
<f:option value="">${%nodeJSInstallationName.emptyValue}</f:option>
Expand All @@ -35,9 +35,8 @@ THE SOFTWARE.
<f:entry title="${%command.title}" description="See &lt;a href='${rootURL}/env-vars.html' target=_new>the list of available environment variables&lt;/a> accessible by process.env.ENV_VARIABLE.">
<f:textarea field="command" />
</f:entry>



<f:entry title="${%configId.title}">
<cf:select field="configId" configs="${descriptor.configs}" allowsEmpty="true" emptyValue="" emptyLabel="${%configId.emptyValue}" />
<f:select field="configId" />
</f:entry>
</j:jelly>
Expand Up @@ -24,5 +24,4 @@ nodeJSInstallationName.title=Installazione NodeJS
nodeJSInstallationName.description=Seleziona l''installazione NodeJS dove sono presenti i packages npm di cui hai bisogno per eseguire lo script
nodeJSInstallationName.emptyValue=- use system default -
command.title=Script
configId.title=npmrc file
configId.emptyValue=- use system default -
configId.title=npmrc file

0 comments on commit 39b10a4

Please sign in to comment.