-
Notifications
You must be signed in to change notification settings - Fork 116
/
NPMConfig.java
153 lines (131 loc) · 5.45 KB
/
NPMConfig.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* The MIT License
*
* Copyright (c) 2018, Nikolas Falco
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.plugins.nodejs.configfiles;
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 org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.lib.configprovider.AbstractConfigProviderImpl;
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.plugins.nodejs.Messages;
/**
* A config/provider to handle the special case of a npmrc config file
*
* @author Nikolas Falco
* @since 1.0
*/
public class NPMConfig extends Config {
private static final long serialVersionUID = 1L;
private final List<NPMRegistry> registries;
@DataBoundConstructor
public NPMConfig(@Nonnull String id, String name, String comment, String content, List<NPMRegistry> registries) {
super(id, Util.fixEmptyAndTrim(name), Util.fixEmptyAndTrim(comment), Util.fixEmptyAndTrim(content));
this.registries = registries == null ? new ArrayList<NPMRegistry>(3) : registries;
}
public List<NPMRegistry> getRegistries() {
return registries;
}
/**
* Perform a validation of the configuration.
* <p>
* If validation pass then no {@link VerifyConfigProviderException} will be
* raised.
*
* @throws VerifyConfigProviderException
* in case this configuration is not valid.
*/
public void doVerify() throws VerifyConfigProviderException {
// check if more than registry is setup to be global
NPMRegistry globalRegistry = null;
for (NPMRegistry registry : registries) {
registry.doVerify();
if (!registry.isHasScopes()) {
if (globalRegistry != null) {
throw new VerifyConfigProviderException(Messages.NPMConfig_verifyTooGlobalRegistry());
}
globalRegistry = registry;
}
}
}
@Extension
public static class NPMConfigProvider extends AbstractConfigProviderImpl {
public NPMConfigProvider() {
load();
}
@Override
public ContentType getContentType() {
return null;
}
@Override
public String getDisplayName() {
return Messages.NPMConfig_displayName();
}
@Override
public Config newConfig(@Nonnull String configId) {
return new NPMConfig(configId, "MyNpmrcConfig", "user config", loadTemplateContent(), null);
}
protected String loadTemplateContent() {
try (InputStream is = this.getClass().getResourceAsStream("template.npmrc")) {
return IOUtils.toString(is, "UTF-8");
} catch (IOException e) { // NOSONAR
return null;
}
}
@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;
}
}
}