1616import com .xpandit .plugins .xrayjenkins .model .HostingType ;
1717import com .xpandit .plugins .xrayjenkins .model .ServerConfiguration ;
1818import com .xpandit .plugins .xrayjenkins .model .XrayInstance ;
19+ import com .xpandit .plugins .xrayjenkins .task .filefilters .OnlyFeatureFilesInPathFilter ;
1920import com .xpandit .xray .exception .XrayClientCoreGenericException ;
2021import com .xpandit .xray .model .FileStream ;
2122import com .xpandit .xray .model .UploadResult ;
3435import hudson .tasks .Builder ;
3536import hudson .util .FormValidation ;
3637import hudson .util .ListBoxModel ;
38+ import java .io .File ;
39+ import java .io .FileFilter ;
3740import java .io .IOException ;
3841import java .util .Date ;
3942import java .util .List ;
43+ import java .util .Set ;
4044import javax .annotation .Nonnull ;
4145import jenkins .tasks .SimpleBuildStep ;
4246import org .apache .commons .lang3 .StringUtils ;
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
0 commit comments