Skip to content
Permalink
Browse files

JENKINS-37953 Add support to ProxySCM

  • Loading branch information...
klimas7 committed Sep 4, 2016
1 parent 9b1243d commit cff5e5e6ff7514388131966625c09e9199a52017
12 pom.xml
@@ -111,6 +111,18 @@
<version>1.15</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>template-project</artifactId>
<version>1.5.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>multiple-scms</artifactId>
<version>0.6</version>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
@@ -1,7 +1,6 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter;

import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@@ -30,15 +29,15 @@
import hudson.model.TopLevelItem;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.UserRemoteConfig;
import hudson.scm.SCM;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.JobWrapper;
import net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.JobWrapperFactory;
import net.uaznia.lukanus.hudson.plugins.gitparameter.scms.RepoSCM;
import net.uaznia.lukanus.hudson.plugins.gitparameter.scms.SCMFactory;
import org.apache.commons.lang.StringUtils;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
@@ -56,12 +55,9 @@
public static final String PARAMETER_TYPE_REVISION = "PT_REVISION";
public static final String PARAMETER_TYPE_BRANCH = "PT_BRANCH";
public static final String PARAMETER_TYPE_TAG_BRANCH = "PT_BRANCH_TAG";
private static final Logger LOGGER = Logger.getLogger(GitParameterDefinition.class.getName());
public static final String TEMPORARY_DIRECTORY_PREFIX = "git_parameter_";

private static final String REPO_SCM_CLASS_NAME = "hudson.plugins.repo.RepoScm";
private static final String REPO_SCM_NAME = "repo";
private static final String REPO_MANIFESTS_DIR = ".repo/manifests";
private static final Logger LOGGER = Logger.getLogger(GitParameterDefinition.class.getName());

