
com.github.nfalco79.jenkins.plugins.configfiles.GemConfig Maven / Gradle / Ivy
Show all versions of ext-configfiles Show documentation
/*
* Copyright 2021 Nikolas Falco
*
* Licensed under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.github.nfalco79.jenkins.plugins.configfiles;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
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.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.verb.POST;
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials;
import com.github.nfalco79.jenkins.plugins.configfiles.util.CredentialsUtil;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Util;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
/**
* A config/provider to handle the special case of a gem config file.
*
* @author Nikolas Falco
* @since 1.0
*/
public class GemConfig extends Config {
private static final long serialVersionUID = 1L;
private String apiKey;
private final List sources;
@DataBoundConstructor
public GemConfig(@NonNull String id, String name, String comment, String content, List sources) {
super(id, Util.fixEmptyAndTrim(name), Util.fixEmptyAndTrim(comment), Util.fixEmptyAndTrim(content));
this.sources = sources == null ? new ArrayList<>(3) : sources;
}
public List getSources() {
return sources;
}
public String getApiKey() {
return apiKey;
}
@DataBoundSetter
public void setApiKey(String apiKey) {
this.apiKey = Util.fixEmptyAndTrim(apiKey);
}
/**
* Perform a validation of the configuration.
*
* If validation pass then no {@link VerifyConfigProviderException} will be
* raised.
*
* @throws VerifyConfigProviderException
* in case this configuration is not valid.
*/
public void doVerify() throws VerifyConfigProviderException {
for (GemSource source : sources) {
source.doVerify();
}
}
@Extension
public static class GemConfigProvider extends AbstractConfigProviderImpl {
public GemConfigProvider() {
load();
}
@Override
public ContentType getContentType() {
return null;
}
@Override
public String getDisplayName() {
return Messages.GemConfig_displayName();
}
@Override
public Config newConfig(@NonNull String configId) {
return new GemConfig(configId, "MyGemConfig", "user config", loadTemplateContent(), null);
}
@POST
public FormValidation doCheckApiKey(@CheckForNull @AncestorInPath Item projectOrFolder, //
@QueryParameter String apiKey) {
return CredentialsUtil.doCheckCredentialsId(projectOrFolder, apiKey, null);
}
@POST
public ListBoxModel doFillApiKeyItems(final @CheckForNull @AncestorInPath ItemGroup> context, //
final @CheckForNull @AncestorInPath Item projectOrFolder, //
@QueryParameter String apiKey) {
return CredentialsUtil.doFillCredentialsIdItems(context, projectOrFolder, apiKey, null);
}
protected String loadTemplateContent() {
try (InputStream is = this.getClass().getResourceAsStream("template.gemrc")) {
return IOUtils.toString(is, StandardCharsets.UTF_8);
} catch (IOException e) { // NOSONAR
return null;
}
}
@Override
public String supplyContent(Config configFile, Run, ?> build, FilePath workDir, TaskListener listener, List tempFiles) throws IOException {
String fileContent = configFile.content;
if (configFile instanceof GemConfig) {
GemConfig config = (GemConfig) configFile;
GemConfigHelper helper = new GemConfigHelper(config.getSources());
List sources = config.getSources();
if (!sources.isEmpty()) {
listener.getLogger().println("Adding all server entries");
Map source2Credentials = helper.resolveCredentials(build);
fileContent = helper.fillSources(fileContent, source2Credentials);
}
String apiKey = config.getApiKey();
if (apiKey != null) {
listener.getLogger().println("Adding API Key entry");
fileContent = helper.fillApiKey(fileContent, apiKey, build);
}
try {
if (StringUtils.isNotBlank(fileContent)) { // NOSONAR
config.doVerify();
}
} catch (VerifyConfigProviderException e) {
throw new AbortException("Invalid user config: " + e.getMessage());
}
}
return fileContent;
}
}
}