Skip to content
Permalink
Browse files

JENKINS-40232: PoC

  • Loading branch information...
klimas7 committed Dec 31, 2016
1 parent 4af4453 commit 734ac6b91911a05e47019908dd1b9650f766b72d
@@ -6,6 +6,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -27,7 +28,7 @@
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.model.TopLevelItem;
import hudson.plugins.git.Branch;
import hudson.plugins.git.GitException;
import hudson.plugins.git.GitSCM;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
@@ -39,6 +40,7 @@
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.lib.ObjectId;
import org.eclipse.jgit.transport.RemoteConfig;
import org.eclipse.jgit.transport.URIish;
import org.jenkinsci.plugins.gitclient.FetchCommand;
@@ -257,29 +259,33 @@ public int compareTo(GitParameterDefinition pd) {
try {
EnvVars environment = getEnvironment(jobWrapper);
for (RemoteConfig repository : git.getRepositories()) {
boolean isRepoScm = RepoSCM.isRepoSCM(repository.getName());
synchronized (GitParameterDefinition.class) {
FilePathWrapper workspace = getWorkspace(jobWrapper, isRepoScm);
GitClient gitClient = getGitClient(jobWrapper, workspace, git, environment);
for (URIish remoteURL : repository.getURIs()) {
initWorkspace(workspace, gitClient, remoteURL);
String gitUrl = remoteURL.toPrivateASCIIString();
GitClient gitClient = getGitClient(jobWrapper, null, git, environment);

FetchCommand fetch = gitClient.fetch_().prune().from(remoteURL, repository.getFetchRefSpecs());
fetch.execute();

if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) {
getRevision(paramList, gitClient);
}
if (type.equalsIgnoreCase(PARAMETER_TYPE_TAG) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {
Set<String> tagSet = gitClient.getTagNames(tagFilter);
Set<String> tagSet = getTag(gitClient, gitUrl);
sortAndPutToParam(tagSet, paramList);
}
if (type.equalsIgnoreCase(PARAMETER_TYPE_BRANCH) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH)) {
Set<String> branchSet = getBranch(gitClient);
Set<String> branchSet = getBranch(gitClient, gitUrl);
sortAndPutToParam(branchSet, paramList);
}

if (type.equalsIgnoreCase(PARAMETER_TYPE_REVISION)) {
//TODO investigation
boolean isRepoScm = RepoSCM.isRepoSCM(repository.getName());
FilePathWrapper workspace = getWorkspace(jobWrapper, isRepoScm);

initWorkspace(workspace, gitClient, remoteURL);
gitClient = getGitClient(jobWrapper, workspace, git, environment);
FetchCommand fetch = gitClient.fetch_().prune().from(remoteURL, repository.getFetchRefSpecs());
fetch.execute();
getRevision(paramList, gitClient);
workspace.delete();
}
}
workspace.delete();
break;
}
}
@@ -292,7 +298,20 @@ public int compareTo(GitParameterDefinition pd) {
return paramList;
}

private Set<String> getBranch(GitClient gitClient) throws InterruptedException {
private Set<String> getTag(GitClient gitClient, String gitUrl) throws InterruptedException {
Set<String> tagSet = new HashSet<String>();
try {
Map<String, ObjectId> tags = gitClient.getRemoteReferences(gitUrl, tagFilter, false, true);
for (String tagName : tags.keySet()) {
tagSet.add(tagName.replaceFirst(".*refs/tags/", ""));
}
} catch (GitException e) {
LOGGER.warning("msg"); //TODO
}
return tagSet;
}

private Set<String> getBranch(GitClient gitClient, String gitUrl) throws InterruptedException {
Set<String> branchSet = new HashSet<String>();
Pattern branchFilterPattern;
try {
@@ -301,8 +320,13 @@ public int compareTo(GitParameterDefinition pd) {
LOGGER.log(Level.INFO, Messages.GitParameterDefinition_branchFilterNotValid(), e.getMessage());
branchFilterPattern = Pattern.compile(".*");
}
for (Branch branch : gitClient.getRemoteBranches()) {
String branchName = branch.getName();

String defaultRemote = "origin";//TODO ((GitAPI) gitClient).getDefaultRemote();

Map<String, ObjectId> branches = gitClient.getRemoteReferences(gitUrl, null, true, false);
Iterator<String> remoteBranchesName = branches.keySet().iterator();
while (remoteBranchesName.hasNext()) {
String branchName = strip(remoteBranchesName.next(), defaultRemote);
Matcher matcher = branchFilterPattern.matcher(branchName);
if (matcher.matches()) {
if (matcher.groupCount() == 1) {
@@ -315,6 +339,12 @@ public int compareTo(GitParameterDefinition pd) {
return branchSet;
}

//Branch.strip
private String strip(String name, String remote) {

return remote + "/" + name.substring(name.indexOf(47, 5) + 1);
}

private void getRevision(Map<String, String> paramList, GitClient gitClient) throws InterruptedException {
RevisionInfoFactory revisionInfoFactory = new RevisionInfoFactory(gitClient, branch);
List<RevisionInfo> revisions = revisionInfoFactory.getRevisions();
@@ -397,7 +427,7 @@ private GitClient getGitClient(final JobWrapper jobWrapper, FilePathWrapper work
int nextBuildNumber = jobWrapper.getNextBuildNumber();

GitClient gitClient = git.createClient(TaskListener.NULL, environment, new Run(jobWrapper.getJob()) {
}, workspace.getFilePath());
}, workspace != null ? workspace.getFilePath() : null);

jobWrapper.updateNextBuildNumber(nextBuildNumber);
return gitClient;
@@ -434,7 +434,7 @@ public void testDoFillValueItems_listBranches() throws Exception {
}

@Test
public void tesListBranchesWrongBrancheFilter() throws Exception {
public void tesListBranchesWrongBranchFilter() throws Exception {
project = jenkins.createFreeStyleProject("testListTags");
project.getBuildersList().add(new Shell("echo test"));
setupGit();
@@ -496,6 +496,26 @@ public void testSortAscendingTag() throws Exception {
assertEquals("0.1", items.get(0).value);
}

@Test
public void testWrongTagFilter() throws Exception {
project = jenkins.createFreeStyleProject("testListTags");
project.getBuildersList().add(new Shell("echo test"));
setupGit();

GitParameterDefinition def = new GitParameterDefinition("testName",
"PT_TAG",
"testDefaultValue",
"testDescription",
null,
".*",
"wrongTagFilter",
SortMode.ASCENDING, SelectedValue.NONE, null, false);
project.addProperty(new ParametersDefinitionProperty(def));

ListBoxModel items = def.getDescriptor().doFillValueItems(project, def.getName());
assertTrue(items.isEmpty());
}

@Test
public void testSortDescendingTag() throws Exception {
project = jenkins.createFreeStyleProject("testListTags");

0 comments on commit 734ac6b

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