@Override public void run() { ReviewInput reviewInput = createReview(); String reviewEndpoint = resolveEndpointURL(); HttpPost httpPost = createHttpPostEntity(reviewInput, reviewEndpoint); if (httpPost == null) { return; } DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient .getCredentialsProvider() .setCredentials( new AuthScope(null, -1), new UsernamePasswordCredentials( config.getGerritHttpUserName(), config.getGerritHttpPassword())); try { HttpResponse httpResponse = httpclient.execute(httpPost); String response = IOUtils.toString(httpResponse.getEntity().getContent(), "UTF-8"); listener.getLogger().print(response); if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { listener.error(httpResponse.getStatusLine().getReasonPhrase()); } } catch (Exception e) { listener.error("Failed to submit result", e); } }
/** * Get the front end url from a ChangeBasedEvent. * * @param event the event * @param project the project for which the parameters are being set * @return the front end url */ private static String getURL(ChangeBasedEvent event, AbstractProject project) { String url = ""; String serverName = null; // Figure out what serverName to use if (event.getProvider() != null) { serverName = event.getProvider().getName(); } else if (project != null) { String name = GerritTrigger.getTrigger(project).getServerName(); if (!GerritServer.ANY_SERVER.equals(name)) { serverName = name; } } if (serverName == null && PluginImpl.getInstance().getFirstServer() != null) { logger.warn( "No server could be determined from event or project config, " + "defaulting to the first configured server. Event: [{}] Project: [{}]", event, project); serverName = PluginImpl.getInstance().getFirstServer().getName(); } else if (serverName == null) { // We have exhausted all possibilities, time to fail horribly throw new IllegalStateException( "Cannot determine a Gerrit server to link to. Have you configured one?"); } GerritServer server = PluginImpl.getInstance().getServer(serverName); if (server != null) { IGerritHudsonTriggerConfig config = server.getConfig(); if (config != null) { url = config.getGerritFrontEndUrlFor(event); } else { logger.error("Could not find config for Gerrit server {}", serverName); } } else { logger.error("Could not find Gerrit server {}", serverName); } return url; }
/** * What it says resolve Endpoint URL. * * @return the url. */ private String resolveEndpointURL() { String gerritFrontEndUrl = config.getGerritFrontEndUrl(); if (!gerritFrontEndUrl.endsWith("/")) { gerritFrontEndUrl = gerritFrontEndUrl + "/"; } ChangeId changeId = new ChangeId( event.getChange().getProject(), event.getChange().getBranch(), event.getChange().getId()); return gerritFrontEndUrl + "a/changes/" + changeId.asUrlPart() + "/revisions/" + event.getPatchSet().getRevision() + "/review"; }