Skip to content

Commit

Permalink
minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
priyankashah1224 committed Feb 22, 2019
1 parent 1c4a166 commit c25e420
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 155 deletions.
2 changes: 1 addition & 1 deletion release.properties
@@ -1,5 +1,5 @@
#release configuration
#Fri Feb 22 20:11:09 IST 2019
#Fri Feb 22 20:46:02 IST 2019
scm.tagNameFormat=@{project.artifactId}-@{project.version}
pushChanges=true
scm.url=scm\:git\:git@github.com\:jenkinsci/qmetry-for-jira-test-management-plugin.git
Expand Down
273 changes: 119 additions & 154 deletions src/main/java/com/qmetry/FindFile.java
Expand Up @@ -35,166 +35,144 @@
import hudson.model.Run;
import hudson.model.TopLevelItem;
import hudson.model.Node;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import java.io.FileNotFoundException;

import java.util.List;

public class FindFile
{
public class FindFile {
protected static boolean onSlave = false;
protected static File resultFile;
public static File findFile(String filePath, Run<?, ?> run, TaskListener listener,String format,FilePath workspace) throws IOException,InterruptedException,FileNotFoundException
{

public static File findFile(String filePath, Run<?, ?> run, TaskListener listener, String format,
FilePath workspace) throws IOException, InterruptedException, FileNotFoundException {
onSlave = false;
resultFile = null;
if(filePath.startsWith("/"))
{
filePath=filePath.substring(1);
if (filePath.startsWith("/")) {
filePath = filePath.substring(1);
}

if(workspace!= null /*&& workspace.toComputer()!=null && workspace.toComputer().getNode()!=null*/)
{

if (workspace != null /* && workspace.toComputer()!=null && workspace.toComputer().getNode()!=null */) {
Computer comp = workspace.toComputer();
if(comp!=null)
{
if (comp != null) {
Node node = comp.getNode();
if(node!=null)
{
Computer comp1 = node.toComputer();
if(comp1!=null)
{
if(comp1 instanceof SlaveComputer)
{
if (node != null) {
Computer comp1 = node.toComputer();
if (comp1 != null) {
if (comp1 instanceof SlaveComputer) {
onSlave = true;
listener.getLogger().println("QMetry for JIRA : build taking place on slave machine");
//FilePath slaveMachineWorkspace = project.getWorkspace();
// FilePath slaveMachineWorkspace = project.getWorkspace();
FilePath slaveMachineWorkspace = workspace;
/*if(filePath.startsWith("/"))
{
filePath=filePath.substring(1);
}*/
/*
* if(filePath.startsWith("/")) { filePath=filePath.substring(1); }
*/
FilePath f = null;
if(format.equals("qas/json"))
{
//Getting latest testresult files for QAS
listener.getLogger().println("QMetry for JIRA : Getting latest test-result folder for QAS...");
f = lastFileModified(slaveMachineWorkspace , filePath );
if (format.equals("qas/json")) {
// Getting latest testresult files for QAS
listener.getLogger()
.println("QMetry for JIRA : Getting latest test-result folder for QAS...");
f = lastFileModified(slaveMachineWorkspace, filePath);
filePath = filePath + "/" + f.getName();
listener.getLogger().println("QMetry for JIRA : Latest test-result folder : " + f.toString());
//listener.getLogger().println("[DEBUG] : final path : "+f.toString());
//listener.getLogger().println("[DEBUG] : filepath : " +filePath);
}
else
{
f = new FilePath(slaveMachineWorkspace , filePath);
if(!f.exists())
{
listener.getLogger()
.println("QMetry for JIRA : Latest test-result folder : " + f.toString());
// listener.getLogger().println("[DEBUG] : final path : "+f.toString());
// listener.getLogger().println("[DEBUG] : filepath : " +filePath);
} else {
f = new FilePath(slaveMachineWorkspace, filePath);
if (!f.exists()) {
throw new FileNotFoundException("cannot find file : " + f);
}
}

//boolean filter = false;
//String fileMask = "";
if(f.isDirectory())
{
String absPath=f.toString();

if(!filePath.endsWith("/"))
{
filePath=filePath.concat("/");

// boolean filter = false;
// String fileMask = "";
if (f.isDirectory()) {
String absPath = f.toString();

if (!filePath.endsWith("/")) {
filePath = filePath.concat("/");
}
//Changes for filtering files

// Changes for filtering files
/*
if(format.equals("junit/xml") || format.equals("hpuft/xml") || format.equals("testng/xml"))
{
filter=true;
fileMask=filePath.concat("*.xml");
listener.getLogger().println("[DEBUG] : xml filepath : "+fileMask);
}
else if(format.equals("cucumber/json") || format.equals("qas/json"))
{
filter=true;
fileMask=filePath.concat("*.json");
listener.getLogger().println("[DEBUG] : json filepath : "+fileMask);
}*/
* if(format.equals("junit/xml") || format.equals("hpuft/xml") ||
* format.equals("testng/xml")) { filter=true;
* fileMask=filePath.concat("*.xml");
* listener.getLogger().println("[DEBUG] : xml filepath : "+fileMask); } else
* if(format.equals("cucumber/json") || format.equals("qas/json")) {
* filter=true; fileMask=filePath.concat("*.json");
* listener.getLogger().println("[DEBUG] : json filepath : "+fileMask); }
*/
}
if(!slaveMachineWorkspace.exists())
{
listener.getLogger().println("QMetry for JIRA : [ERROR]Failed to access slave machine workspace directory");
if (!slaveMachineWorkspace.exists()) {
listener.getLogger().println(
"QMetry for JIRA : [ERROR]Failed to access slave machine workspace directory");
return null;
}

FilePath masterMachineWorkspace = null;
//for free style job
if(run.getParent() instanceof AbstractProject)
{
AbstractProject project = (AbstractProject)run.getParent();
if(project.getCustomWorkspace() != null && project.getCustomWorkspace().length()>0 )
{

FilePath masterMachineWorkspace = null;
// for free style job
if (run.getParent() instanceof AbstractProject) {
AbstractProject project = (AbstractProject) run.getParent();
if (project.getCustomWorkspace() != null && project.getCustomWorkspace().length() > 0) {
masterMachineWorkspace = new FilePath(new File(project.getCustomWorkspace()));
}
else
{
masterMachineWorkspace = Jenkins.getInstance().getWorkspaceFor((TopLevelItem)project);
} else {
masterMachineWorkspace = Jenkins.getInstance()
.getWorkspaceFor((TopLevelItem) project);
}
}
//for pipeline job
else if(run.getParent() instanceof WorkflowJob)
{
//listener.getLogger().println("[DEBUG] : instance of WorkFlowJob");
WorkflowJob project = (WorkflowJob)run.getParent();
masterMachineWorkspace = Jenkins.getInstance().getWorkspaceFor((TopLevelItem)project);
// for pipeline job
else if (run.getParent() instanceof WorkflowJob) {
// listener.getLogger().println("[DEBUG] : instance of WorkFlowJob");
WorkflowJob project = (WorkflowJob) run.getParent();
masterMachineWorkspace = Jenkins.getInstance().getWorkspaceFor((TopLevelItem) project);
}
//listener.getLogger().println("[DEBUG] : masterMachineWorkspace : " + masterMachineWorkspace);
if(masterMachineWorkspace==null)
{
listener.getLogger().println("QMetry for JIRA : [ERROR]Failed to access master machine workspace directory");
// listener.getLogger().println("[DEBUG] : masterMachineWorkspace : " +
// masterMachineWorkspace);
if (masterMachineWorkspace == null) {
listener.getLogger().println(
"QMetry for JIRA : [ERROR]Failed to access master machine workspace directory");
return null;
}
listener.getLogger().println("QMetry for JIRA : Copying files from slave to master machine...");
listener.getLogger()
.println("QMetry for JIRA : Copying files from slave to master machine...");
int fileCount = slaveMachineWorkspace.copyRecursiveTo(filePath, masterMachineWorkspace);
//Changes for file filtering
// Changes for file filtering
/*
int fileCount;
if(filter)
{
fileCount = slaveMachineWorkspace.copyRecursiveTo(fileMask, masterMachineWorkspace);
}
else
{
fileCount = slaveMachineWorkspace.copyRecursiveTo(filePath, masterMachineWorkspace);
}
*/
listener.getLogger().println("QMetry for JIRA : Total "+fileCount+" result file(s) copied from slave to master machine");
* int fileCount; if(filter) { fileCount =
* slaveMachineWorkspace.copyRecursiveTo(fileMask, masterMachineWorkspace); }
* else { fileCount = slaveMachineWorkspace.copyRecursiveTo(filePath,
* masterMachineWorkspace); }
*/
listener.getLogger().println("QMetry for JIRA : Total " + fileCount
+ " result file(s) copied from slave to master machine");
File finalResultFile = new File(masterMachineWorkspace.toURI());
resultFile = new File(finalResultFile, filePath);
return resultFile;
}
else if(comp1 instanceof MasterComputer)
{
} else if (comp1 instanceof MasterComputer) {
onSlave = false;
//File masterWorkspace = new File(project.getWorkspace().toURI());
//listener.getLogger().println("[DEBUG] : Build taking place on master machine");
// File masterWorkspace = new File(project.getWorkspace().toURI());
// listener.getLogger().println("[DEBUG] : Build taking place on master
// machine");
File masterWorkspace = new File(workspace.toString());
FilePath resultFilePath = null;
if(format.equals("qas/json"))
{
//Getting latest testresult files for QAS
listener.getLogger().println("QMetry for JIRA : Getting latest test-result folder for QAS...");
resultFilePath = lastFileModified(new FilePath(masterWorkspace),filePath);
listener.getLogger().println("QMetry for JIRA : Latest test-result folder : " + resultFilePath.toString());
//listener.getLogger().println("[DEBUG]: final path : "+resultFilePath.toString());
}
else
{
if (format.equals("qas/json")) {
// Getting latest testresult files for QAS
listener.getLogger()
.println("QMetry for JIRA : Getting latest test-result folder for QAS...");
resultFilePath = lastFileModified(new FilePath(masterWorkspace), filePath);
listener.getLogger().println(
"QMetry for JIRA : Latest test-result folder : " + resultFilePath.toString());
// listener.getLogger().println("[DEBUG]: final path :
// "+resultFilePath.toString());
} else {
resultFilePath = new FilePath(new File(masterWorkspace, filePath));
if(!resultFilePath.exists())
{
if (!resultFilePath.exists()) {
throw new FileNotFoundException("cannot find file : " + resultFilePath);
}
//listener.getLogger().println("[DEBUG]: final path : "+resultFilePath.toString());
// listener.getLogger().println("[DEBUG]: final path :
// "+resultFilePath.toString());
}
resultFile = new File(resultFilePath.toString());
return resultFile;
Expand All @@ -205,25 +183,20 @@ else if(comp1 instanceof MasterComputer)
}
return null;
}

public static FilePath lastFileModified(FilePath base, String path) throws IOException,InterruptedException,FileNotFoundException
{
FilePath slaveDir = new FilePath(base,path);
if(!slaveDir.exists())
{

public static FilePath lastFileModified(FilePath base, String path)
throws IOException, InterruptedException, FileNotFoundException {
FilePath slaveDir = new FilePath(base, path);
if (!slaveDir.exists()) {
throw new FileNotFoundException("cannot find file : " + slaveDir);
}
List<FilePath> files = slaveDir.listDirectories();
long lastMod = Long.MIN_VALUE;
FilePath choice = null;
if(files!=null)
{
for (FilePath file : files)
{
if(file.isDirectory() && !(file.getName()).equals("surefire"))
{
if (file.lastModified() > lastMod)
{
if (files != null) {
for (FilePath file : files) {
if (file.isDirectory() && !(file.getName()).equals("surefire")) {
if (file.lastModified() > lastMod) {
choice = file;
lastMod = file.lastModified();
}
Expand All @@ -233,32 +206,24 @@ public static FilePath lastFileModified(FilePath base, String path) throws IOExc
return choice;
}

public static boolean deleteFilesFromMaster(File sourceDir)
{
public static boolean deleteFilesFromMaster(File sourceDir) {
return recursiveDelete(sourceDir);
}

public static boolean recursiveDelete(File file)
{
if (file == null || !file.exists()) {
return false;
}
else if (file != null){

if (file.isDirectory())
{
if(file.listFiles() != null)
{
for (File f : file.listFiles())
{
//call recursively
boolean deleted = recursiveDelete(f);
public static boolean recursiveDelete(File file) {
if (!(file != null && file.exists())) {
if (file.isDirectory()) {
if (file.listFiles() != null) {
for (File f : file.listFiles()) {
// call recursively
boolean deleted = recursiveDelete(f);
}
}
}
return file.delete();
}
return file.delete();
}
return true;

return false;
}

}

0 comments on commit c25e420

Please sign in to comment.