Skip to content

Commit

Permalink
Merge PR #37
Browse files Browse the repository at this point in the history
* Refactored some code in CheckForChanges and tried to sanitize the paths ( #JENKINS-41018 ).

* Processed the comments from @Casz and made more use of the Apache Commons library (related to #JENKINS-41018 ).
  • Loading branch information
itavero authored and Casz committed Jan 13, 2017
1 parent c2ebacc commit 81d07fe
Showing 1 changed file with 53 additions and 113 deletions.
166 changes: 53 additions & 113 deletions src/main/java/hudson/plugins/accurev/CheckForChanges.java
Expand Up @@ -5,6 +5,8 @@
import hudson.model.TaskListener;
import hudson.plugins.accurev.AccurevSCM.AccurevServer;
import hudson.plugins.accurev.cmd.History;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.io.IOException;
import java.util.*;
Expand Down Expand Up @@ -55,24 +57,9 @@ public static boolean checkStreamForChanges(AccurevServer server,
"Checking transactions of type " + Arrays.asList(validTransactionTypes) + //
" in stream [" + stream + "]");

Iterator<String> affectedPaths;
Collection<String> serverPaths;
Set<String> pollingFilters = getListOfPollingFilters(filterForPollSCM, subPath);

final String FFPSCM_DELIM = ",";

Collection<String> Filter_For_Poll_SCM = null;

if (filterForPollSCM != null && !(filterForPollSCM.isEmpty())) {
String FFPSCM_LIST = filterForPollSCM;
FFPSCM_LIST = FFPSCM_LIST.replace(", ", ",");
Filter_For_Poll_SCM = new ArrayList<>(Arrays.asList(FFPSCM_LIST.split(FFPSCM_DELIM)));
} else {
if (subPath != null && !(subPath.isEmpty())) {
String FFPSCM_LIST = subPath;
FFPSCM_LIST = FFPSCM_LIST.replace(", ", ",");
Filter_For_Poll_SCM = new ArrayList<>(Arrays.asList(FFPSCM_LIST.split(FFPSCM_DELIM)));
}
}
for (final String transactionType : validTransactionTypes) {
try {
final AccurevTransaction tempTransaction = History.getLatestTransaction(scm, server, accurevEnv, workspace,
Expand All @@ -86,45 +73,9 @@ public static boolean checkStreamForChanges(AccurevServer server,
serverPaths = tempTransaction.getAffectedPaths();
if (tempTransaction.getAffectedPaths().size() > 0) {
listener.getLogger().println("This transaction seems to have happened after the latest build!!!");
if (filterForPollSCM != null && !(filterForPollSCM.isEmpty())) {
boolean buildRequired = false;
for (String filterPath : Filter_For_Poll_SCM) {
affectedPaths = serverPaths.iterator();
while (affectedPaths.hasNext()) {

if (affectedPaths.next().contains(filterPath)) {
buildRequired = true;
break;
}

}
}
if (buildRequired) {

} else {
return false;
}
} else {
if (subPath != null && !(subPath.isEmpty())) {

boolean buildRequired = false;
for (String filterPath : Filter_For_Poll_SCM) {
affectedPaths = serverPaths.iterator();
while (affectedPaths.hasNext()) {

if (affectedPaths.next().contains(filterPath)) {
buildRequired = true;
break;
}

}
}
if (buildRequired) {

} else {
return false;
}
}
if (!changesMatchFilter(serverPaths, pollingFilters)) {
// Continue to next transaction (that may have a match)
continue;
}
}
}
Expand All @@ -133,8 +84,6 @@ public static boolean checkStreamForChanges(AccurevServer server,
//log last transaction information if retrieved
if (latestCodeChangeTransaction.getDate().equals(AccurevSCM.NO_TRANS_DATE)) {
listener.getLogger().println("No last transaction found.");
} else {

}
if (buildDate.before(latestCodeChangeTransaction.getDate())) {
listener.getLogger().println("Last valid trans " + latestCodeChangeTransaction);
Expand All @@ -152,8 +101,6 @@ public static boolean checkStreamForChanges(AccurevServer server,
}
}
return false;


}

/**
Expand Down Expand Up @@ -202,25 +149,9 @@ public static boolean checkStreamForChanges(AccurevServer server,
}
boolean isTransLatestThanBuild = false;


Iterator<String> affectedPaths;
Collection<String> serverPaths;
Set<String> pollingFilters = getListOfPollingFilters(filterForPollSCM, subPath);

final String FFPSCM_DELIM = ",";

Collection<String> Filter_For_Poll_SCM = null;

if (filterForPollSCM != null && !(filterForPollSCM.isEmpty())) {
String FFPSCM_LIST = filterForPollSCM;
FFPSCM_LIST = FFPSCM_LIST.replace(", ", ",");
Filter_For_Poll_SCM = new ArrayList<>(Arrays.asList(FFPSCM_LIST.split(FFPSCM_DELIM)));
} else {
if (subPath != null && !(subPath.isEmpty())) {
String FFPSCM_LIST = subPath;
FFPSCM_LIST = FFPSCM_LIST.replace(", ", ",");
Filter_For_Poll_SCM = new ArrayList<>(Arrays.asList(FFPSCM_LIST.split(FFPSCM_DELIM)));
}
}
for (final String transactionType : validTransactionTypes) {
try {
final AccurevTransaction tempTransaction = History.getLatestTransaction(scm, server, accurevEnv, workspace,
Expand All @@ -233,43 +164,9 @@ public static boolean checkStreamForChanges(AccurevServer server,
//check the affected
serverPaths = tempTransaction.getAffectedPaths();
if (tempTransaction.getAffectedPaths().size() > 0) {
if (filterForPollSCM != null && !(filterForPollSCM.isEmpty())) {
boolean buildRequired = false;
for (String filterPath : Filter_For_Poll_SCM) {
affectedPaths = serverPaths.iterator();
while (affectedPaths.hasNext()) {
if (affectedPaths.next().contains(filterPath)) {
buildRequired = true;
break;
}
}
}
if (buildRequired) {

} else {
return false;
}
} else {
if (subPath != null && !(subPath.isEmpty())) {

boolean buildRequired = false;
for (String filterPath : Filter_For_Poll_SCM) {
affectedPaths = serverPaths.iterator();
while (affectedPaths.hasNext()) {

if (affectedPaths.next().contains(filterPath)) {
buildRequired = true;
break;
}

}
}
if (buildRequired) {

} else {
return false;
}
}
if (!changesMatchFilter(serverPaths, pollingFilters)) {
// Continue to next transaction (that may have a match)
continue;
}
}
}
Expand All @@ -295,4 +192,47 @@ public static boolean checkStreamForChanges(AccurevServer server,
}
return isTransLatestThanBuild;
}

private static boolean changesMatchFilter(Collection<String> serverPaths, Collection<String> filters) {
if (CollectionUtils.isEmpty(filters)) {
// No filters, so always a match.
return true;
}

final String[] filterArray = filters.toArray(new String[filters.size()]);

for (String path : serverPaths) {
if (StringUtils.indexOfAny(sanitizeSlashes(path), filterArray) > -1) {
// Path contains one of the filters
return true;
}
}

return false;
}

private static Set<String> getListOfPollingFilters(String filterForPollSCM, String subPath) {
if (StringUtils.isNotBlank(filterForPollSCM)) {
return splitAndSanitizeFilters(filterForPollSCM);
}

return splitAndSanitizeFilters(subPath);
}

private static Set<String> splitAndSanitizeFilters(String input) {
if (StringUtils.isBlank(input)) {
return null;
}

final char DELIMITER = ',';
final String STRIP_CHARS = " \t\n\r/";
String[] filters = StringUtils.split(sanitizeSlashes(input), DELIMITER);
filters = StringUtils.stripAll(filters, STRIP_CHARS);

return new HashSet<>(Arrays.asList(filters));
}

private static String sanitizeSlashes(String input) {
return input.replace('\\', '/');
}
}

0 comments on commit 81d07fe

Please sign in to comment.