Skip to content

Commit

Permalink
Merge pull request #48 from gauee/master
Browse files Browse the repository at this point in the history
  • Loading branch information
klimas7 committed Aug 7, 2017
2 parents 306c612 + 90f3f43 commit 5b14c82
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
Expand Up @@ -60,6 +60,9 @@ public class GitParameterDefinition extends ParameterDefinition implements Compa
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";
public static final String PARAMETER_TYPE_PULL_REQUEST = "PT_PULL_REQUEST";

public static final Pattern PULL_REQUEST_REFS_PATTERN = Pattern.compile("refs/pull.*/(\\d+)/[from|head]");

public static final String TEMPORARY_DIRECTORY_PREFIX = "git_parameter_";
private static final Logger LOGGER = Logger.getLogger(GitParameterDefinition.class.getName());
Expand Down Expand Up @@ -170,7 +173,8 @@ public void setType(String type) {

private boolean isParameterTypeCorrect(String type) {
return type.equals(PARAMETER_TYPE_TAG) || type.equals(PARAMETER_TYPE_REVISION)
|| type.equals(PARAMETER_TYPE_BRANCH) || type.equals(PARAMETER_TYPE_TAG_BRANCH);
|| type.equals(PARAMETER_TYPE_BRANCH) || type.equals(PARAMETER_TYPE_TAG_BRANCH)
|| type.equals(PARAMETER_TYPE_PULL_REQUEST);
}

public String getBranch() {
Expand Down Expand Up @@ -283,6 +287,11 @@ public Map<String, String> generateContents(JobWrapper jobWrapper, GitSCM git) {
if (isRevisionType()) {
getRevision(jobWrapper, git, paramList, environment, repository, remoteURL);
}

if(isPullRequestType()){
Set<String> pullRequestSet = getPullRequest(gitClient, gitUrl);
sortAndPutToParam(pullRequestSet, paramList);
}
}
break;
}
Expand All @@ -296,7 +305,6 @@ public Map<String, String> generateContents(JobWrapper jobWrapper, GitSCM git) {
return paramList;
}


private boolean isRevisionType() {
return type.equalsIgnoreCase(PARAMETER_TYPE_REVISION);
}
Expand All @@ -309,6 +317,10 @@ private boolean isTagType() {
return type.equalsIgnoreCase(PARAMETER_TYPE_TAG) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH);
}

private boolean isPullRequestType() {
return type.equalsIgnoreCase(PARAMETER_TYPE_PULL_REQUEST);
}

private Set<String> getTag(GitClient gitClient, String gitUrl) throws InterruptedException {
Set<String> tagSet = new HashSet<String>();
try {
Expand Down Expand Up @@ -342,6 +354,18 @@ private Set<String> getBranch(GitClient gitClient, String gitUrl) throws Interru
return branchSet;
}

private Set<String> getPullRequest(GitClient gitClient, String gitUrl) throws InterruptedException {
Set<String> pullRequestSet = new HashSet<String>();
Map<String, ObjectId> remoteReferences = gitClient.getRemoteReferences(gitUrl, null, false, false);
for (String remoteReference : remoteReferences.keySet()) {
Matcher matcher = PULL_REQUEST_REFS_PATTERN.matcher(remoteReference);
if (matcher.find()) {
pullRequestSet.add(matcher.group(1));
}
}
return pullRequestSet;
}

private Pattern compileBranchFilterPattern() {
Pattern branchFilterPattern;
try {
Expand Down
Expand Up @@ -41,6 +41,14 @@
<option value="PT_REVISION">${%parameter.revision}</option>
</j:otherwise>
</j:choose>
<j:choose>
<j:when test="${instance.type eq 'PT_PULL_REQUEST'}">
<option value="PT_PULL_REQUEST" selected="selected">${%parameter.pull.request}</option>
</j:when>
<j:otherwise>
<option value="PT_PULL_REQUEST">${%parameter.pull.request}</option>
</j:otherwise>
</j:choose>
</select>
</f:entry>

Expand Down
Expand Up @@ -7,6 +7,7 @@ parameter.branch=Branch
parameter.branch.filter=Branch Filter
parameter.branch.or.tag=Branch or Tag
parameter.revision=Revision
parameter.pull.request=Pull Request
parameter.sort.mode=Sort Mode
parameter.default.value=Default Value
parameter.selected.value=Selected Value
Expand Down
Expand Up @@ -7,6 +7,7 @@ parameter.branch=Branch
parameter.branch.filter=Branch Filtr
parameter.branch.or.tag=Branch lub Tag
parameter.revision=Revision
parameter.pull.request=Pull Request
parameter.sort.mode=Rodzaj sortowania
parameter.default.value=Domy\u015Blna warto\u015B\u0107
parameter.selected.value=Zaznaczana warto\u015B\u0107
Expand Down
Expand Up @@ -16,6 +16,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;

import hudson.EnvVars;
import hudson.model.FreeStyleBuild;
Expand Down Expand Up @@ -71,6 +72,20 @@ public void testCreateValue_StaplerRequest() {
assertEquals(result, new GitParameterValue(NAME, "defaultValue"));
}

@Test
public void matchesWithBitbucketPullRequestRefs(){
Matcher matcher = GitParameterDefinition.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull-requests/186/from");
matcher.find();
assertEquals(matcher.group(1),"186");
}

@Test
public void matchesWithGithubPullRequestRefs(){
Matcher matcher = GitParameterDefinition.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull/45/head");
matcher.find();
assertEquals(matcher.group(1),"45");
}

@Test
public void testCreateValue_StaplerRequest_ValueInRequest() {
GitParameterDefinition instance = new GitParameterDefinition(NAME, PT_REVISION, "defaultValue", "description", "branch", ".*", "*", SortMode.NONE, SelectedValue.NONE, null, false);
Expand Down Expand Up @@ -604,6 +619,28 @@ public void testDoFillValueItems_listRevisionsWithBranch() throws Exception {
assertTrue(isListBoxItem(items, "00a8385c"));
}

@Test
public void testDoFillValueItems_listPullRequests() throws Exception {
project = jenkins.createFreeStyleProject("testListPullRequests");
project.getBuildersList().add(new Shell("echo test"));
setupGit();
GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_PULL_REQUEST,
"master",
"testDescription",
"",
".*",
"*",
SortMode.NONE, SelectedValue.NONE, null, false);

project.addProperty(new ParametersDefinitionProperty(def));

FreeStyleBuild build = project.scheduleBuild2(0).get();
ListBoxModel items = def.getDescriptor().doFillValueItems(project, def.getName());
assertTrue(isListBoxItem(items, "41"));
assertTrue(isListBoxItem(items, "44"));
}

@Test
public void testSearchInFolders() throws Exception {
MockFolder folder = jenkins.createFolder("folder");
Expand Down

0 comments on commit 5b14c82

Please sign in to comment.