Skip to content
Permalink
Browse files

Merge pull request #19 from aquarellian/master

JENKINS-43397 Initial support for pipelines
  • Loading branch information...
aquarellian committed Apr 24, 2017
2 parents d5fe098 + ec3fdb1 commit 4b790081ed485b485e56a9d523eae6066ff318b9
Showing with 47 additions and 42 deletions.
  1. +5 −0 pom.xml
  2. +42 −42 src/main/java/org/jenkinsci/plugins/sonargerrit/SonarToGerritPublisher.java
@@ -138,6 +138,11 @@
</build>

<dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>com.sonyericsson.hudson.plugins.gerrit</groupId>
<artifactId>gerrit-trigger</artifactId>
@@ -20,17 +20,14 @@
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;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.*;
import hudson.model.*;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import jenkins.tasks.SimpleBuildStep;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.sonargerrit.data.ComponentPathBuilder;
import org.jenkinsci.plugins.sonargerrit.data.SonarReportBuilder;
import org.jenkinsci.plugins.sonargerrit.data.converter.CustomIssueFormatter;
@@ -43,6 +40,7 @@
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.servlet.ServletException;
import java.io.IOException;
@@ -58,8 +56,7 @@
* Project: Sonar-Gerrit Plugin
* Author: Tatiana Didik
*/

