Skip to content

Commit

Permalink
[JENKINS-40364] Update to config-file-provider 2.15 version and some …
Browse files Browse the repository at this point in the history
…javadoc.
  • Loading branch information
nfalco79 committed Jan 14, 2017
1 parent 4bb7417 commit d518e3b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 34 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -48,7 +48,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>config-file-provider</artifactId>
<version>2.13</version>
<version>2.15.4</version>
</dependency>
</dependencies>

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/jenkins/plugins/nodejs/NodeJSUtils.java
Expand Up @@ -22,6 +22,7 @@

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;

Expand Down Expand Up @@ -81,7 +82,7 @@ public static NodeJSInstallation[] getInstallations() {
*/
public static FilePath supplyConfig(String configId, AbstractBuild<?, ?> build, TaskListener listener) throws AbortException {
if (StringUtils.isNotBlank(configId)) {
Config c = Config.getByIdOrNull(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");
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/jenkins/plugins/nodejs/configfiles/NPMConfig.java
Expand Up @@ -10,10 +10,12 @@

import javax.annotation.Nonnull;

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

import org.apache.commons.io.IOUtils;
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;
Expand All @@ -39,6 +41,16 @@ public List<NPMRegistry> getRegistries() {
return registries;
}

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

@Extension
public static class NPMConfigProvider extends AbstractConfigProviderImpl {

Expand Down
85 changes: 64 additions & 21 deletions src/main/java/jenkins/plugins/nodejs/configfiles/NPMRegistry.java
@@ -1,16 +1,5 @@
package jenkins.plugins.nodejs.configfiles;

import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.ItemGroup;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.security.ACL;
import hudson.security.AccessControlled;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
Expand All @@ -19,9 +8,9 @@
import java.util.List;
import java.util.StringTokenizer;

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

import jenkins.model.Jenkins;
import javax.annotation.Nullable;

import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.AncestorInPath;
Expand All @@ -34,6 +23,32 @@
import com.cloudbees.plugins.credentials.domains.DomainRequirement;
import com.cloudbees.plugins.credentials.domains.HostnameRequirement;

import hudson.Extension;
import hudson.Util;
import hudson.model.AbstractDescribableImpl;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.ItemGroup;
import hudson.security.ACL;
import hudson.security.AccessControlled;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;

/**
* Holder of all informations about a npm public/private registry.
* <p>
* This class keep all necessary information to access a npm registry that must
* be stored in a user config file.
* Typically information are:
* <ul>
* <li>the registry URL</li>
* <li>list of scope for the registry, used typical in private registry</li>
* <li>account credentials to access the registry</li>
* </ul>
*
* @author Nikolas Falco
*/
public class NPMRegistry extends AbstractDescribableImpl<NPMRegistry> implements Serializable {
private static final long serialVersionUID = -5199710867477461372L;

Expand All @@ -59,7 +74,7 @@ public NPMRegistry(@Nonnull String url, String credentialsId, String scopes) {
*
* @param url url of a npm registry
* @param credentialsId credentials identifier
* @param scoped if this registry was designed for a specific scope
* @param hasScopes if this registry was designed for a specific scope
* @param scopes url-safe characters, no leading dots or underscores
*/
@DataBoundConstructor
Expand All @@ -69,17 +84,31 @@ public NPMRegistry(@Nonnull String url, String credentialsId, boolean hasScopes,
this.scopes = hasScopes ? fixScope(Util.fixEmpty(scopes)) : null;
}

private String fixScope(String scope) {
@Nullable
private String fixScope(final @Nullable String scope) {
if (scope != null && scope.startsWith("@")) {
return scope.substring(1);
}
return scope;
}

/**
* Get the registry URL
*
* @return the registry URL
*/
@Nullable
public String getUrl() {
return url;
}

/**
* Get list of scope for this registry.
* <p>
* The scope are not prefixed with {@literal @} character.
*
* @return a space separated list of scope.
*/
public String getScopes() {
return scopes;
}
Expand All @@ -88,6 +117,13 @@ public boolean isHasScopes() {
return scopes != null;
}

/**
* Provide a list of scope for this registry.
* <p>
* The scope are not prefixed with {@literal @} character.
*
* @return list of scope.
*/
public List<String> getScopesAsList() {
List<String> result = Collections.emptyList();
if (isHasScopes()) {
Expand All @@ -96,14 +132,21 @@ public List<String> getScopesAsList() {
return result;
}

/**
* Get list of scope for this registry.
* <p>
* The scope are not prefixed with {@literal @} character.
*
* @return a space separated list of scope.
*/
public String getCredentialsId() {
return credentialsId;
}

@Extension
public static class DescriptorImpl extends Descriptor<NPMRegistry> {

public FormValidation doCheckScopes(@QueryParameter boolean hasScopes, @QueryParameter String scopes) {
public FormValidation doCheckScopes(final @CheckForNull @QueryParameter boolean hasScopes, @CheckForNull @QueryParameter String scopes) {
scopes = Util.fixEmptyAndTrim(scopes);
if (hasScopes) {
if (scopes == null) {
Expand All @@ -124,7 +167,7 @@ public FormValidation doCheckScopes(@QueryParameter boolean hasScopes, @QueryPar
return FormValidation.ok();
}

public FormValidation doCheckUrl(@QueryParameter String url) {
public FormValidation doCheckUrl(final @CheckForNull @QueryParameter String url) {
if (StringUtils.isBlank(url)) {
return FormValidation.error("Empty URL");
}
Expand All @@ -137,9 +180,9 @@ public FormValidation doCheckUrl(@QueryParameter String url) {
return FormValidation.ok();
}

public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup<?> context,
@QueryParameter String credentialsId,
@QueryParameter String url) {
public ListBoxModel doFillCredentialsIdItems(final @AncestorInPath ItemGroup<?> context,
final @CheckForNull @QueryParameter String credentialsId,
final @CheckForNull @QueryParameter String url) {
if (!hasPermission(context)) {
return new StandardUsernameListBoxModel().includeCurrentValue(credentialsId);
}
Expand All @@ -157,7 +200,7 @@ public ListBoxModel doFillCredentialsIdItems(@AncestorInPath ItemGroup<?> contex
.includeCurrentValue(credentialsId);
}

private boolean hasPermission(ItemGroup<?> context) {
private boolean hasPermission(final ItemGroup<?> context) {
AccessControlled controller = context instanceof AccessControlled ? (AccessControlled) context : Jenkins.getInstance();
return controller != null && controller.hasPermission(Computer.CONFIGURE);
}
Expand Down
37 changes: 31 additions & 6 deletions src/main/java/jenkins/plugins/nodejs/configfiles/Npmrc.java
Expand Up @@ -26,6 +26,12 @@ public class Npmrc {

private Map<Object, String> properties = new LinkedHashMap<>();

/**
* Parse the given file and store internally all user settings and
* comments.
*
* @param content a valid npmrc user config file content.
*/
public static Npmrc load(File file) throws IOException {
if (file == null) {
throw new NullPointerException("file is null");
Expand All @@ -42,6 +48,12 @@ public static Npmrc load(File file) throws IOException {
return config;
}

/**
* Parse the given content and store internally all user settings and
* comments.
*
* @param content a valid npmrc user config content.
*/
public void from(String content) {
if (content == null) {
return;
Expand Down Expand Up @@ -74,7 +86,7 @@ public void from(String content) {
/**
* Add a comment line at the end of the file.
*
* @param comment the text content without the ';' suffix
* @param comment the text content without the ';' prefix
*/
public void addComment(String comment) {
properties.put(new Comment() {}, comment);
Expand All @@ -97,20 +109,33 @@ public String toString() {
return sb.toString();
}

/**
* Write the content of user config to a file.
*
* @param file the destination file
* @throws IOException in case of I/O write error
*/
public void save(File file) throws IOException {
try (Writer writer = new FileWriterWithEncoding(file, UTF_8)) {
IOUtils.write(toString(), writer);
}
}

public boolean containsKey(String key) {
/**
* Returns {@literal true} if this map contains a user config for the
* specified key.
*
* @param key user setting whose presence in this config
* @return {@literal true} if this config already contains the specified key
*/
public boolean contains(String key) {
return properties.containsKey(key);
}

/**
* Get the value for the specified property key.
*
* @param key
* @param key user config entry key
* @return the property value
*/
public String get(String key) {
Expand All @@ -123,7 +148,7 @@ public String get(String key) {
*
* @param key property key
* @param value property value
* @return the old value associated to the property key, <code>null</code>
* @return the old value associated to the setting key, {@literal null}
* otherwise
*/
public String set(String key, String value) {
Expand All @@ -136,8 +161,8 @@ public String set(String key, String value) {
*
* @param key property key
* @param value property value
* @return {@code false} the old value associated to the property key,
* {@code true} otherwise
* @return {@literal false} the old value associated to the property key,
* {@literal true} otherwise
*/
public boolean set(String key, boolean value) {
return Boolean.parseBoolean(properties.put(key, Boolean.toString(value)));
Expand Down
Expand Up @@ -34,7 +34,7 @@ public RegistryHelper(@CheckForNull List<NPMRegistry> registries) {
/**
* Resolves all registry credentials and returns a map paring registry URL
* to credential.
*
*
* @param build a build being run
* @return map of registry URL - credential
*/
Expand Down Expand Up @@ -63,7 +63,6 @@ public Map<String, StandardUsernameCredentials> resolveCredentials(Run<?, ?> bui
* Fill the npmpc user config with the given registries.
*
* @param npmrcContent .npmrc user config
* @param registries a list of registry to insert into the user config
* @param registry2Credentials the credentials to be inserted into the user
* config (key: registry URL, value: Jenkins credentials)
* @return the updated version of the {@code npmrcContent} with the registry
Expand Down
Expand Up @@ -37,7 +37,7 @@ public void setUp() throws IOException {
@Test
public void testLoad() throws Exception {
Npmrc npmrc = Npmrc.load(file);
assertTrue(npmrc.containsKey("always-auth"));
assertTrue(npmrc.contains("always-auth"));
assertEquals("true", npmrc.get("always-auth"));
assertEquals("\"/var/lib/jenkins/tools/jenkins.plugins.nodejs.tools.NodeJSInstallation/Node_6.x\"",
npmrc.get("prefix"));
Expand All @@ -46,7 +46,7 @@ public void testLoad() throws Exception {
@Test
public void testAvoidParseError() throws Exception {
Npmrc npmrc = Npmrc.load(file);
assertFalse(npmrc.containsKey("browser"));
assertFalse(npmrc.contains("browser"));
}

@Test
Expand All @@ -60,7 +60,7 @@ public void testSave() throws Exception {

// reload content
npmrc = Npmrc.load(file);
assertTrue(npmrc.containsKey(testKey));
assertTrue(npmrc.contains(testKey));
assertEquals(testValue, npmrc.get(testKey));
}

Expand Down

0 comments on commit d518e3b

Please sign in to comment.