Skip to content

Commit

Permalink
JENKINS-45419
Browse files Browse the repository at this point in the history
  • Loading branch information
klimas7 committed Nov 19, 2017
1 parent a7d90cf commit c262a4f
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -283,6 +284,10 @@ public Map<String, String> generateContents(JobWrapper jobWrapper, GitSCM git) {
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);
Expand Down Expand Up @@ -313,6 +318,20 @@ public Map<String, String> generateContents(JobWrapper jobWrapper, GitSCM git) {
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);
}
Expand Down
Expand Up @@ -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;

Expand Down Expand Up @@ -43,13 +44,23 @@ public static List<GitSCM> getGitSCMs(JobWrapper jobWrapper, String repositoryRe
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) {
Expand Down
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit c262a4f

Please sign in to comment.