Skip to content
Permalink
Browse files
Convert from Build to Post-Build Step
The build step has now been refactored to be a post-build step.
This will help accomodate job configs where sonar analysis is only
performed as a post build step.

As well, some additional console logging was added.

[JENKINS-31003]
  • Loading branch information
Scott Hebert committed Oct 30, 2015
1 parent 6100e0a commit dcdf88be5f84c62d6db24065a51b18970391c4d7
Showing with 51 additions and 39 deletions.
  1. +24 −16 .../java/org/jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder.java → SonarToGerritPublisher.java}
  2. +5 −1 src/main/resources/messages.properties
  3. 0 ...es/org/jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/config.jelly
  4. 0 ...g/jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/config.properties
  5. 0 .../jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-category.html
  6. 0 ...ci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-changedLinesOnly.html
  7. 0 ...kinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-issueComment.html
  8. 0 .../plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-issuesNotification.html
  9. 0 ...nkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-issuesScore.html
  10. 0 ...insci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-newIssuesOnly.html
  11. 0 ...lugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-noIssuesNotification.html
  12. 0 ...insci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-noIssuesScore.html
  13. 0 .../plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-noIssuesToPostText.html
  14. 0 .../org/jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-path.html
  15. 0 ...jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-postScore.html
  16. 0 ...nkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-projectPath.html
  17. 0 .../jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-severity.html
  18. 0 ...lugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-someIssuesToPostText.html
  19. 0 .../jenkinsci/plugins/sonargerrit/{SonarToGerritBuilder → SonarToGerritPublisher}/help-sonarURL.html
  20. +22 −22 ...g/jenkinsci/plugins/sonargerrit/{SonarToGerritBuilderTest.java → SonarToGerritPublisherTest.java}
@@ -17,6 +17,7 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import com.urswolfer.gerrit.client.rest.GerritAuthData;
import com.urswolfer.gerrit.client.rest.GerritRestApiFactory;

import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
@@ -25,8 +26,10 @@
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;

import org.jenkinsci.plugins.sonargerrit.data.SonarReportBuilder;
import org.jenkinsci.plugins.sonargerrit.data.converter.CustomIssueFormatter;
import org.jenkinsci.plugins.sonargerrit.data.converter.CustomReportFormatter;
@@ -41,6 +44,7 @@

import javax.annotation.Nullable;
import javax.servlet.ServletException;

import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
@@ -56,7 +60,7 @@
* Author: Tatiana Didik
*/

