
io.jenkins.updatebot.commands.CommandSupport Maven / Gradle / Ivy
/*
* Copyright 2016 Red Hat, Inc.
*
* Red Hat licenses this file to you 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 io.jenkins.updatebot.commands;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import io.jenkins.updatebot.Configuration;
import io.jenkins.updatebot.git.GitHelper;
import io.jenkins.updatebot.github.Issues;
import io.jenkins.updatebot.model.RepositoryConfig;
import io.jenkins.updatebot.repository.LocalRepository;
import io.jenkins.updatebot.repository.Repositories;
import io.fabric8.utils.Strings;
import io.jenkins.updatebot.support.UserPassword;
import org.kohsuke.github.GHIssue;
import org.kohsuke.github.GHRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import static io.jenkins.updatebot.github.PullRequests.COMMAND_COMMENT_INDENT;
import static io.jenkins.updatebot.github.PullRequests.COMMAND_COMMENT_PREFIX;
import static io.jenkins.updatebot.github.PullRequests.COMMAND_COMMENT_PREFIX_SEPARATOR;
import static io.jenkins.updatebot.support.ReflectionHelper.findFieldsAnnotatedWith;
import static io.jenkins.updatebot.support.ReflectionHelper.getFieldValue;
import static io.jenkins.updatebot.support.Strings.*;
/**
*/
public abstract class CommandSupport {
private static final transient Logger LOG = LoggerFactory.getLogger(PushSourceChanges.class);
private List localRepositories;
private RepositoryConfig repositoryConfig;
public String createPullRequestComment() {
StringBuilder builder = new StringBuilder(COMMAND_COMMENT_PREFIX);
builder.append(COMMAND_COMMENT_PREFIX_SEPARATOR);
appendPullRequestComment(builder);
return builder.toString();
}
protected void appendPullRequestComment(StringBuilder builder) {
builder.append(COMMAND_COMMENT_INDENT);
Parameters annotation = getClass().getAnnotation(Parameters.class);
if (annotation != null) {
String[] commandNames = annotation.commandNames();
if (commandNames != null && commandNames.length > 0) {
builder.append(commandNames[0]);
}
}
appendPullRequestCommentArguments(builder);
builder.append("\n");
}
/**
* Appends any command specific parameters
*/
protected void appendPullRequestCommentArguments(StringBuilder builder) {
List fields = findFieldsAnnotatedWith(getClass(), Parameter.class);
appendPullRequestsCommentArguments(builder, fields, true);
appendPullRequestsCommentArguments(builder, fields, false);
}
private void appendPullRequestsCommentArguments(StringBuilder builder, List fields, boolean namedArguments) {
for (Field field : fields) {
Parameter parameter = field.getAnnotation(Parameter.class);
String[] names = parameter.names();
if (names.length > 0) {
String name = names[0];
if (!namedArguments) {
continue;
}
builder.append(" ");
builder.append(name);
} else {
if (namedArguments) {
continue;
}
}
Object value = getFieldValue(field, this);
if (value != null) {
builder.append(" ");
if (value instanceof Collection) {
builder.append(Strings.join((Collection) value, " "));
} else if (value instanceof Object[]) {
builder.append(Strings.join(" ", (Object[]) value));
} else {
builder.append(value);
}
}
}
}
public ParentContext run(Configuration configuration) throws IOException {
validateConfiguration(configuration);
ParentContext parentContext = new ParentContext();
List repositories = cloneOrPullRepositories(configuration);
for (LocalRepository repository : repositories) {
CommandContext context = createCommandContext(repository, configuration);
parentContext.addChild(context);
run(context);
}
return parentContext;
}
protected void validateConfiguration(Configuration configuration) throws IOException {
if (empty(configuration.getGithubUsername()) || empty(configuration.getGithubPassword())) {
discoverGitCredentials(configuration);
}
}
protected void discoverGitCredentials(Configuration configuration) {
Map credentials = configuration.getGitCredentials();
String home = System.getProperty("user.home", ".");
configuration.info(LOG, "Looking for .git-credentials in " + home);
File gitCredentials = new File(home, ".git-credentials");
GitHelper.loadGitCredentials(credentials, gitCredentials);
if (credentials.isEmpty()) {
home = System.getenv("HOME");
configuration.info(LOG, "Looking for .git-credentials in " + home);
gitCredentials = new File(home, ".git-credentials");
GitHelper.loadGitCredentials(credentials, gitCredentials);
}
if (credentials.isEmpty()) {
String configHome = System.getenv("XDG_CONFIG_HOME");
configuration.info(LOG, "Looking for credentials in " + configHome);
if (io.jenkins.updatebot.support.Strings.notEmpty(configHome)) {
GitHelper.loadGitCredentials(credentials, new File(configHome, "git/credentials"));
if (credentials.isEmpty()) {
GitHelper.loadGitCredentials(credentials, new File(home, ".config/git/credentials"));
}
}
}
if (io.jenkins.updatebot.support.Strings.empty(configuration.getGithubUsername()) && io.jenkins.updatebot.support.Strings.empty(configuration.getGithubPassword())) {
UserPassword userPassword = credentials.get("github.com");
if (userPassword != null) {
configuration.info(LOG, "Loaded from git credentials");
configuration.setGithubUsername(userPassword.getUser());
configuration.setGithubPassword(userPassword.getPassword());
}
}
}
protected CommandContext createCommandContext(LocalRepository repository, Configuration configuration) {
return new CommandContext(repository, configuration);
}
public abstract void run(CommandContext context) throws IOException;
public List cloneOrPullRepositories(Configuration configuration) throws IOException {
return getLocalRepositories(configuration);
}
public List getLocalRepositories(Configuration configuration) throws IOException {
if (localRepositories == null) {
RepositoryConfig repositoryConfig = getRepositoryConfig(configuration);
this.localRepositories = Repositories.cloneOrPullRepositories(configuration, repositoryConfig);
}
return localRepositories;
}
public RepositoryConfig getRepositoryConfig(Configuration configuration) throws IOException {
if (repositoryConfig == null) {
repositoryConfig = configuration.loadRepositoryConfig();
}
return repositoryConfig;
}
// Properties
//-------------------------------------------------------------------------
protected GHIssue getOrFindIssue(CommandContext context, GHRepository ghRepository) throws IOException {
GHIssue issue = context.getIssue();
if (issue == null) {
List issues = Issues.getOpenIssues(ghRepository, context.getConfiguration());
issue = Issues.findIssue(context, issues);
context.setIssue(issue);
}
return issue;
}
protected void setLocalRepositories(List localRepositories) {
this.localRepositories = localRepositories;
}
protected void setRepositoryConfig(RepositoryConfig repositoryConfig) {
this.repositoryConfig = repositoryConfig;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy