Skip to content

Commit bcd8080

Browse files
author
Hugo Braz
committed
XRAYJENKINS-52 features files are now being upload in a single Zip file
1 parent 4e4830f commit bcd8080

File tree

3 files changed

+122
-39
lines changed

3 files changed

+122
-39
lines changed

src/main/java/com/xpandit/plugins/xrayjenkins/Utils/FileUtils.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
package com.xpandit.plugins.xrayjenkins.Utils;
99

10+
import com.google.common.collect.Sets;
1011
import com.xpandit.plugins.xrayjenkins.exceptions.XrayJenkinsGenericException;
1112
import hudson.FilePath;
1213
import hudson.model.TaskListener;
@@ -15,6 +16,8 @@
1516
import java.util.Arrays;
1617
import java.util.LinkedList;
1718
import java.util.List;
19+
import java.util.Set;
20+
import org.apache.commons.collections.CollectionUtils;
1821
import org.apache.commons.lang3.StringUtils;
1922
import java.util.Collections;
2023

@@ -53,6 +56,24 @@ public static List<FilePath> getFeatureFilesFromWorkspace(FilePath workspace,
5356
}
5457
return paths;
5558
}
59+
60+
public static Set<String> getFeatureFileNamesFromWorkspace(FilePath workspace,
61+
String path,
62+
TaskListener listener) throws IOException, InterruptedException {
63+
final List<FilePath> filePaths = getFeatureFilesFromWorkspace(workspace, path, listener);
64+
final Set<String> fileNames = Sets.newHashSet();
65+
66+
if (CollectionUtils.isNotEmpty(filePaths)) {
67+
for (FilePath fp : filePaths) {
68+
String remoteFileName = fp.getRemote();
69+
if (StringUtils.isNotBlank(remoteFileName)) {
70+
fileNames.add(remoteFileName);
71+
}
72+
}
73+
}
74+
75+
return Collections.unmodifiableSet(fileNames);
76+
}
5677