public class SonarToGerritPublisher extends Publisher {
public class SonarToGerritPublisher extends Publisher implements SimpleBuildStep {

private static final String DEFAULT_SONAR_REPORT_PATH = "target/sonar/sonar-report.json";
private static final String DEFAULT_PROJECT_PATH = "";
@@ -217,39 +214,32 @@ public String getIssuesScore() {
}

@Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException,
InterruptedException {

List<ReportInfo> issueInfos = readSonarReports(listener, build.getWorkspace());
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
List<ReportInfo> issueInfos = readSonarReports(listener, filePath);
if (issueInfos == null) {
logMessage(listener, "jenkins.plugin.validation.path.no.project.config.available", Level.SEVERE);
return false;
throw new AbortException(getLocalized("jenkins.plugin.validation.path.no.project.config.available"));
}

Multimap<String, Issue> file2issues = generateFilenameToIssuesMapFilteredByPredicates(issueInfos);

// Step 3 - Prepare Gerrit REST API client
// Check Gerrit configuration is available
String gerritNameEnvVar = getEnvVar(build, listener, GERRIT_NAME_ENV_VAR_NAME);
GerritTrigger trigger = GerritTrigger.getTrigger(build.getProject());
String gerritNameEnvVar = getEnvVar(run, listener, GERRIT_NAME_ENV_VAR_NAME);
GerritTrigger trigger = GerritTrigger.getTrigger(run.getParent());
String gerritServerName = gerritNameEnvVar != null ? gerritNameEnvVar : trigger != null ? trigger.getServerName() : null;
if (gerritServerName == null) {
logMessage(listener, "jenkins.plugin.error.gerrit.server.empty", Level.SEVERE);
return false;
throw new AbortException(getLocalized("jenkins.plugin.error.gerrit.server.empty"));
}
IGerritHudsonTriggerConfig gerritConfig = GerritManagement.getConfig(gerritServerName);
if (gerritConfig == null) {
logMessage(listener, "jenkins.plugin.error.gerrit.config.empty", Level.SEVERE);
return false;
throw new AbortException(getLocalized("jenkins.plugin.error.gerrit.config.empty"));
}

if (!gerritConfig.isUseRestApi()) {
logMessage(listener, "jenkins.plugin.error.gerrit.restapi.off", Level.SEVERE);
return false;
throw new AbortException(getLocalized("jenkins.plugin.error.gerrit.restapi.off"));
}
if (gerritConfig.getGerritHttpUserName() == null) {
logMessage(listener, "jenkins.plugin.error.gerrit.user.empty", Level.SEVERE);
return false;
throw new AbortException(getLocalized("jenkins.plugin.error.gerrit.user.empty"));
}
GerritRestApiFactory gerritRestApiFactory = new GerritRestApiFactory();
GerritAuthData.Basic authData = new GerritAuthData.Basic(
@@ -259,17 +249,15 @@ public boolean perform(AbstractBuild build, Launcher launcher, BuildListener lis
gerritConfig.isUseRestApi());
GerritApi gerritApi = gerritRestApiFactory.create(authData);
try {
String changeNStr = getEnvVar(build, listener, GERRIT_CHANGE_NUMBER_ENV_VAR_NAME);
String changeNStr = getEnvVar(run, listener, GERRIT_CHANGE_NUMBER_ENV_VAR_NAME);
if (changeNStr == null) {
logMessage(listener, "jenkins.plugin.error.gerrit.change.number.empty", Level.SEVERE);
return false;
throw new AbortException(getLocalized("jenkins.plugin.error.gerrit.change.number.empty"));
}
int changeNumber = Integer.parseInt(changeNStr);

String patchsetNStr = getEnvVar(build, listener, GERRIT_PATCHSET_NUMBER_ENV_VAR_NAME);
String patchsetNStr = getEnvVar(run, listener, GERRIT_PATCHSET_NUMBER_ENV_VAR_NAME);
if (patchsetNStr == null) {
logMessage(listener, "jenkins.plugin.error.gerrit.patchset.number.empty", Level.SEVERE);
return false;
throw new AbortException(getLocalized("jenkins.plugin.error.gerrit.patchset.number.empty"));
}
int patchSetNumber = Integer.parseInt(patchsetNStr);

@@ -300,12 +288,9 @@ public boolean apply(@Nullable String input) {
revision.review(reviewInput);
logMessage(listener, "jenkins.plugin.review.sent", Level.INFO);
} catch (RestApiException e) {
listener.getLogger().println("Unable to post review: " + e.getMessage());
LOGGER.log(Level.SEVERE, "Unable to post review: " + e.getMessage(), e);
return false;
throw new AbortException("Unable to post review: " + e.getMessage());
}

return true;
}

@VisibleForTesting
@@ -324,7 +309,7 @@ public boolean apply(@Nullable String input) {
return file2issues;
}

private Report readSonarReport(BuildListener listener, FilePath workspace, SubJobConfig config) throws IOException,
private Report readSonarReport(TaskListener listener, FilePath workspace, SubJobConfig config) throws IOException,
InterruptedException {
FilePath reportPath = workspace.child(config.getSonarReportPath());
if (!reportPath.exists()) {
@@ -341,7 +326,7 @@ private Report readSonarReport(BuildListener listener, FilePath workspace, SubJo
}

@VisibleForTesting
List<ReportInfo> readSonarReports(BuildListener listener, FilePath workspace) throws IOException,
List<ReportInfo> readSonarReports(TaskListener listener, FilePath workspace) throws IOException,
InterruptedException {
List<ReportInfo> reports = new ArrayList<ReportInfo>();
for (SubJobConfig subJobConfig : getSubJobConfigs(false)) { // to be replaced by this.subJobConfigs in further releases - this code is to support older versions
@@ -354,12 +339,26 @@ private Report readSonarReport(BuildListener listener, FilePath workspace, SubJo
return reports;
}

private String getEnvVar(AbstractBuild build, BuildListener listener, String name) throws IOException, InterruptedException {
private String getEnvVar(Run<?, ?> build, TaskListener listener, String name) throws IOException, InterruptedException {
EnvVars envVars = build.getEnvironment(listener);
return envVars.get(name);
String value = envVars.get(name);
// due to JENKINS-30910 old versions of workflow-job-plugin do not have code copying ParameterAction values to Environment Variables in pipeline jobs.
if (value == null) {
ParametersAction action = build.getAction(ParametersAction.class);
if (action != null) {
ParameterValue parameter = action.getParameter(name);
if (parameter != null) {
Object parameterValue = parameter.getValue();
if (parameterValue != null) {
value = parameterValue.toString();
}
}
}
}
return value;
}

private void logMessage(BuildListener listener, String message, Level l, Object... params) {
private void logMessage(TaskListener listener, String message, Level l, Object... params) {
message = getLocalized(message, params);
if (listener != null) { // it can be it tests
listener.getLogger().println(message);
@@ -523,6 +522,7 @@ public ReportInfo(String directoryPath, Report report) {
* See <tt>src/main/resources/hudson/plugins/hello_world/SonarToGerritBuilder/*.jelly</tt>
* for the actual HTML fragment for the configuration screen.
*/
@Symbol("sonarToGerrit")
@Extension // This indicates to Jenkins that this is an implementation of an extension point.
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {

@@ -584,8 +584,8 @@ public FormValidation doTestConnection(@QueryParameter("httpUsername") final Str

}

public List<String> getGerritServerNames(){
return PluginImpl.getServerNames_();
public List<String> getGerritServerNames() {
return PluginImpl.getServerNames_();
}

/**

0 comments on commit 4b79008

Please sign in to comment.
You can’t perform that action at this time.