Skip to content

Commit

Permalink
Small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
klimas7 committed Oct 22, 2018
1 parent 3cd3740 commit e53beab
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 59 deletions.
@@ -0,0 +1,42 @@
package net.uaznia.lukanus.hudson.plugins.gitparameter;

import java.util.regex.Pattern;

public class Consts {
public static final String DEFAULT_LIST_SIZE = "5";
public static final String DEFAULT_REMOTE = "origin";
public static final String REFS_TAGS_PATTERN = ".*refs/tags/";

public static final String PARAMETER_TYPE_TAG = "PT_TAG";
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_";
public static final String EMPTY_JOB_NAME = "EMPTY_JOB_NAME";

public static 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_PULL_REQUEST);
}

public static boolean isRevisionType(String type) {
return type.equalsIgnoreCase(PARAMETER_TYPE_REVISION);
}

public static boolean isBranchType(String type) {
return type.equalsIgnoreCase(PARAMETER_TYPE_BRANCH) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH);
}

public static boolean isTagType(String type) {
return type.equalsIgnoreCase(PARAMETER_TYPE_TAG) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH);
}

public static boolean isPullRequestType(String type) {
return type.equalsIgnoreCase(PARAMETER_TYPE_PULL_REQUEST);
}
}
Expand Up @@ -42,7 +42,6 @@
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.lib.ObjectId;
import org.eclipse.jgit.transport.RemoteConfig;
Expand All @@ -56,26 +55,12 @@
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;

import static net.uaznia.lukanus.hudson.plugins.gitparameter.Consts.*;
import static net.uaznia.lukanus.hudson.plugins.gitparameter.scms.SCMFactory.getGitSCMs;
import static org.apache.commons.lang3.StringUtils.isBlank;

public class GitParameterDefinition extends ParameterDefinition implements Comparable<GitParameterDefinition> {
private static final long serialVersionUID = 9157832967140868122L;

private static final String DEFAULT_LIST_SIZE = "5";
private static final String DEFAULT_REMOTE = "origin";
private static final String REFS_TAGS_PATTERN = ".*refs/tags/";

public static final String PARAMETER_TYPE_TAG = "PT_TAG";
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_";
public static final String EMPTY_JOB_NAME = "EMPTY_JOB_NAME";
private static final Logger LOGGER = Logger.getLogger(GitParameterDefinition.class.getName());

private final UUID uuid;
Expand Down Expand Up @@ -190,12 +175,6 @@ 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_PULL_REQUEST);
}

public String getBranch() {
return this.branch;
}
Expand Down Expand Up @@ -306,23 +285,23 @@ public Map<String, String> generateContents(JobWrapper jobWrapper, List<GitSCM>
continue;
}

