示例#1
0
 public List<CIBuild> getBuilds(CIJob job) throws PhrescoException {
   if (debugEnabled) {
     S_LOGGER.debug("Entering Method CIManagerImpl.getCIBuilds(CIJob job)");
   }
   List<CIBuild> ciBuilds = null;
   try {
     if (debugEnabled) {
       S_LOGGER.debug("getCIBuilds()  JobName = " + job.getName());
     }
     JsonArray jsonArray = getBuildsArray(job);
     ciBuilds = new ArrayList<CIBuild>(jsonArray.size());
     Gson gson = new Gson();
     CIBuild ciBuild = null;
     for (int i = 0; i < jsonArray.size(); i++) {
       ciBuild = gson.fromJson(jsonArray.get(i), CIBuild.class);
       setBuildStatus(ciBuild, job);
       String buildUrl = ciBuild.getUrl();
       String jenkinUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort();
       buildUrl =
           buildUrl.replaceAll(
               "localhost:" + job.getJenkinsPort(),
               jenkinUrl); // when displaying url it should display setup machine ip
       ciBuild.setUrl(buildUrl);
       ciBuilds.add(ciBuild);
     }
   } catch (Exception e) {
     if (debugEnabled) {
       S_LOGGER.debug(
           "Entering Method CIManagerImpl.getCIBuilds(CIJob job) " + e.getLocalizedMessage());
     }
   }
   return ciBuilds;
 }
示例#2
0
  private void setBuildStatus(CIBuild ciBuild, CIJob job) throws PhrescoException {
    S_LOGGER.debug("Entering Method CIManagerImpl.setBuildStatus(CIBuild ciBuild)");
    S_LOGGER.debug("setBuildStatus()  url = " + ciBuild.getUrl());
    String buildUrl = ciBuild.getUrl();
    String jenkinsUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort();
    buildUrl =
        buildUrl.replaceAll(
            "localhost:" + job.getJenkinsPort(),
            jenkinsUrl); // display the jenkins running url in ci list
    String response = getJsonResponse(buildUrl + API_JSON);
    JsonParser parser = new JsonParser();
    JsonElement jsonElement = parser.parse(response);
    JsonObject jsonObject = jsonElement.getAsJsonObject();

    JsonElement resultJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT);
    JsonElement idJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_ID);
    JsonElement timeJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_TIME_STAMP);
    JsonArray asJsonArray = jsonObject.getAsJsonArray(FrameworkConstants.CI_JOB_BUILD_ARTIFACTS);

    if (jsonObject
        .get(FrameworkConstants.CI_JOB_BUILD_RESULT)
        .toString()
        .equals(STRING_NULL)) { // when build is result is not known
      ciBuild.setStatus(INPROGRESS);
    } else if (resultJson.getAsString().equals(CI_SUCCESS_FLAG)
        && asJsonArray.size()
            < 1) { // when build is success and zip relative path is not added in json
      ciBuild.setStatus(INPROGRESS);
    } else {
      ciBuild.setStatus(resultJson.getAsString());
      // download path
      for (JsonElement jsonArtElement : asJsonArray) {
        String buildDownloadZip =
            jsonArtElement
                .getAsJsonObject()
                .get(FrameworkConstants.CI_JOB_BUILD_DOWNLOAD_PATH)
                .toString();
        if (buildDownloadZip.endsWith(CI_ZIP)) {
          if (debugEnabled) {
            S_LOGGER.debug("download artifact " + buildDownloadZip);
          }
          ciBuild.setDownload(buildDownloadZip);
        }
      }
    }

    ciBuild.setId(idJson.getAsString());
    String dispFormat = DD_MM_YYYY_HH_MM_SS;
    ciBuild.setTimeStamp(getDate(timeJson.getAsString(), dispFormat));
  }
示例#3
0
 private CLI getCLI(CIJob job) throws PhrescoException {
   if (debugEnabled) {
     S_LOGGER.debug("Entering Method CIManagerImpl.getCLI()");
   }
   String jenkinsUrl =
       HTTP_PROTOCOL
           + PROTOCOL_POSTFIX
           + job.getJenkinsUrl()
           + COLON
           + job.getJenkinsPort()
           + FORWARD_SLASH
           + CI
           + FORWARD_SLASH;
   if (debugEnabled) {
     S_LOGGER.debug("jenkinsUrl to get cli object " + jenkinsUrl);
   }
   try {
     return new CLI(new URL(jenkinsUrl));
   } catch (MalformedURLException e) {
     throw new PhrescoException(e);
   } catch (IOException e) {
     throw new PhrescoException(e);
   } catch (InterruptedException e) {
     throw new PhrescoException(e);
   }
 }
