Skip to content

Commit

Permalink
Merge pull request #2 from lalitKumarHCL/main
Browse files Browse the repository at this point in the history
bug fix
  • Loading branch information
anandnd authored Jun 13, 2022
2 parents d7630eb + 4eb3ed3 commit a29412a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@
<version>1.10</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.hcl.devops</groupId>
<artifactId>hcl-accelerate</artifactId>
<version>2.1.4</version>
<version>2.1.5</version>
<packaging>hpi</packaging>
<name>HCL Accelerate Plugin</name>
<description>This plugin can run Jenkins jobs as a part of a deployment plan in HCL Accelerate.</description>
Expand Down Expand Up @@ -167,7 +167,7 @@
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.6</version>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down Expand Up @@ -238,7 +238,7 @@
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.2.0</version>
<version>5.14.2</version>
</dependency>
<dependency>
<groupId>org.acegisecurity</groupId>
Expand Down
100 changes: 55 additions & 45 deletions src/main/java/com/hcl/devops/connect/CloudSocketComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,14 @@ public void handleDelivery(String consumerTag, Envelope envelope,
String syncToken = getSyncToken();

String url = removeTrailingSlash(Jenkins.getInstance().getDescriptorByType(DevOpsGlobalConfiguration.class).getBaseUrl());
boolean connected = CloudPublisher.testConnection(syncId, syncToken, url);
CloudPublisher.testConnection(syncId, syncToken, url);
} else {
String message = new String(body, "UTF-8");
String payload = null;
String syncToken = getSyncToken();
try {
payload = decrypt(syncToken, message.toString());
log.info("payload: " + payload);
} catch (Exception e) {
System.out.println("Unable to decrypt");
}
Expand All @@ -206,69 +207,78 @@ public void handleDelivery(String consumerTag, Envelope envelope,
String encodedString = getEncodedString(plainCredentials);
String authorizationHeader = "Basic " + encodedString;
String rootUrl = Jenkins.getInstance().getRootUrl();
log.info("Root Url: " + rootUrl);
String path = "job/"+jobName.replaceAll("/", "/job/")+"/api/json";
log.info("Path: " + path);
String finalUrl = null;
String buildDetails = null;
try {
URIBuilder builder = new URIBuilder(rootUrl);
builder.setPath(builder.getPath()+path);
builder.setQuery("fetchAllbuildDetails=True");
builder.setParameter("fetchAllbuildDetails", "True");
finalUrl = builder.toString();
log.info("Final Url: " + finalUrl);
} catch (Exception e) {
log.error("Caught error while building url to get details of previous builds: ", e);
log.warn("Caught error while building url to get details of previous builds: ", e);
}
try {
HttpResponse<String> response = Unirest.get(finalUrl)
.header("Authorization", authorizationHeader)
.asString();
buildDetails = response.getBody().toString();
log.info("buildDetails Response: " + buildDetails);
} catch (UnirestException e) {
log.error("UnirestException: Failed to get details of previous Builds", e);
log.warn("UnirestException: Failed to get details of previous Builds. Skipping duplicate check.");
System.out.println(" [x] Received '" + message + "'");
CloudWorkListener2 cloudWorkListener = new CloudWorkListener2();
cloudWorkListener.call("startJob", message);
}
JSONArray buildDetailsArray = JSONArray.fromObject("[" + buildDetails + "]");
JSONObject buildDetailsObject = buildDetailsArray.getJSONObject(0);
if(buildDetailsObject.has("builds")){
JSONArray builds = JSONArray.fromObject(buildDetailsObject.getString("builds"));
int buildsCount = 0;
if(builds.size()<50){
buildsCount=builds.size();
}
else{
buildsCount=50;
}
StringBuilder str = new StringBuilder();
for(int i=0;i<buildsCount;i++){
JSONObject build = builds.getJSONObject(i);
if(build.has("url")){
String buildUrl = build.getString("url")+"consoleText";
String finalBuildUrl = null;
try {
URIBuilder builder = new URIBuilder(buildUrl);
finalBuildUrl = builder.toString();
} catch (Exception e) {
log.error("Caught error while building console log url: ", e);
}
try {
HttpResponse<String> buildResponse = Unirest.get(finalBuildUrl)
.header("Authorization", authorizationHeader)
.asString();
String buildConsole = buildResponse.getBody().toString();
str.append(buildConsole);
} catch (UnirestException e) {
log.error("UnirestException: Failed to get console Logs of previous builds", e);
if (buildDetails != null) {
JSONArray buildDetailsArray = JSONArray.fromObject("[" + buildDetails + "]");
JSONObject buildDetailsObject = buildDetailsArray.getJSONObject(0);
if(buildDetailsObject.has("builds")){
JSONArray builds = JSONArray.fromObject(buildDetailsObject.getString("builds"));
int buildsCount = 0;
if(builds.size()<50){
buildsCount=builds.size();
}
else{
buildsCount=50;
}
StringBuilder str = new StringBuilder();
for(int i=0;i<buildsCount;i++){
JSONObject build = builds.getJSONObject(i);
if(build.has("url")){
String buildUrl = build.getString("url")+"consoleText";
String finalBuildUrl = null;
try {
URIBuilder builder = new URIBuilder(buildUrl);
finalBuildUrl = builder.toString();
} catch (Exception e) {
log.error("Caught error while building console log url: ", e);
}
try {
HttpResponse<String> buildResponse = Unirest.get(finalBuildUrl)
.header("Authorization", authorizationHeader)
.asString();
String buildConsole = buildResponse.getBody().toString();
str.append(buildConsole);
} catch (UnirestException e) {
log.error("UnirestException: Failed to get console Logs of previous builds", e);
}
}
}
String allConsoleLogs = str.toString();
boolean isFound = allConsoleLogs.contains("Started due to a request from UrbanCode Velocity. Work Id: "+workId);
if(isFound==true){
log.info(" =========================== Found duplicate Jenkins Job and stopped it =========================== ");
}
else{
System.out.println(" [x] Received '" + message + "'");
CloudWorkListener2 cloudWorkListener = new CloudWorkListener2();
cloudWorkListener.call("startJob", message);
}
}
String allConsoleLogs =str.toString();
boolean isFound = allConsoleLogs.contains("Started due to a request from HCL Accelerate. Work Id: "+workId);
if(isFound==true){
log.info(" =========================== Found duplicate Jenkins Job and stopped it =========================== ");
}
else{
System.out.println(" [x] Received '" + message + "'");
CloudWorkListener2 cloudWorkListener = new CloudWorkListener2();
cloudWorkListener.call("startJob", message);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public JSONObject generate(boolean completed) {
}
}
} catch (UnirestException e) {
log.error("UnirestException: Failed to get details of requestor", e);
log.warn("UnirestException: Failed to get details of requestor");
}
// log.info(result.toString());
return result;
Expand Down

0 comments on commit a29412a

Please sign in to comment.