예제 #1
0
  private void generateStatusJSON(
      String commitSHA1, Job project, StaplerRequest req, StaplerResponse rsp) {
    SCMTriggerItem item = SCMTriggerItems.asSCMTriggerItem(project);
    GitSCM gitSCM = getGitSCM(item);

    if (gitSCM == null) {
      throw new IllegalArgumentException("This repo does not use git.");
    }

    Run mainBuild = this.getBuildBySHA1(project, commitSHA1, true);

    JSONObject object = new JSONObject();
    object.put("sha", commitSHA1);

    if (mainBuild == null) {
      try {
        object.put("status", "pending");
        this.writeJSON(rsp, object);
        return;
      } catch (IOException e) {
        throw HttpResponses.error(500, "Could not generate response.");
      }
    }

    object.put("id", mainBuild.getNumber());

    Result res = mainBuild.getResult();

    // TODO: add status of pending when we figure it out.
    if (mainBuild.isBuilding()) {
      object.put("status", "running");
    } else if (res == Result.SUCCESS) {
      object.put("status", "success");
    } else {
      object.put("status", "failed");
    }

    try {
      this.writeJSON(rsp, object);
    } catch (IOException e) {
      throw HttpResponses.error(500, "Could not generate response.");
    }
  }
예제 #2
0
  private void generateStatusPNG(
      String branch,
      String commitSHA1,
      Job project,
      final StaplerRequest req,
      final StaplerResponse rsp)
      throws ServletException, IOException {
    SCMTriggerItem item = SCMTriggerItems.asSCMTriggerItem(project);
    GitSCM gitSCM = getGitSCM(item);

    if (gitSCM == null) {
      throw new IllegalArgumentException("This repo does not use git.");
    }

    Run mainBuild = null;

    if (branch != null) {
      mainBuild = this.getBuildByBranch(project, branch);
    } else if (commitSHA1 != null) {
      mainBuild = this.getBuildBySHA1(project, commitSHA1, false);
    }

    String baseUrl = Jenkins.getInstance().getRootUrl();
    // Remove trailing slash
    if (baseUrl.endsWith("/")) {
      baseUrl = baseUrl.substring(0, baseUrl.length() - 1);
    }
    String imageUrl = "images/unknown.png";
    if (null != mainBuild) {
      Result res = mainBuild.getResult();
      if (mainBuild.isBuilding()) {
        imageUrl = "images/running.png";
      } else if (res == Result.SUCCESS) {
        imageUrl = "images/success.png";
      } else if (res == Result.FAILURE) {
        imageUrl = "images/failed.png";
      } else if (res == Result.UNSTABLE) {
        imageUrl = "images/unstable.png";
      } else {
        imageUrl = "images/unknown.png";
      }
    }
    Authentication old = SecurityContextHolder.getContext().getAuthentication();
    SecurityContextHolder.getContext().setAuthentication(ACL.SYSTEM);
    try {
      URL resourceUrl =
          new URL(
              Jenkins.getInstance().getPlugin("gitlab-plugin").getWrapper().baseResourceURL
                  + imageUrl);
      LOGGER.info("serving image " + resourceUrl.toExternalForm());
      rsp.setHeader("Expires", "Fri, 01 Jan 1984 00:00:00 GMT");
      rsp.setHeader("Cache-Control", "no-cache, private");
      rsp.setHeader("Content-Type", "image/png");
      hudson.util.IOUtils.copy(new File(resourceUrl.toURI()), rsp.getOutputStream());
      rsp.flushBuffer();
    } catch (Exception e) {
      throw HttpResponses.error(500, "Could not generate response.");
    } finally {
      SecurityContextHolder.getContext().setAuthentication(old);
    }
  }