Skip to content

Commit

Permalink
JENKINS-53361 Copy files using a regular expression (#13)
Browse files Browse the repository at this point in the history
* JENKINS-53361 Copy files using a regular expression
* Address pull request comments
* Address pull request comments
  • Loading branch information
nrayapati authored Jan 8, 2019
1 parent 169e8f8 commit 020f434
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
== 1.2.0 (Unreleased)

* https://issues.jenkins-ci.org/browse/JENKINS-55194[JENKINS-55194] - Add logLevel to support debugging and also reduce default excessive logging.
* https://issues.jenkins-ci.org/browse/JENKINS-53361[JENKINS-53361] - Copy files using a regular expression.
* Code cleanup, update documentation.

== 1.1.1
Expand Down
16 changes: 16 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,14 @@ Put a file or directory into the remote host.
|String, *Mandatory*
|file or directory path on the remote node.

|filterBy
|String, *Optional*, Defaults to `name`.
|Put files by a file filter. Possible values are params on the java File object.

|filterRegex
|String, *Optional*.
|Put files by a file regex (Groovy syntax). Example: /\.xml$/ - Puts all xml files.

|failOnError
|boolean, default: `true`.
|If this is `false`, no job failure would occur though there is an error while running the command.
Expand Down Expand Up @@ -335,6 +343,14 @@ Get a file or directory from the remote host.
|String, *Mandatory*
|file or directory path on current workspace.

|filterBy
|String, *Optional*, Defaults to `name`.
|Get files by a file filter. Possible values are params on the java File object.

|filterRegex
|String, *Optional*.
|Get files by a file regex (Groovy syntax). Example: /\.xml$/ - Gets all xml files.

|failOnError
|boolean, default: `true`.
|If this is `false`, no job failure would occur though there is an error while running the command.
Expand Down
18 changes: 14 additions & 4 deletions src/main/groovy/org/jenkinsci/plugins/sshsteps/SSHService.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,20 @@ class SSHService implements Serializable {
*
* @param from location to put file to.
* @param into location to put file from.
* @param filterBy put files by a file filter.
* @param filterRegex filter regex.
* @return response from ssh run.
*/
def put(String from, String into) {
def put(String from, String into, String filterBy, String filterRegex) {
logger.println("Sending a file/directory to $remote.name[$remote.host]: from: $from into: $into")
registerLogHandler()
defineRemote(remote)
ssh.run {
session(ssh.remotes."$remote.name") {
put from: from, into: into
if (filterBy && filterRegex)
put from: from, into: into, filter: { it."$filterBy" =~ filterRegex }
else
put from: from, into: into
}
}
}
Expand All @@ -224,15 +229,20 @@ class SSHService implements Serializable {
*
* @param from location to get file from.
* @param into location to get file into.
* @param filterBy get files by a file filter.
* @param filterRegex filter regex.
* @return response from ssh run.
*/
def get(String from, String into) {
def get(String from, String into, String filterBy, String filterRegex) {
logger.println("Receiving a file/directory from $remote.name[$remote.host]: from: $from into: $into")
registerLogHandler()
defineRemote(remote)
ssh.run {
session(ssh.remotes."$remote.name") {
get from: from, into: into
if (filterBy && filterRegex)
get from: from, into: into, filter: { it."$filterBy" =~ filterRegex }
else
get from: from, into: into
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/org/jenkinsci/plugins/sshsteps/steps/GetStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,26 @@ public class GetStep extends BasicSSHStep {
@Getter
private final String into;

@Getter
private final String filterRegex;

@Getter
private String filterBy;

@Getter
@Setter
@DataBoundSetter
private boolean override = false;

@DataBoundConstructor
public GetStep(String from, String into) {
public GetStep(String from, String into, String filterBy, String filterRegex) {
this.from = from;
this.into = into;
this.filterRegex = filterRegex;
this.filterBy = filterBy;
if (Util.fixEmpty(filterRegex) != null && Util.fixEmpty(filterBy) == null) {
this.filterBy = "name";
}
}

@Override
Expand Down Expand Up @@ -106,7 +117,8 @@ public GetCallable(GetStep step, TaskListener listener, String into) {

@Override
public Object execute() {
return getService().get(((GetStep) getStep()).getFrom(), into);
final GetStep step = (GetStep) getStep();
return getService().get(step.getFrom(), into, step.getFilterBy(), step.getFilterRegex());
}
}
}
Expand Down
16 changes: 14 additions & 2 deletions src/main/java/org/jenkinsci/plugins/sshsteps/steps/PutStep.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,21 @@ public class PutStep extends BasicSSHStep {
@Getter
private final String into;

@Getter
private final String filterRegex;

@Getter
private String filterBy;

@DataBoundConstructor
public PutStep(String from, String into) {
public PutStep(String from, String into, String filterBy, String filterRegex) {
this.from = from;
this.into = into;
this.filterRegex = filterRegex;
this.filterBy = filterBy;
if (Util.fixEmpty(filterRegex) != null && Util.fixEmpty(filterBy) == null) {
this.filterBy = "name";
}
}

@Override
Expand Down Expand Up @@ -98,7 +109,8 @@ public PutCallable(PutStep step, TaskListener listener, String from) {

@Override
public Object execute() {
return getService().put(from, ((PutStep) getStep()).getInto());
final PutStep step = (PutStep) getStep();
return getService().put(from, step.getInto(), step.getFilterBy(), step.getFilterRegex());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
public class GetStepTest {

final String path = "test.sh";
final String filterBy = "";
final String filterRegex = "";

@Mock
TaskListener taskListenerMock;
Expand Down Expand Up @@ -82,7 +84,7 @@ public void setup() throws IOException, InterruptedException {

@Test
public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
final GetStep step = new GetStep("", path);
final GetStep step = new GetStep("", path, filterBy, filterRegex);
stepExecution = new GetStep.Execution(step, contextMock);

// Execute and assert Test.
Expand All @@ -95,7 +97,7 @@ public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {

@Test
public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
final GetStep step = new GetStep(path, "");
final GetStep step = new GetStep(path, "", filterBy, filterRegex);
step.setOverride(true);
stepExecution = new GetStep.Execution(step, contextMock);

Expand All @@ -109,7 +111,7 @@ public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {

@Test
public void testSuccessfulExecuteScript() throws Exception {
final GetStep step = new GetStep(path, path);
final GetStep step = new GetStep(path, path, filterBy, filterRegex);
step.setOverride(true);

// Since SSHService is a mock, it is not validating remote.
Expand All @@ -119,6 +121,6 @@ public void testSuccessfulExecuteScript() throws Exception {
stepExecution.run();

// Assert Test
verify(sshServiceMock, times(1)).get(path, path);
verify(sshServiceMock, times(1)).get(path, path, filterBy, filterRegex);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
public class PutStepTest {

final String path = "test.sh";
final String filterBy = "";
final String filterRegex = "";

@Mock
TaskListener taskListenerMock;
Expand Down Expand Up @@ -82,7 +84,7 @@ public void setup() throws IOException, InterruptedException {

@Test
public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {
final PutStep step = new PutStep("", path);
final PutStep step = new PutStep("", path, filterBy, filterRegex);
stepExecution = new PutStep.Execution(step, contextMock);

// Execute and assert Test.
Expand All @@ -95,7 +97,7 @@ public void testWithEmptyFromThrowsIllegalArgumentException() throws Exception {

@Test
public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {
final PutStep step = new PutStep(path, "");
final PutStep step = new PutStep(path, "", filterBy, filterRegex);
stepExecution = new PutStep.Execution(step, contextMock);

// Execute and assert Test.
Expand All @@ -108,7 +110,7 @@ public void testWithEmptyIntoThrowsIllegalArgumentException() throws Exception {

@Test
public void testSuccessfulPut() throws Exception {
final PutStep step = new PutStep(path, path);
final PutStep step = new PutStep(path, path, filterBy, filterRegex);

// Since SSHService is a mock, it is not validating remote.
stepExecution = new PutStep.Execution(step, contextMock);
Expand All @@ -117,6 +119,6 @@ public void testSuccessfulPut() throws Exception {
stepExecution.run();

// Assert Test
verify(sshServiceMock, times(1)).put(path, path);
verify(sshServiceMock, times(1)).put(path, path, filterBy, filterRegex);
}
}

0 comments on commit 020f434

Please sign in to comment.