Skip to content
Permalink
Browse files

JENKINS-45419

  • Loading branch information...
klimas7 committed Nov 19, 2017
1 parent a7d90cf commit c262a4f11c4857dc4a228e36c4f9483c1fad3001
@@ -18,6 +18,7 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.google.common.base.Strings;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
@@ -283,6 +284,10 @@ public int compareTo(GitParameterDefinition pd) {
GitClient gitClient = getGitClient(jobWrapper, null, git, environment);
String gitUrl = Util.replaceMacro(remoteURL.toPrivateASCIIString(), environment);

if (notMatchUseRepository(gitUrl)) {
continue;
}

if (isTagType()) {
Set<String> tagSet = getTag(gitClient, gitUrl);
sortAndPutToParam(tagSet, paramList);
@@ -313,6 +318,20 @@ public int compareTo(GitParameterDefinition pd) {
return paramList;
}

private boolean notMatchUseRepository(String gitUrl) {
if (Strings.isNullOrEmpty(useRepository)) {
return false;
}
Pattern repositoryNamePattern;
try {
repositoryNamePattern = Pattern.compile(useRepository);
} catch (Exception e) {
LOGGER.log(Level.INFO, Messages.GitParameterDefinition_invalidUseRepositoryPattern(useRepository), e.getMessage());
return false;
}
return !repositoryNamePattern.matcher(gitUrl).find();
}

private boolean isRevisionType() {
return type.equalsIgnoreCase(PARAMETER_TYPE_REVISION);
}
@@ -13,6 +13,7 @@

import com.google.common.base.Strings;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.UserRemoteConfig;
import hudson.scm.SCM;
import net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.JobWrapper;

@@ -43,13 +44,23 @@
private static List<GitSCM> matchAndGetGitSCM(List<SCM> scms, Pattern repositoryNamePattern) {
List<GitSCM> gitSCMs = new ArrayList<>();
for (SCM scm : scms) {
if (scm instanceof GitSCM && repositoryNamePattern.matcher(((GitSCM) scm).getUserRemoteConfigs().get(0).getUrl()).find()) {
if (scm instanceof GitSCM && anyUserRemoteConfigMatch(scm, repositoryNamePattern)) {
gitSCMs.add((GitSCM) scm);
}
}
return gitSCMs;
}

private static boolean anyUserRemoteConfigMatch(SCM scm, Pattern repositoryNamePattern) {
List<UserRemoteConfig> userRemoteConfigs = ((GitSCM) scm).getUserRemoteConfigs();
for (UserRemoteConfig userRemoteConfig : userRemoteConfigs) {
if (repositoryNamePattern.matcher(userRemoteConfig.getUrl()).find()) {
return true;
}
}
return false;
}

private static List<GitSCM> getFirstGitSCM(List<SCM> scms) {
SCM scm = scms.get(0);
if (scm instanceof GitSCM) {
@@ -856,6 +856,40 @@ public void testMultiSCM() throws IOException, InterruptedException {
assertTrue(isListBoxItem(items, "origin/master"));
}

@Test
public void testMultiRepositoryInOneSCM() throws IOException, InterruptedException {
project = jenkins.createFreeStyleProject("projectHaveMultiRepositoryInOneSCM");
project.getBuildersList().add(new Shell("echo test"));
SCM gitSCM = getGitSCM(GIT_PARAMETER_REPOSITORY_URL, GIT_CLIENT_REPOSITORY_URL);
project.setScm(gitSCM);

GitParameterDefinition defGitParameter = new GitParameterDefinition("name_git_parameter",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
".*",
"*",
SortMode.ASCENDING, SelectedValue.TOP, ".*git-parameter-plugin.git", false);

GitParameterDefinition defGitCient = new GitParameterDefinition("name_git_client",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
".*",
"*",
SortMode.ASCENDING, SelectedValue.TOP, ".*git-client-plugin.git", false);

project.addProperty(new ParametersDefinitionProperty(defGitParameter, defGitCient));

ListBoxModel items = defGitParameter.getDescriptor().doFillValueItems(project, defGitParameter.getName());
assertTrue(isListBoxItem(items, "origin/master"));

items = defGitCient.getDescriptor().doFillValueItems(project, defGitCient.getName());
assertTrue(isListBoxItem(items, "origin1/master"));
}

@Test
public void testMultiSCM_forSubdirectoryForRepo() throws IOException, InterruptedException {
project = jenkins.createFreeStyleProject("projectHaveMultiSCM");
@@ -921,10 +955,12 @@ private SCM getGitSCM() {
return getGitSCM(GIT_PARAMETER_REPOSITORY_URL);
}

private SCM getGitSCM(String url) {
UserRemoteConfig config = new UserRemoteConfig(url, null, null, null);
private SCM getGitSCM(String... urls) {
List<UserRemoteConfig> configs = new ArrayList<UserRemoteConfig>();
configs.add(config);
for (String url : urls) {
UserRemoteConfig config = new UserRemoteConfig(url, null, null, null);
configs.add(config);
}
return new GitSCM(configs, null, false, null, null, null, null);
}

0 comments on commit c262a4f

Please sign in to comment.
You can’t perform that action at this time.