5778
private static String getErrors(FilePath workspace,
5879
String path,

src/main/java/com/xpandit/plugins/xrayjenkins/task/XrayImportFeatureBuilder.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import com.xpandit.plugins.xrayjenkins.model.HostingType;
1717
import com.xpandit.plugins.xrayjenkins.model.ServerConfiguration;
1818
import com.xpandit.plugins.xrayjenkins.model.XrayInstance;
19+
import com.xpandit.plugins.xrayjenkins.task.filefilters.OnlyFeatureFilesInPathFilter;
1920
import com.xpandit.xray.exception.XrayClientCoreGenericException;
2021
import com.xpandit.xray.model.FileStream;
2122
import com.xpandit.xray.model.UploadResult;
@@ -34,9 +35,12 @@
3435
import hudson.tasks.Builder;
3536
import hudson.util.FormValidation;
3637
import hudson.util.ListBoxModel;
38+
import java.io.File;
39+
import java.io.FileFilter;
3740
import java.io.IOException;
3841
import java.util.Date;
3942
import java.util.List;
43+
import java.util.Set;
4044
import javax.annotation.Nonnull;
4145
import jenkins.tasks.SimpleBuildStep;
4246
import org.apache.commons.lang3.StringUtils;
@@ -49,7 +53,9 @@
4953
/**
5054
* This class is responsible for performing the Xray: Cucumber Features Import Task
5155
*/
52-
public class XrayImportFeatureBuilder extends Builder implements SimpleBuildStep{
56+
public class XrayImportFeatureBuilder extends Builder implements SimpleBuildStep {
57+
58+
private static final String TMP_ZIP_FILENAME = "xray_cucumber_features.zip";
5359

5460
private String serverInstance;
5561
private String folderPath;
@@ -142,26 +148,22 @@ public void perform(@Nonnull Run<?, ?> run,
142148

143149
}
144150

145-
private void processImport(FilePath workspace,
146-
XrayTestImporter client,
147-
TaskListener listener) throws IOException, InterruptedException {
148-
List<FilePath> paths;
149-
try{
150-
paths = FileUtils.getFeatureFilesFromWorkspace(workspace, this.folderPath, listener);
151-
} catch (IOException | InterruptedException e){
152-
listener.getLogger().println("An error occurred while getting the feature files");
153-
throw e;
154-
}
151+
private void processImport(final FilePath workspace,
152+
final XrayTestImporter client,
153+
final TaskListener listener) throws IOException, InterruptedException {
154+
155155
try{
156-
for(FilePath fp : paths){
157-
if(isApplicableAsModifiedFile(fp)){
158-
FileStream f = new com.xpandit.xray.model.FileStream(fp.getName(),
159-
fp.read(),
160-
ContentType.APPLICATION_JSON);
161-
UploadResult up = client.importFeatures(this.projectKey, f);
162-
listener.getLogger().println(up.getMessage());
163-
}
164-
}
156+
final Set<String> validFilePath = FileUtils.getFeatureFileNamesFromWorkspace(workspace, this.folderPath, listener);
157+
final FilePath zipFile = createZipFile(workspace);
158+
159+
// Create Zip file in the workspace's root folder
160+
workspace.zip(zipFile.write(), new OnlyFeatureFilesInPathFilter(validFilePath, lastModified));
161+
162+
// Uploads the Zip file to the Jira instance
163+
uploadZipFile(client, listener, zipFile);
164+
165+
// Deletes the Zip File
166+
deleteFile(zipFile, listener);
165167

166168
} catch(XrayClientCoreGenericException e){
167169
listener.error(e.getMessage());
@@ -171,28 +173,28 @@ private void processImport(FilePath workspace,
171173
}
172174

173175
}
174-
175-
private boolean isApplicableAsModifiedFile(FilePath filePath) throws InterruptedException, IOException{
176-
if(StringUtils.isBlank(lastModified)){
177-
//the modified field is not used so we return true
178-
return true;
176+
177+
private void deleteFile(FilePath file, TaskListener listener) throws IOException, InterruptedException {
178+
try {
179+
file.delete();
180+
listener.getLogger().println("Temporary file: " + file.getRemote() + " deleted");
181+
} catch (IOException | InterruptedException e) {
182+
listener.getLogger().println("Unable to delete temporary file: " + file.getRemote());
183+
throw e;
179184
}
180-
int lastModifiedIntValue = getLastModifiedIntValue();
181-
Long diffInMillis = new Date().getTime() - filePath.lastModified();
182-
Long diffInHour = diffInMillis / DateUtils.MILLIS_PER_HOUR;
183-
return diffInHour <= lastModifiedIntValue;
184185
}
185186

186-
private int getLastModifiedIntValue(){
187-
try{
188-
int m = Integer.parseInt(this.lastModified);
189-
if(m <= 0){
190-
throw new XrayJenkinsGenericException("last modified value must be a positive integer");
191-
}
192-
return m;
193-
} catch (NumberFormatException e){
194-
throw new XrayJenkinsGenericException("last modified value is not an integer");
195-
}
187+
private void uploadZipFile(XrayTestImporter client, TaskListener listener, FilePath zipFile) throws IOException, InterruptedException {
188+
FileStream zipFileStream = new FileStream(
189+
zipFile.getName(),
190+
zipFile.read(),
191+
ContentType.APPLICATION_JSON);
192+
UploadResult uploadResult = client.importFeatures(this.projectKey, zipFileStream);
193+
listener.getLogger().println(uploadResult.getMessage());
194+
}
195+
196+
private FilePath createZipFile(final FilePath workspace) {
197+
return new FilePath(workspace, TMP_ZIP_FILENAME);
196198
}
197199

198200
@Extension
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.xpandit.plugins.xrayjenkins.task.filefilters;
2+
3+
import com.xpandit.plugins.xrayjenkins.exceptions.XrayJenkinsGenericException;
4+
import hudson.FilePath;
5+
import java.io.File;
6+
import java.io.FileFilter;
7+
import java.io.IOException;
8+
import java.util.Date;
9+
import java.util.Set;
10+
import org.apache.commons.lang3.StringUtils;
11+
import org.apache.commons.lang3.time.DateUtils;
12+
13+
public class OnlyFeatureFilesInPathFilter implements FileFilter {
14+
15+
private final Set<String> validFilePaths;
16+
private final String lastModified;
17+
18+
public OnlyFeatureFilesInPathFilter(Set<String> validFilePaths, String lastModified) {
19+
this.validFilePaths = validFilePaths;
20+
this.lastModified = lastModified;
21+
}
22+
23+
@Override
24+
public boolean accept(File pathname) {
25+
try {
26+
return validFilePaths.contains(pathname.getAbsolutePath()) &&
27+
isApplicableAsModifiedFile(pathname);
28+
} catch (Exception e) {
29+
return false;
30+
}
31+
}
32+
33+
private boolean isApplicableAsModifiedFile(File file) throws InterruptedException, IOException {
34+
return file != null && isApplicableAsModifiedFile(new FilePath(file));
35+
}
36+
37+
private boolean isApplicableAsModifiedFile(FilePath filePath) throws InterruptedException, IOException{
38+
if(StringUtils.isBlank(lastModified)){
39+
//the modified field is not used so we return true
40+
return true;
41+
}
42+
int lastModifiedIntValue = getLastModifiedIntValue();
43+
long diffInMillis = new Date().getTime() - filePath.lastModified();
44+
long diffInHour = diffInMillis / DateUtils.MILLIS_PER_HOUR;
45+
46+
return diffInHour <= lastModifiedIntValue;
47+
}
48+
49+
private int getLastModifiedIntValue(){
50+
try{
51+
int m = Integer.parseInt(this.lastModified);
52+
if(m <= 0){
53+
throw new XrayJenkinsGenericException("last modified value must be a positive integer");
54+
}
55+
return m;
56+
} catch (NumberFormatException e){
57+
throw new XrayJenkinsGenericException("last modified value is not an integer");
58+
}
59+
}
60+
}

0 commit comments

Comments
 (0)