Skip to content

Commit

Permalink
Merge pull request #19 from aquarellian/master
Browse files Browse the repository at this point in the history
JENKINS-43397 Initial support for pipelines
  • Loading branch information
aquarellian committed Apr 24, 2017
2 parents d5fe098 + ec3fdb1 commit 4b79008
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@
</build> </build>


<dependencies> <dependencies>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>structs</artifactId>
<version>1.2</version>
</dependency>
<dependency> <dependency>
<groupId>com.sonyericsson.hudson.plugins.gerrit</groupId> <groupId>com.sonyericsson.hudson.plugins.gerrit</groupId>
<artifactId>gerrit-trigger</artifactId> <artifactId>gerrit-trigger</artifactId>
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,17 +20,14 @@
import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger; import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritTrigger;
import com.urswolfer.gerrit.client.rest.GerritAuthData; import com.urswolfer.gerrit.client.rest.GerritAuthData;
import com.urswolfer.gerrit.client.rest.GerritRestApiFactory; import com.urswolfer.gerrit.client.rest.GerritRestApiFactory;
import hudson.EnvVars; import hudson.*;
import hudson.Extension; import hudson.model.*;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor; import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Publisher; import hudson.tasks.Publisher;
import hudson.util.FormValidation; 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.ComponentPathBuilder;
import org.jenkinsci.plugins.sonargerrit.data.SonarReportBuilder; import org.jenkinsci.plugins.sonargerrit.data.SonarReportBuilder;
import org.jenkinsci.plugins.sonargerrit.data.converter.CustomIssueFormatter; import org.jenkinsci.plugins.sonargerrit.data.converter.CustomIssueFormatter;
Expand All @@ -43,6 +40,7 @@
import org.kohsuke.stapler.DataBoundConstructor; import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter; import org.kohsuke.stapler.QueryParameter;


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

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


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


@Override @Override
public boolean perform(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException {
InterruptedException { List<ReportInfo> issueInfos = readSonarReports(listener, filePath);

List<ReportInfo> issueInfos = readSonarReports(listener, build.getWorkspace());
if (issueInfos == null) { if (issueInfos == null) {
logMessage(listener, "jenkins.plugin.validation.path.no.project.config.available", Level.SEVERE); throw new AbortException(getLocalized("jenkins.plugin.validation.path.no.project.config.available"));
return false;
} }


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


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


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


Expand Down Expand Up @@ -300,12 +288,9 @@ public boolean apply(@Nullable String input) {
revision.review(reviewInput); revision.review(reviewInput);
logMessage(listener, "jenkins.plugin.review.sent", Level.INFO); logMessage(listener, "jenkins.plugin.review.sent", Level.INFO);
} catch (RestApiException e) { } catch (RestApiException e) {
listener.getLogger().println("Unable to post review: " + e.getMessage());
LOGGER.log(Level.SEVERE, "Unable to post review: " + e.getMessage(), e); 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 @VisibleForTesting
Expand All @@ -324,7 +309,7 @@ Multimap<String, Issue> generateFilenameToIssuesMapFilteredByPredicates(List<Rep
return file2issues; 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 { InterruptedException {
FilePath reportPath = workspace.child(config.getSonarReportPath()); FilePath reportPath = workspace.child(config.getSonarReportPath());
if (!reportPath.exists()) { if (!reportPath.exists()) {
Expand All @@ -341,7 +326,7 @@ private Report readSonarReport(BuildListener listener, FilePath workspace, SubJo
} }


@VisibleForTesting @VisibleForTesting
List<ReportInfo> readSonarReports(BuildListener listener, FilePath workspace) throws IOException, List<ReportInfo> readSonarReports(TaskListener listener, FilePath workspace) throws IOException,
InterruptedException { InterruptedException {
List<ReportInfo> reports = new ArrayList<ReportInfo>(); 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 for (SubJobConfig subJobConfig : getSubJobConfigs(false)) { // to be replaced by this.subJobConfigs in further releases - this code is to support older versions
Expand All @@ -354,12 +339,26 @@ List<ReportInfo> readSonarReports(BuildListener listener, FilePath workspace) th
return reports; 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); 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); message = getLocalized(message, params);
if (listener != null) { // it can be it tests if (listener != null) { // it can be it tests
listener.getLogger().println(message); listener.getLogger().println(message);
Expand Down Expand Up @@ -523,6 +522,7 @@ public ReportInfo(String directoryPath, Report report) {
* See <tt>src/main/resources/hudson/plugins/hello_world/SonarToGerritBuilder/*.jelly</tt> * See <tt>src/main/resources/hudson/plugins/hello_world/SonarToGerritBuilder/*.jelly</tt>
* for the actual HTML fragment for the configuration screen. * 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. @Extension // This indicates to Jenkins that this is an implementation of an extension point.
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> { public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {


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


} }


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


/** /**
Expand Down

0 comments on commit 4b79008

Please sign in to comment.