Jenkins Utils
Hi...
This post could give you some idea about Jenkins-utils api
For this util..you need the below mentioned jars
1. Jsoup
2. jenkins-client
Please check the link for reference https://github.com/jenkinsci/java-client-api
package com.jenkins.util.util_jenkins;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
//https://github.com/jenkinsci/java-client-api
public class AppTest2 {
private static String baseURL = "jenkinsurl";
private static String viewName = "Viewname";
private static String userName = "jenkinsusername";
private static String passWord = "jenkinspassword";
public static void main(String h[]) throws URISyntaxException, IOException{
JobWithDetails jobdetails = null;
ArrayList<Object> JD = null;
ArrayList<Object> DT = null;
ArrayList<ArrayList<Object>> DETAILS = null;
JenkinsServer jenkins = new JenkinsServer(new URI(baseURL+"/"+viewName), userName, passWord);
int pass=0, fail=0, abort=0;
JD = new ArrayList<Object>();
DETAILS = new ArrayList<ArrayList<Object>>();
Map<String, Job> jobs = jenkins.getJobs();
for(String job:jobs.keySet()){
DT = new ArrayList<Object>();
jobdetails = jobs.get(job).details();
DT.add(jobdetails.getBuilds().get(0).getNumber());
DT.add(jobdetails.getBuilds().size());
for(int i=0;i<jobdetails.getBuilds().size();i++){
if(jobdetails.getBuilds().get(i).details().getResult().toString().equalsIgnoreCase("SUCCESS")){
pass++;
}
if(jobdetails.getBuilds().get(i).details().getResult().toString().equalsIgnoreCase("FAILURE")){
fail++;
}
}
if((pass+fail)!=jobdetails.getBuilds().size()){
abort = jobdetails.getBuilds().size()-(pass+fail);
}
DT.add(pass);
DT.add(fail);
DT.add(abort);
JD.add(jobdetails.getDisplayName());
DETAILS.add(DT);
pass=0;
fail=0;
abort=0;
}
createHTMLFile(JD,DETAILS);
System.out.println("Execution done..HTML file created");
}
public static void createHTMLFile(ArrayList<Object> jobs,ArrayList<ArrayList<Object>> data) throws IOException{
File f = new File("d://template.html");
FileWriter fw = new FileWriter(f);
int j = 0;
Document doc = Jsoup.parse("<html>"
+ "<head></head>"
+ "<body class='body-styles-cls'>"
+ "<table border='3'><tbody id='tid'>"
+ "<th bgcolor='4771F3'>JOB NAME</th>"
+ "<th bgcolor='4771F3'>#BUILDS</th>"
+ "<th bgcolor='4771F3'>#AVAILABLE BUILDS</th>"
+ "<th bgcolor='4771F3'>#PASS</th>"
+ "<th bgcolor='4771F3'>#FAIL</th>"
+ "<th bgcolor='4771F3'>#ABORT</th>");
Element div = doc.getElementById("tid");
for(Object jobName:jobs){
ArrayList<Object> dataReturned = data.get(j);
j++;
div.append("<tr><td bgcolor='F496F4'>"+jobName+"<td bgcolor='F496F4'>"
+dataReturned.get(0)+"<td bgcolor='F496F4'>"
+dataReturned.get(1)+"<td bgcolor='green'>"
+dataReturned.get(2)+"<td bgcolor='red'>"
+dataReturned.get(3)+"<td bgcolor='yellow'>"
+dataReturned.get(4));
}
fw.write(doc.toString());
fw.flush();
fw.close();
}
}
This post could give you some idea about Jenkins-utils api
For this util..you need the below mentioned jars
1. Jsoup
2. jenkins-client
Please check the link for reference https://github.com/jenkinsci/java-client-api
package com.jenkins.util.util_jenkins;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Map;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobWithDetails;
//https://github.com/jenkinsci/java-client-api
public class AppTest2 {
private static String baseURL = "jenkinsurl";
private static String viewName = "Viewname";
private static String userName = "jenkinsusername";
private static String passWord = "jenkinspassword";
public static void main(String h[]) throws URISyntaxException, IOException{
JobWithDetails jobdetails = null;
ArrayList<Object> JD = null;
ArrayList<Object> DT = null;
ArrayList<ArrayList<Object>> DETAILS = null;
JenkinsServer jenkins = new JenkinsServer(new URI(baseURL+"/"+viewName), userName, passWord);
int pass=0, fail=0, abort=0;
JD = new ArrayList<Object>();
DETAILS = new ArrayList<ArrayList<Object>>();
Map<String, Job> jobs = jenkins.getJobs();
for(String job:jobs.keySet()){
DT = new ArrayList<Object>();
jobdetails = jobs.get(job).details();
DT.add(jobdetails.getBuilds().get(0).getNumber());
DT.add(jobdetails.getBuilds().size());
for(int i=0;i<jobdetails.getBuilds().size();i++){
if(jobdetails.getBuilds().get(i).details().getResult().toString().equalsIgnoreCase("SUCCESS")){
pass++;
}
if(jobdetails.getBuilds().get(i).details().getResult().toString().equalsIgnoreCase("FAILURE")){
fail++;
}
}
if((pass+fail)!=jobdetails.getBuilds().size()){
abort = jobdetails.getBuilds().size()-(pass+fail);
}
DT.add(pass);
DT.add(fail);
DT.add(abort);
JD.add(jobdetails.getDisplayName());
DETAILS.add(DT);
pass=0;
fail=0;
abort=0;
}
createHTMLFile(JD,DETAILS);
System.out.println("Execution done..HTML file created");
}
public static void createHTMLFile(ArrayList<Object> jobs,ArrayList<ArrayList<Object>> data) throws IOException{
File f = new File("d://template.html");
FileWriter fw = new FileWriter(f);
int j = 0;
Document doc = Jsoup.parse("<html>"
+ "<head></head>"
+ "<body class='body-styles-cls'>"
+ "<table border='3'><tbody id='tid'>"
+ "<th bgcolor='4771F3'>JOB NAME</th>"
+ "<th bgcolor='4771F3'>#BUILDS</th>"
+ "<th bgcolor='4771F3'>#AVAILABLE BUILDS</th>"
+ "<th bgcolor='4771F3'>#PASS</th>"
+ "<th bgcolor='4771F3'>#FAIL</th>"
+ "<th bgcolor='4771F3'>#ABORT</th>");
Element div = doc.getElementById("tid");
for(Object jobName:jobs){
ArrayList<Object> dataReturned = data.get(j);
j++;
div.append("<tr><td bgcolor='F496F4'>"+jobName+"<td bgcolor='F496F4'>"
+dataReturned.get(0)+"<td bgcolor='F496F4'>"
+dataReturned.get(1)+"<td bgcolor='green'>"
+dataReturned.get(2)+"<td bgcolor='red'>"
+dataReturned.get(3)+"<td bgcolor='yellow'>"
+dataReturned.get(4));
}
fw.write(doc.toString());
fw.flush();
fw.close();
}
}
Comments
Post a Comment