Skip to content

Commit

Permalink
Added support for include patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
danechitoaie committed May 20, 2019
1 parent 25778dd commit 637ccde
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import org.jenkinsci.plugins.osfbuildersuiteforsfcc.dataimport.model.DataImportAction;
import org.jenkinsci.plugins.osfbuildersuiteforsfcc.dataimport.model.DataImportEnvAction;
import org.jenkinsci.plugins.osfbuildersuiteforsfcc.dataimport.repeatable.ExcludePattern;
import org.jenkinsci.plugins.osfbuildersuiteforsfcc.dataimport.repeatable.IncludePattern;
import org.jenkinsci.plugins.tokenmacro.MacroEvaluationException;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.*;
Expand Down Expand Up @@ -84,6 +85,7 @@ public class DataImportBuilder extends Builder implements SimpleBuildStep {
private String ocVersion;
private String archiveName;
private String sourcePath;
private List<IncludePattern> includePatterns;
private List<ExcludePattern> excludePatterns;
private String importStrategy;
private String tempDirectory;
Expand All @@ -97,6 +99,7 @@ public DataImportBuilder(
String ocVersion,
String archiveName,
String sourcePath,
List<IncludePattern> includePatterns,
List<ExcludePattern> excludePatterns,
String importStrategy,
String tempDirectory) {
Expand All @@ -108,6 +111,7 @@ public DataImportBuilder(
this.ocVersion = ocVersion;
this.archiveName = archiveName;
this.sourcePath = sourcePath;
this.includePatterns = includePatterns;
this.excludePatterns = excludePatterns;
this.importStrategy = importStrategy;
this.tempDirectory = tempDirectory;
Expand Down Expand Up @@ -190,6 +194,17 @@ public void setSourcePath(String sourcePath) {
this.sourcePath = sourcePath;
}

@SuppressWarnings("unused")
public List<IncludePattern> getIncludePatterns() {
return includePatterns;
}

@SuppressWarnings("unused")
@DataBoundSetter
public void setIncludePatterns(List<IncludePattern> includePatterns) {
this.includePatterns = includePatterns;
}

@SuppressWarnings("unused")
public List<ExcludePattern> getExcludePatterns() {
return excludePatterns;
Expand Down Expand Up @@ -315,7 +330,6 @@ public void perform(
}

DataImportResult dataImportResult = workspace.act(new DataImportCallable(
workspace,
listener,
expandedHostname,
bmCredentialsId,
Expand All @@ -327,6 +341,7 @@ public void perform(
ocVersion,
expandedArchiveName,
sourcePath,
includePatterns,
excludePatterns,
importStrategy,
tempDirectory,
Expand Down Expand Up @@ -543,7 +558,6 @@ private static class DataImportCallable extends MasterToSlaveFileCallable<DataIm

private static final long serialVersionUID = 1L;

private final FilePath workspace;
private final TaskListener listener;
private final String hostname;
private final String bmCredentialsId;
Expand All @@ -555,6 +569,7 @@ private static class DataImportCallable extends MasterToSlaveFileCallable<DataIm
private final String ocVersion;
private final String archiveName;
private final String sourcePath;
private final List<IncludePattern> includePatterns;
private final List<ExcludePattern> excludePatterns;
private final String importStrategy;
private final String tempDirectory;
Expand All @@ -564,7 +579,6 @@ private static class DataImportCallable extends MasterToSlaveFileCallable<DataIm

@SuppressWarnings("WeakerAccess")
public DataImportCallable(
FilePath workspace,
TaskListener listener,
String hostname,
String bmCredentialsId,
Expand All @@ -576,14 +590,14 @@ public DataImportCallable(
String ocVersion,
String archiveName,
String sourcePath,
List<IncludePattern> includePatterns,
List<ExcludePattern> excludePatterns,
String importStrategy,
String tempDirectory,
HTTPProxyCredentials httpProxyCredentials,
Boolean disableSSLValidation,
List<String> previousDataFingerprints) {

this.workspace = workspace;
this.listener = listener;
this.hostname = hostname;
this.bmCredentialsId = bmCredentialsId;
Expand All @@ -595,6 +609,7 @@ public DataImportCallable(
this.ocVersion = ocVersion;
this.archiveName = archiveName;
this.sourcePath = sourcePath;
this.includePatterns = includePatterns;
this.excludePatterns = excludePatterns;
this.importStrategy = importStrategy;
this.tempDirectory = tempDirectory;
Expand Down Expand Up @@ -813,6 +828,16 @@ public DataImportResult invoke(File dir, VirtualChannel channel) throws IOExcept
);
}

List<MatchPattern> includePatternsList = new ArrayList<>();
if (includePatterns != null) {
includePatternsList.addAll(includePatterns.stream()
.map(IncludePattern::getIncludePattern)
.filter(StringUtils::isNotEmpty)
.map((p) -> MatchPattern.fromString("%ant[" + File.separator + p + "]"))
.collect(Collectors.toList())
);
}

List<MatchPattern> excludePatternsList = new ArrayList<>();
if (excludePatterns != null) {
excludePatternsList.addAll(excludePatterns.stream()
Expand All @@ -827,9 +852,23 @@ public DataImportResult invoke(File dir, VirtualChannel channel) throws IOExcept
List<String> currentDataZipFiles = new ArrayList<>();

ZipUtil.pack(dataSrc, dataZip, (path) -> {
boolean excludeFile = excludePatternsList.stream().anyMatch(
(pattern) -> pattern.matchPath(File.separator + path, true)
);
boolean includeFile = true;
if (!includePatternsList.isEmpty()) {
includeFile = includePatternsList.stream().anyMatch(
(pattern) -> pattern.matchPath(File.separator + path, true)
);
}

if (!includeFile) {
return null;
}

boolean excludeFile = false;
if (!excludePatternsList.isEmpty()) {
excludeFile = excludePatternsList.stream().anyMatch(
(pattern) -> pattern.matchPath(File.separator + path, true)
);
}

if (excludeFile) {
return null;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.jenkinsci.plugins.osfbuildersuiteforsfcc.dataimport.repeatable;

import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Descriptor;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serializable;

public class IncludePattern implements Serializable, Describable<IncludePattern> {

private final String includePattern;

@DataBoundConstructor
public IncludePattern(String includePattern) {
this.includePattern = includePattern;
}

public String getIncludePattern() {
return includePattern;
}

@Override
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) Jenkins.get().getDescriptor(getClass());
}

@Extension
public static final class DescriptorImpl extends Descriptor<IncludePattern> {

@Override
public String getDisplayName() {
return "OSF Builder Suite For Salesforce Commerce Cloud :: Data Import (IncludePattern)";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,17 @@
<f:textbox />
</f:entry>

<f:entry
title="Include Patterns"
help="/plugin/osf-builder-suite-for-sfcc-data-import/help/projectConfig-includePatterns.html">

<f:repeatableProperty field="includePatterns">
<div align="right">
<f:repeatableDeleteButton />
</div>
</f:repeatableProperty>
</f:entry>

<f:entry
title="Exclude Patterns"
help="/plugin/osf-builder-suite-for-sfcc-data-import/help/projectConfig-excludePatterns.html">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:textbox field="includePattern" />
</j:jelly>
57 changes: 57 additions & 0 deletions src/main/webapp/help/projectConfig-includePatterns.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<div>
List of patterns to be included. If a path matches any of the patterns in this list then it will be added to
the build.
<br>
The pattern needs to be relative to the source path defined above.
<br>
<br>
When a path is matched against a pattern, the following special characters can be used:
<ul>
<li>
<tt>?</tt>
<br>
Matches one character (any character except path separators)
</li>
<li>
<tt>*</tt>
<br>
Matches zero or more characters (not including path separators)
</li>
<li>
<tt>**</tt>
<br>
Matches zero or more path segments
</li>
</ul>

Examples:
<ul>
<li>
<tt>**/*.js</tt>
<br>
Matches all .js files/dirs in a directory tree
</li>
<li>
<tt>node_modules/**</tt>
<br>
Matches the node_modules folder and all its contents
</li>
<li>
<tt>test/a??.js</tt>
<br>
Matches all files/dirs which start with an <tt>a</tt>, then two more characters and then <tt>.js</tt>,
in a directory called test
</li>
<li>
<tt>**</tt>
<br>
Matches everything in a directory tree
</li>
<li>
<tt>**/test/**/XYZ*</tt>
<br>
Matches all files/dirs which start with <tt>XYZ</tt> and where there is a parent directory called
<tt>test</tt> (e.g. <tt>abc/test/def/ghi/XYZ123</tt>)
</li>
</ul>
</div>
21 changes: 21 additions & 0 deletions src/main/webapp/help/projectConfig-sourcePath.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,25 @@
<br>
<br>
For example: &quot;<tt>scm/my-git-repo/metadata</tt>&quot;
<br>
<br>
<ul>
<li>
If no include and no exclude patterns are setup then all files in the source path defined here
will be included
</li>
<li>
If include patterns are defined while no exclude patterns are defined then only the files matching the
include patterns will be included
</li>
<li>
If include patterns are not defined while exclude patterns are defined then all files in the source
path defined here will be included with the exception of those matching the exclude patterns
</li>
<li>
If both include patterns and exclude patterns are defined then only the files matching the
include patterns and not matching the exclude patterns will be included
</li>

</ul>
</div>

0 comments on commit 637ccde

Please sign in to comment.