public class SonarToGerritBuilder extends Builder {
public class SonarToGerritPublisher extends Publisher {

private static final String DEFAULT_PATH = "target/sonar/sonar-report.json";
private static final String DEFAULT_SONAR_URL = "http://localhost:9000";
@@ -68,7 +72,7 @@
public static final String GERRIT_FILE_DELIMITER = "/";
public static final String EMPTY_STR = "";

private static final Logger LOGGER = Logger.getLogger(SonarToGerritBuilder.class.getName());
private static final Logger LOGGER = Logger.getLogger(SonarToGerritPublisher.class.getName());
public static final String GERRIT_CHANGE_NUMBER_ENV_VAR_NAME = "GERRIT_CHANGE_NUMBER";
public static final String GERRIT_NAME_ENV_VAR_NAME = "GERRIT_NAME";
public static final String GERRIT_PATCHSET_NUMBER_ENV_VAR_NAME = "GERRIT_PATCHSET_NUMBER";
@@ -92,7 +96,7 @@


@DataBoundConstructor
public SonarToGerritBuilder(String projectPath, String sonarURL, String path,
public SonarToGerritPublisher(String projectPath, String sonarURL, String path,
String severity, boolean changedLinesOnly, boolean newIssuesOnly,
String noIssuesToPostText, String someIssuesToPostText, String issueComment,
boolean postScore, String category, String noIssuesScore, String issuesScore,
@@ -184,15 +188,15 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
InterruptedException {
FilePath reportPath = build.getWorkspace().child(getPath());
if (!reportPath.exists()) {
logError(listener, "jenkins.plugin.error.sonar.report.not.exists", Level.SEVERE, reportPath);
logMessage(listener, "jenkins.plugin.error.sonar.report.not.exists", Level.SEVERE, reportPath);
return false;
}
LOGGER.log(Level.INFO, "Getting Sonar Report from: {0}", reportPath);
logMessage(listener, "jenkins.plugin.getting.report", Level.INFO, reportPath);

SonarReportBuilder builder = new SonarReportBuilder();
String reportJson = reportPath.readToString();
Report report = builder.fromJson(reportJson);
LOGGER.log(Level.INFO, "Report has loaded and contains {0} issues", report.getIssues().size());
logMessage(listener, "jenkins.plugin.report.loaded", Level.INFO, report.getIssues().size());

// Step 1 - Filter issues by issues only predicates
Iterable<Issue> filtered = filterIssuesByPredicates(report);
@@ -208,21 +212,21 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
GerritTrigger trigger = GerritTrigger.getTrigger(build.getProject());
String gerritServerName = gerritNameEnvVar != null ? gerritNameEnvVar : trigger != null ? trigger.getServerName() : null;
if (gerritServerName == null) {
logError(listener, "jenkins.plugin.error.gerrit.server.empty", Level.SEVERE);
logMessage(listener, "jenkins.plugin.error.gerrit.server.empty", Level.SEVERE);
return false;
}
IGerritHudsonTriggerConfig gerritConfig = GerritManagement.getConfig(gerritServerName);
if (gerritConfig == null) {
logError(listener, "jenkins.plugin.error.gerrit.config.empty", Level.SEVERE);
logMessage(listener, "jenkins.plugin.error.gerrit.config.empty", Level.SEVERE);
return false;
}

if (!gerritConfig.isUseRestApi()) {
logError(listener, "jenkins.plugin.error.gerrit.restapi.off", Level.SEVERE);
logMessage(listener, "jenkins.plugin.error.gerrit.restapi.off", Level.SEVERE);
return false;
}
if (gerritConfig.getGerritHttpUserName() == null) {
logError(listener, "jenkins.plugin.error.gerrit.user.empty", Level.SEVERE);
logMessage(listener, "jenkins.plugin.error.gerrit.user.empty", Level.SEVERE);
return false;
}
GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
@@ -233,7 +237,7 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
int changeNumber = Integer.parseInt(getEnvVar(build, listener, GERRIT_CHANGE_NUMBER_ENV_VAR_NAME));
int patchSetNumber = Integer.parseInt(getEnvVar(build, listener, GERRIT_PATCHSET_NUMBER_ENV_VAR_NAME));
RevisionApi revision = gerritApi.changes().id(changeNumber).revision(patchSetNumber);
LOGGER.log(Level.INFO, "Connected to Gerrit: server name: {0}. Change Number: {1}, PatchSetNumber: {2}", new Object[]{gerritServerName, changeNumber, patchSetNumber});
logMessage(listener, "jenkins.plugin.connected.to.gerrit", Level.INFO, new Object[]{gerritServerName, changeNumber, patchSetNumber});

// Step 4 - Filter issues by changed files
final Map<String, FileInfo> files = revision.files();
@@ -257,7 +261,7 @@ public boolean apply(@Nullable String input) {

// Step 7 - Post review
revision.review(reviewInput);
LOGGER.log(Level.INFO, "Review has been sent");
logMessage(listener, "jenkins.plugin.review.sent", Level.INFO);
} catch (RestApiException e) {
listener.getLogger().println("Unable to post review: " + e.getMessage());
LOGGER.severe("Unable to post review: " + e.getMessage());
@@ -272,7 +276,7 @@ private String getEnvVar(AbstractBuild build, BuildListener listener, String nam
return envVars.get(name);
}

private void logError(BuildListener listener, String message, Level l, Object... params) {
private void logMessage(BuildListener listener, String message, Level l, Object... params) {
message = getLocalized(message, params);
listener.getLogger().println(message);
LOGGER.log(l, message);
@@ -441,15 +445,15 @@ public DescriptorImpl getDescriptor() {
}

/**
* Descriptor for {@link SonarToGerritBuilder}. Used as a singleton.
* Descriptor for {@link SonarToGerritPublisher}. Used as a singleton.
* The class is marked as public so that it can be accessed from views.
* <p/>
* <p/>
* See <tt>src/main/resources/hudson/plugins/hello_world/SonarToGerritBuilder/*.jelly</tt>
* for the actual HTML fragment for the configuration screen.
*/
@Extension // This indicates to Jenkins that this is an implementation of an extension point.
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {

/**
* In order to load the persisted global configuration, you have to
@@ -667,5 +671,9 @@ public String getDisplayName() {
}
}

@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
}

@@ -19,4 +19,8 @@ jenkins.plugin.validation.review.severity.unknown=Unknown severity
jenkins.plugin.validation.review.category.unknown=Unknown category
jenkins.plugin.validation.review.score.not.numeric=Score should be numeric
jenkins.plugin.error.gerrit.restapi.off=RestAPI in Gerrit-Trigger settings must be enabled
jenkins.plugin.validation.review.notification.recipient.unknown=Notification recipient is unknown
jenkins.plugin.validation.review.notification.recipient.unknown=Notification recipient is unknown
jenkins.plugin.review.sent=Review has been sent
jenkins.plugin.connected.to.gerrit=Connected to Gerrit: server name: %s. Change Number: %s, PatchSetNumber: %s
jenkins.plugin.report.loaded=Report has loaded and contains %s issues
jenkins.plugin.getting.report=Getting Sonar Report from: %s
Loading

0 comments on commit dcdf88b

Please sign in to comment.