private final UUID uuid;
private String type;
@@ -259,7 +255,7 @@ public int compareTo(GitParameterDefinition pd) {
try {
EnvVars environment = getEnvironment(jobWrapper);
for (RemoteConfig repository : git.getRepositories()) {
boolean isRepoScm = REPO_SCM_NAME.equals(repository.getName());
boolean isRepoScm = RepoSCM.isRepoSCM(repository.getName());
synchronized (GitParameterDefinition.class) {
FilePathWrapper workspace = getWorkspace(jobWrapper, isRepoScm);
GitClient gitClient = getGitClient(jobWrapper, workspace, git, environment);
@@ -351,7 +347,7 @@ private void sortAndPutToParam(Set<String> setElement, Map<String, String> param
private FilePathWrapper getWorkspace(JobWrapper jobWrapper, boolean isRepoScm) throws IOException, InterruptedException {
FilePathWrapper someWorkspace = new FilePathWrapper(jobWrapper.getSomeWorkspace());
if (isRepoScm) {
FilePath repoDir = new FilePath(someWorkspace.getFilePath(), REPO_MANIFESTS_DIR);
FilePath repoDir = new FilePath(someWorkspace.getFilePath(), RepoSCM.getRepoMainfestsDir());
if (repoDir.exists()) {
someWorkspace = new FilePathWrapper(repoDir);
} else {
@@ -466,39 +462,7 @@ public ListBoxModel doFillValueItems(@AncestorInPath Job job, @QueryParameter St
}

public GitSCM getProjectSCM(JobWrapper jobWrapper) {
SCM projectScm = null;
if (jobWrapper != null) {
projectScm = jobWrapper.getScm();
}

if (projectScm instanceof GitSCM) {
return (GitSCM) projectScm;
}

if (isRepoScm(projectScm)) {
return getGitSCMFromRepoScm(projectScm);
}

return null;
}

private static boolean isRepoScm(SCM projectScm) {
return projectScm != null && REPO_SCM_CLASS_NAME.equals(projectScm.getClass().getName());
}

private static GitSCM getGitSCMFromRepoScm(SCM projectScm) {
try {
Class<?> clazz = projectScm.getClass();
Method method = clazz.getDeclaredMethod("getManifestRepositoryUrl");
String repositoryUrl = (String) method.invoke(projectScm);
UserRemoteConfig config = new UserRemoteConfig(repositoryUrl, REPO_SCM_NAME, null, null);
List<UserRemoteConfig> configs = new ArrayList<UserRemoteConfig>();
configs.add(config);
return new GitSCM(configs, null, false, null, null, null, null);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, Messages.GitParameterDefinition_getRepoScmFailed(), e);
}
return null;
return SCMFactory.getGitSCM(jobWrapper);
}

public FormValidation doCheckBranchFilter(@QueryParameter String value) {
@@ -0,0 +1,10 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.scms;

import hudson.scm.SCM;

public class EmptySCM implements SCMWrapper {
@Override
public SCM getSCM() {
return null;
}
}
@@ -0,0 +1,32 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.scms;

import java.lang.reflect.Method;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.scm.SCM;

public class ProxySCM implements SCMWrapper {
public static final String PROXY_SCM_CLASS_NAME = "hudson.plugins.templateproject.ProxySCM";
private static final Logger LOGGER = Logger.getLogger(ProxySCM.class.getName());
private SCM scm;

public ProxySCM(SCM scm) {
this.scm = scm;
}

@Override
public SCM getSCM() {
//((ProxySCM)projectScm).getProjectScm()
try {
Class<?> clazz = scm.getClass();
Method getProjectScmMethod = clazz.getDeclaredMethod("getProjectScm");

SCM projectSCM = (SCM) getProjectScmMethod.invoke(scm);
return projectSCM;
} catch (Exception e) {
LOGGER.log(Level.SEVERE, Messages.ProxySCM_getSCMFromProxySCM(), e);
}
return null;
}
}
@@ -0,0 +1,47 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.scms;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.plugins.git.GitSCM;
import hudson.plugins.git.UserRemoteConfig;
import hudson.scm.SCM;

public class RepoSCM implements SCMWrapper {
public static final String REPO_SCM_CLASS_NAME = "hudson.plugins.repo.RepoScm";
private static final String REPO_SCM_NAME = "repo";
private static final String REPO_MANIFESTS_DIR = ".repo/manifests";
private static final Logger LOGGER = Logger.getLogger(RepoSCM.class.getName());
private SCM scm;

public RepoSCM(SCM scm) {
this.scm = scm;
}

@Override
public SCM getSCM() {
try {
Class<?> clazz = scm.getClass();
Method method = clazz.getDeclaredMethod("getManifestRepositoryUrl");
String repositoryUrl = (String) method.invoke(scm);
UserRemoteConfig config = new UserRemoteConfig(repositoryUrl, REPO_SCM_NAME, null, null);
List<UserRemoteConfig> configs = new ArrayList<>();
configs.add(config);
return new GitSCM(configs, null, false, null, null, null, null);
} catch (Exception e) {
LOGGER.log(Level.SEVERE, Messages.RepoSCM_getRepoScmFailed(), e);
}
return null;
}

public static boolean isRepoSCM(String repositoryName) {
return REPO_SCM_NAME.equals(repositoryName);
}

public static String getRepoMainfestsDir() {
return REPO_MANIFESTS_DIR;
}
}
@@ -0,0 +1,46 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.scms;

import static net.uaznia.lukanus.hudson.plugins.gitparameter.scms.ProxySCM.PROXY_SCM_CLASS_NAME;
import static net.uaznia.lukanus.hudson.plugins.gitparameter.scms.RepoSCM.REPO_SCM_CLASS_NAME;

import hudson.plugins.git.GitSCM;
import hudson.scm.SCM;
import net.uaznia.lukanus.hudson.plugins.gitparameter.jobs.JobWrapper;

public class SCMFactory {
private static final SCMWrapper EMPTY_SCM = new EmptySCM();

public static GitSCM getGitSCM(JobWrapper jobWrapper) {
SCM scm = getSCM(jobWrapper);
if (scm instanceof GitSCM) {
return (GitSCM) scm;
}

return null;
}

private static SCM getSCM(JobWrapper jobWrapper) {
if (jobWrapper == null) {
return EMPTY_SCM.getSCM();
}

SCM projectSCM = jobWrapper.getScm();

if (projectSCM == null) {
return EMPTY_SCM.getSCM();
}

String projectSCMClassName = projectSCM.getClass().getName();
if (PROXY_SCM_CLASS_NAME.equals(projectSCMClassName)) {
projectSCM = new ProxySCM(projectSCM).getSCM();
projectSCMClassName = projectSCM.getClass().getName();
}

if (REPO_SCM_CLASS_NAME.equals(projectSCMClassName)) {
projectSCM = new RepoSCM(projectSCM).getSCM();
}

return projectSCM;
}

}
@@ -0,0 +1,7 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter.scms;

import hudson.scm.SCM;

public interface SCMWrapper {
SCM getSCM();
}
@@ -5,5 +5,4 @@ GitParameterDefinition.unexpectedError=Unexpected error!
GitParameterDefinition.lookAtLog=Please look at the Log
GitParameterDefinition.branchFilterNotValid=Specified branchFilter is not a valid regex. Setting to '.*'
GitParameterDefinition.genContentsCloneDone=generateContents clone done
GitParameterDefinition.getRepoScmFailed=get repo scm failed
GitParameterDefinition.notFindAuthorPattern=Did not find author pattern {0}
GitParameterDefinition.notFindAuthorPattern=Did not find author pattern {0}
@@ -5,5 +5,4 @@ GitParameterDefinition.unexpectedError=Niespodziewany b\u0142\u0105d!
GitParameterDefinition.lookAtLog=Prosz\u0119 sprawd\u017A logi
GitParameterDefinition.branchFilterNotValid=Ustawienie branchFilter nie jest prawid\u0142owym wyra\u017Ceniem regularnym. Ustawiono '.*'
GitParameterDefinition.genContentsCloneDone=Klonowanie wykonane
GitParameterDefinition.getRepoScmFailed=B\u0142\u0105d przy obs\u0142udze repo scm
GitParameterDefinition.notFindAuthorPattern=Nie znaleziono wzorca autora
GitParameterDefinition.notFindAuthorPattern=Nie znaleziono wzorca autora
@@ -0,0 +1,2 @@
ProxySCM.getSCMFromProxySCM=get scm from proxy scm failed
RepoSCM.getRepoScmFailed=get repo scm failed
@@ -0,0 +1,2 @@
ProxySCM.getSCMFromProxySCM=B\u0142\u0105d przy obs\u0142udze proxy scm
RepoSCM.getRepoScmFailed=B\u0142\u0105d przy obs\u0142udze repo scm
@@ -24,6 +24,7 @@
import hudson.model.Result;
import hudson.plugins.git.GitSCM;
import hudson.plugins.git.UserRemoteConfig;
import hudson.plugins.templateproject.ProxySCM;
import hudson.scm.SCM;
import hudson.tasks.Shell;
import hudson.util.FormValidation;
@@ -683,6 +684,30 @@ public void testWorkflowJob() throws IOException, InterruptedException {
assertTrue(isListBoxItem(items, "git-parameter-0.2"));
}

@Test
public void testProxySCM() throws IOException, InterruptedException {
FreeStyleProject anotherProject = jenkins.createFreeStyleProject("AnotherProject");
anotherProject.getBuildersList().add(new Shell("echo test"));
anotherProject.setScm(getGitSCM());

project = jenkins.createFreeStyleProject("projectHaveProxySCM");
project.setScm(new ProxySCM("AnotherProject"));

GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_TAG,
null,
"testDescription",
null,
".*",
"*",
SortMode.ASCENDING, SelectedValue.TOP, false);

project.addProperty(new ParametersDefinitionProperty(def));
ListBoxModel items = def.getDescriptor().doFillValueItems(project, def.getName());
assertTrue(isListBoxItem(items, "git-parameter-0.2"));
}


private void setupGit() throws IOException {
setupGit(repositoryUrl);
}

0 comments on commit cff5e5e

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