Move code for retrieving branch auto completion candidates to ProjectBranchProvider

This commit is contained in:
Robin Müller 2016-03-09 22:13:54 +01:00
parent 2db8ef562e
commit a4723698ad
2 changed files with 38 additions and 34 deletions

View File

@ -268,39 +268,12 @@ public class GitLabPushTrigger extends Trigger<Job<?, ?>> implements WebHookTrig
return Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(spec)); return Lists.newArrayList(Splitter.on(',').omitEmptyStrings().trimResults().split(spec));
} }
private AutoCompletionCandidates doAutoCompleteBranchesSpec(final Job<?, ?> job, @QueryParameter final String value) {
String query = value.toLowerCase();
final AutoCompletionCandidates ac = new AutoCompletionCandidates();
List<String> values = ac.getValues();
try {
List<String> branches = ProjectBranchesProvider.instance().getProjectBranches(job);
// show all suggestions for short strings
if (query.length() < 2){
values.addAll(branches);
} else {
for (String branch : branches){
if (branch.toLowerCase().indexOf(query) > -1){
values.add(branch);
}
}
}
} catch (final IllegalStateException ex) {
LOGGER.log(Level.FINEST, "Unexpected IllegalStateException. Please check the logs and your configuration.", ex);
} catch (final IOException ex) {
LOGGER.log(Level.FINEST, "Unexpected IllegalStateException. Please check the logs and your configuration.", ex);
}
return ac;
}
public AutoCompletionCandidates doAutoCompleteIncludeBranchesSpec(@AncestorInPath final Job<?, ?> job, @QueryParameter final String value) { public AutoCompletionCandidates doAutoCompleteIncludeBranchesSpec(@AncestorInPath final Job<?, ?> job, @QueryParameter final String value) {
return this.doAutoCompleteBranchesSpec(job, value); return ProjectBranchesProvider.instance().doAutoCompleteBranchesSpec(job, value);
} }
public AutoCompletionCandidates doAutoCompleteExcludeBranchesSpec(@AncestorInPath final Job<?, ?> job, @QueryParameter final String value) { public AutoCompletionCandidates doAutoCompleteExcludeBranchesSpec(@AncestorInPath final Job<?, ?> job, @QueryParameter final String value) {
return this.doAutoCompleteBranchesSpec(job, value); return ProjectBranchesProvider.instance().doAutoCompleteBranchesSpec(job, value);
} }
private FormValidation doCheckBranchesSpec(@AncestorInPath final Job<?, ?> project, @QueryParameter final String value) { private FormValidation doCheckBranchesSpec(@AncestorInPath final Job<?, ?> project, @QueryParameter final String value) {

View File

@ -3,12 +3,14 @@ package com.dabsquared.gitlabjenkins.trigger.branch;
import com.dabsquared.gitlabjenkins.GitLabProjectBranchesService; import com.dabsquared.gitlabjenkins.GitLabProjectBranchesService;
import com.dabsquared.gitlabjenkins.Messages; import com.dabsquared.gitlabjenkins.Messages;
import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty; import com.dabsquared.gitlabjenkins.connection.GitLabConnectionProperty;
import hudson.model.AutoCompletionCandidates;
import hudson.model.Job; import hudson.model.Job;
import hudson.plugins.git.GitSCM; import hudson.plugins.git.GitSCM;
import hudson.scm.SCM; import hudson.scm.SCM;
import jenkins.triggers.SCMTriggerItem; import jenkins.triggers.SCMTriggerItem;
import org.eclipse.jgit.transport.RemoteConfig; import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish; import org.eclipse.jgit.transport.URIish;
import org.kohsuke.stapler.QueryParameter;
import java.io.IOException; import java.io.IOException;
import java.util.Collections; import java.util.Collections;
@ -24,7 +26,8 @@ public final class ProjectBranchesProvider {
private static final Logger LOGGER = Logger.getLogger(ProjectBranchesProvider.class.getName()); private static final Logger LOGGER = Logger.getLogger(ProjectBranchesProvider.class.getName());
private static final ProjectBranchesProvider INSTANCE = new ProjectBranchesProvider(); private static final ProjectBranchesProvider INSTANCE = new ProjectBranchesProvider();
private ProjectBranchesProvider() { } private ProjectBranchesProvider() {
}
public static ProjectBranchesProvider instance() { public static ProjectBranchesProvider instance() {
return INSTANCE; return INSTANCE;
@ -41,6 +44,34 @@ public final class ProjectBranchesProvider {
} }
} }
public AutoCompletionCandidates doAutoCompleteBranchesSpec(Job<?, ?> job, String query) {
AutoCompletionCandidates result = new AutoCompletionCandidates();
// show all suggestions for short strings
if (query.length() < 2) {
result.add(getProjectBranchesAsArray(job));
} else {
for (String branch : getProjectBranchesAsArray(job)) {
if (branch.toLowerCase().contains(query.toLowerCase())) {
result.add(branch);
}
}
}
return result;
}
private String[] getProjectBranchesAsArray(Job<?, ?> job) {
try {
List<String> branches = getProjectBranches(job);
return branches.toArray(new String[branches.size()]);
} catch (final IllegalStateException ex) {
LOGGER.log(Level.FINEST, "Unexpected IllegalStateException. Please check the logs and your configuration.", ex);
} catch (final IOException ex) {
LOGGER.log(Level.FINEST, "Unexpected IllegalStateException. Please check the logs and your configuration.", ex);
}
return new String[0];
}
/** /**
* Get the URL of the first declared repository in the project configuration. * Get the URL of the first declared repository in the project configuration.
* Use this as default source repository url. * Use this as default source repository url.
@ -51,7 +82,7 @@ public final class ProjectBranchesProvider {
private URIish getSourceRepoURLDefault(Job<?, ?> job) { private URIish getSourceRepoURLDefault(Job<?, ?> job) {
SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job); SCMTriggerItem item = SCMTriggerItem.SCMTriggerItems.asSCMTriggerItem(job);
GitSCM gitSCM = getGitSCM(item); GitSCM gitSCM = getGitSCM(item);
if(gitSCM == null) { if (gitSCM == null) {
LOGGER.log(Level.WARNING, "Could not find GitSCM for project. Project = {1}, next build = {2}", LOGGER.log(Level.WARNING, "Could not find GitSCM for project. Project = {1}, next build = {2}",
array(job.getName(), String.valueOf(job.getNextBuildNumber()))); array(job.getName(), String.valueOf(job.getNextBuildNumber())));
throw new IllegalStateException("This project does not use git:" + job.getName()); throw new IllegalStateException("This project does not use git:" + job.getName());
@ -70,9 +101,9 @@ public final class ProjectBranchesProvider {
} }
private GitSCM getGitSCM(SCMTriggerItem item) { private GitSCM getGitSCM(SCMTriggerItem item) {
if(item != null) { if (item != null) {
for(SCM scm : item.getSCMs()) { for (SCM scm : item.getSCMs()) {
if(scm instanceof GitSCM) { if (scm instanceof GitSCM) {
return (GitSCM) scm; return (GitSCM) scm;
} }
} }