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); } }