示例#4
0
 private int getTotalBuilds(CIJob job) throws PhrescoException {
   try {
     S_LOGGER.debug("Entering Method CIManagerImpl.getTotalBuilds(CIJob job)");
     S_LOGGER.debug("getCIBuilds()  JobName = " + job.getName());
     JsonArray jsonArray = getBuildsArray(job);
     Gson gson = new Gson();
     CIBuild ciBuild = null;
     if (jsonArray.size() > 0) {
       ciBuild = gson.fromJson(jsonArray.get(0), CIBuild.class);
       String buildUrl = ciBuild.getUrl();
       String jenkinsUrl = job.getJenkinsUrl() + ":" + job.getJenkinsPort();
       // display the jenkins running url in ci
       buildUrl = buildUrl.replaceAll("localhost:" + job.getJenkinsPort(), jenkinsUrl);
       // list
       String response = getJsonResponse(buildUrl + API_JSON);
       JsonParser parser = new JsonParser();
       JsonElement jsonElement = parser.parse(response);
       JsonObject jsonObject = jsonElement.getAsJsonObject();
       JsonElement resultJson = jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT);
       JsonArray asJsonArray =
           jsonObject.getAsJsonArray(FrameworkConstants.CI_JOB_BUILD_ARTIFACTS);
       // when build result is not known
       if (jsonObject.get(FrameworkConstants.CI_JOB_BUILD_RESULT).toString().equals(STRING_NULL)) {
         // it indicates the job is in progress and not yet completed
         return -1;
         // when build is success and build zip relative path is unknown
       } else if (resultJson.getAsString().equals(CI_SUCCESS_FLAG) && asJsonArray.size() < 1) {
         return -1;
       } else {
         return jsonArray.size();
       }
     } else {
       return -1; // When the project is build first time,
     }
   } catch (ClientHandlerException ex) {
     S_LOGGER.error(ex.getLocalizedMessage());
     throw new PhrescoException(ex);
   }
 }
示例#5
0
  private void setMailCredential(CIJob job) {
    if (debugEnabled) {
      S_LOGGER.debug("Entering Method CIManagerImpl.setMailCredential");
    }
    try {
      String jenkinsTemplateDir = Utility.getJenkinsTemplateDir();
      String mailFilePath = jenkinsTemplateDir + MAIL + HYPHEN + CREDENTIAL_XML;
      if (debugEnabled) {
        S_LOGGER.debug("configFilePath ... " + mailFilePath);
      }
      File mailFile = new File(mailFilePath);

      SvnProcessor processor = new SvnProcessor(mailFile);

      //			DataInputStream in = new DataInputStream(new FileInputStream(mailFile));
      //			while (in.available() != 0) {
      //				System.out.println(in.readLine());
      //			}
      //			in.close();

      // Mail have to go with jenkins running email address
      InetAddress ownIP = InetAddress.getLocalHost();
      processor.changeNodeValue(
          CI_HUDSONURL,
          HTTP_PROTOCOL
              + PROTOCOL_POSTFIX
              + ownIP.getHostAddress()
              + COLON
              + job.getJenkinsPort()
              + FORWARD_SLASH
              + CI
              + FORWARD_SLASH);
      processor.changeNodeValue("smtpAuthUsername", job.getSenderEmailId());
      processor.changeNodeValue("smtpAuthPassword", job.getSenderEmailPassword());
      processor.changeNodeValue("adminAddress", job.getSenderEmailId());

      // jenkins home location
      String jenkinsJobHome = System.getenv(JENKINS_HOME);
      StringBuilder builder = new StringBuilder(jenkinsJobHome);
      builder.append(File.separator);

      processor.writeStream(new File(builder.toString() + CI_MAILER_XML));
    } catch (Exception e) {
      S_LOGGER.error(
          "Entered into the catch block of CIManagerImpl.setMailCredential "
              + e.getLocalizedMessage());
    }
  }
示例#6
0
 private int getProgressInBuild(CIJob job) throws PhrescoException {
   S_LOGGER.debug("Entering Method CIManagerImpl.isBuilding(CIJob job)");
   String jenkinsUrl =
       HTTP_PROTOCOL
           + PROTOCOL_POSTFIX
           + job.getJenkinsUrl()
           + COLON
           + job.getJenkinsPort()
           + FORWARD_SLASH
           + CI
           + FORWARD_SLASH;
   String isBuildingUrlUrl = BUSY_EXECUTORS;
   String jsonResponse = getJsonResponse(jenkinsUrl + isBuildingUrlUrl);
   int buidInProgress = Integer.parseInt(jsonResponse);
   S_LOGGER.debug("buidInProgress " + buidInProgress);
   return buidInProgress;
 }
示例#7
0
  private JsonArray getBuildsArray(CIJob job) throws PhrescoException {
    try {
      String jenkinsUrl = "http://" + job.getJenkinsUrl() + ":" + job.getJenkinsPort() + "/ci/";
      String jobNameUtf8 = job.getName().replace(" ", "%20");
      String buildsJsonUrl = jenkinsUrl + "job/" + jobNameUtf8 + "/api/json";
      String jsonResponse = getJsonResponse(buildsJsonUrl);

      JsonParser parser = new JsonParser();
      JsonElement jsonElement = parser.parse(jsonResponse);
      JsonObject jsonObject = jsonElement.getAsJsonObject();
      JsonElement element = jsonObject.get(FrameworkConstants.CI_JOB_JSON_BUILDS);

      JsonArray jsonArray = element.getAsJsonArray();

      return jsonArray;
    } catch (Exception e) {
      throw new PhrescoException(e);
    }
  }