if (isTagType()) {
if (isTagType(type)) {
Set<String> tagSet = getTag(gitClient, gitUrl);
sortAndPutToParam(tagSet, paramList);
}

if (isBranchType()) {
if (isBranchType(type)) {
Set<String> branchSet = getBranch(gitClient, gitUrl, repository.getName());
sortAndPutToParam(branchSet, paramList);
}


if (isPullRequestType()) {
if (isPullRequestType(type)) {
Set<String> pullRequestSet = getPullRequest(gitClient, gitUrl);
sortAndPutToParam(pullRequestSet, paramList);
}

if (isRevisionType()) {
if (isRevisionType(type)) {
synchronized (GitParameterDefinition.class) {
getRevision(jobWrapper, git, paramList, environment, repository, remoteURL);
}
Expand Down Expand Up @@ -358,22 +337,6 @@ private boolean notMatchUseRepository(String gitUrl) {
return !repositoryNamePattern.matcher(gitUrl).find();
}

private boolean isRevisionType() {
return type.equalsIgnoreCase(PARAMETER_TYPE_REVISION);
}

private boolean isBranchType() {
return type.equalsIgnoreCase(PARAMETER_TYPE_BRANCH) || type.equalsIgnoreCase(PARAMETER_TYPE_TAG_BRANCH);
}

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 All @@ -388,7 +351,7 @@ private Set<String> getTag(GitClient gitClient, String gitUrl) throws Interrupte
}

private Set<String> getBranch(GitClient gitClient, String gitUrl, String remoteName) throws Exception {
Set<String> branchSet = new HashSet<String>();
Set<String> branchSet = new HashSet<>();
Pattern branchFilterPattern = compileBranchFilterPattern();

Map<String, ObjectId> branches = gitClient.getRemoteReferences(gitUrl, null, true, false);
Expand All @@ -408,7 +371,7 @@ private Set<String> getBranch(GitClient gitClient, String gitUrl, String remoteN
}

private Set<String> getPullRequest(GitClient gitClient, String gitUrl) throws Exception {
Set<String> pullRequestSet = new HashSet<String>();
Set<String> pullRequestSet = new HashSet<>();
Map<String, ObjectId> remoteReferences = gitClient.getRemoteReferences(gitUrl, null, false, false);
for (String remoteReference : remoteReferences.keySet()) {
Matcher matcher = PULL_REQUEST_REFS_PATTERN.matcher(remoteReference);
Expand Down Expand Up @@ -475,7 +438,7 @@ private ArrayList<String> sort(Set<String> toSort) {
Collections.reverse(sorted);
}
} else {
sorted = new ArrayList<String>(toSort);
sorted = new ArrayList<>(toSort);
}
return sorted;
}
Expand Down
Expand Up @@ -38,14 +38,14 @@ public void testCreateValue_StaplerRequest() {

@Test
public void matchesWithBitbucketPullRequestRefs() {
Matcher matcher = GitParameterDefinition.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull-requests/186/from");
Matcher matcher = Consts.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 matcher = Consts.PULL_REQUEST_REFS_PATTERN.matcher("refs/pull/45/head");
matcher.find();
assertEquals(matcher.group(1), "45");
}
Expand Down
Expand Up @@ -343,7 +343,7 @@ public void testDoFillValueItems_listPullRequests() throws Exception {
project.getBuildersList().add(new Shell("echo test"));
setupGit();
GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_PULL_REQUEST,
Consts.PARAMETER_TYPE_PULL_REQUEST,
"master",
"testDescription",
"",
Expand Down Expand Up @@ -403,7 +403,7 @@ public void testGetDefaultValueWhenDefaultValueIsSet() throws Exception {
setupGit();
String testDefaultValue = "testDefaultValue";
GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_TAG,
Consts.PARAMETER_TYPE_TAG,
testDefaultValue,
"testDescription",
null,
Expand All @@ -422,7 +422,7 @@ public void testGetDefaultValueAsTop() throws Exception {
project.getBuildersList().add(new Shell("echo test"));
setupGit();
GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_TAG,
Consts.PARAMETER_TYPE_TAG,
null,
"testDescription",
null,
Expand All @@ -441,7 +441,7 @@ public void testGlobalVariableRepositoryUrl() throws Exception {
project.getBuildersList().add(new Shell("echo test"));
setupGit("$GIT_REPO_URL");
GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
Consts.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
Expand All @@ -462,7 +462,7 @@ public void testWorkflowJobWithCpsScmFlowDefinition() throws IOException, Interr
p.setDefinition(new CpsScmFlowDefinition(getGitSCM(), "jenkinsfile"));

GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_TAG,
Consts.PARAMETER_TYPE_TAG,
null,
"testDescription",
null,
Expand All @@ -486,7 +486,7 @@ public void testWorkflowJobWithCpsFlowDefinition() throws IOException, Interrupt

p.setDefinition(new CpsFlowDefinition(script, false));
GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_TAG,
Consts.PARAMETER_TYPE_TAG,
null,
"testDescription",
null,
Expand Down Expand Up @@ -514,7 +514,7 @@ public void testProxySCM() throws IOException, InterruptedException {
project.setScm(new ProxySCM("AnotherProject"));

GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_TAG,
Consts.PARAMETER_TYPE_TAG,
null,
"testDescription",
null,
Expand All @@ -534,7 +534,7 @@ public void testParameterDefinedRepositoryUrl() throws Exception {

StringParameterDefinition stringParameterDef = new StringParameterDefinition("GIT_REPO_URL", GIT_PARAMETER_REPOSITORY_URL, "Description");
GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
Consts.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
Expand All @@ -560,7 +560,7 @@ public void testMultiRepositoryInOneSCM() throws IOException, InterruptedExcepti
project.setScm(gitSCM);

GitParameterDefinition def = new GitParameterDefinition("name_git_parameter",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
Consts.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
Expand Down Expand Up @@ -588,7 +588,7 @@ public void testMultiSCM() throws IOException, InterruptedException {
project.setScm(multiSCM);

GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
Consts.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
Expand Down Expand Up @@ -617,7 +617,7 @@ public void testMultiSCM_forSubdirectoryForRepo() throws IOException, Interrupte
project.setScm(multiSCM);

GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
Consts.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
Expand All @@ -644,7 +644,7 @@ public void testMultiSCM_forSubdirectoryForTheSomeRepo() throws IOException, Int
project.setScm(multiSCM);

GitParameterDefinition def = new GitParameterDefinition("testName",
GitParameterDefinition.PARAMETER_TYPE_BRANCH,
Consts.PARAMETER_TYPE_BRANCH,
null,
"testDescription",
null,
Expand Down

0 comments on commit e53beab

Please sign in to comment.