Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target/
.gradle
local.properties
**build
/bin/
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ dependencies {
jenkinsPlugins 'org.jenkins-ci.plugins:credentials:1.22@jar'

optionalJenkinsPlugins 'org.jenkins-ci.plugins:junit:1.6@jar'
optionalJenkinsPlugins 'org.jenkins-ci.plugins:cobertura:1.9.6@jar'
optionalJenkinsPlugins 'org.jenkins-ci.plugins:cobertura:1.11@jar'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? Can we decouple this change?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Answered in #185 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good


testCompile 'junit:junit:4.12'
testCompile 'org.apache.httpcomponents:httpclient:4.3:tests'
Expand Down
15 changes: 15 additions & 0 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,18 @@ avoid confusion. Please note that builds on the same diff triggered by the same

Also note that if you pass additional arguments to your harbormaster request they may need to be included in the `ABORT_ON_REVISION_ID` field as well. A good example is when you use the same CI job to build multiple targets on a single diff. So for example, if the jenkins request params have
`TARGET=some_target`, then to ensure other targets are not cancelled for the same diff, you may want to set `ABORT_ON_REVISION_ID=some_target_${buildable.revision}`.

Pipeline
--------

Typically the Phabricator Notifier is used as a reporting step in a Jenkins Pipeline. The test result collector step must be run before the Notifier.

```groovy
stage ('report') {
//...
// junit()
step([$class: 'PhabricatorNotifier', commentOnSuccess: true, commentWithConsoleLinkOnFailure: true])
//...
}

```
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import com.uber.jenkins.phabricator.utils.CommonUtils;
import com.uber.jenkins.phabricator.utils.Logger;
import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Result;
import hudson.model.Run;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

Expand All @@ -59,15 +59,15 @@ public class BuildResultProcessor {
private final String buildUrl;
private final boolean runHarbormaster;
private final FilePath workspace;
private final AbstractBuild build;
private final Run<?, ?> build;
private String commentAction;
private final CommentBuilder commenter;
private UnitResults unitResults;
private Map<String, String> harbormasterCoverage;
private LintResults lintResults;

public BuildResultProcessor(
Logger logger, AbstractBuild build, Differential diff, DifferentialClient diffClient,
Logger logger, Run<?, ?> build, FilePath workspace, Differential diff, DifferentialClient diffClient,
String phid, CodeCoverageMetrics coverageResult, String buildUrl, boolean preserveFormatting,
CoverageCheckSettings coverageCheckSettings) {
this.logger = logger;
Expand All @@ -76,7 +76,7 @@ public BuildResultProcessor(
this.phid = phid;
this.buildUrl = buildUrl;
this.build = build;
this.workspace = build.getWorkspace();
this.workspace = workspace;

this.commentAction = "none";
this.commenter = new CommentBuilder(logger, build.getResult(), coverageResult, buildUrl, preserveFormatting,
Expand All @@ -85,7 +85,11 @@ public BuildResultProcessor(
}

public Result getBuildResult() {
return this.build.getResult();
if (this.build.getResult() == null) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain why we would want to return success on null? It seems a bit counterintuitive. Are there any api docs defining this new behavior?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on oldschool freestyle jobs after the build reached the last build step it set the build result to success, but in pipeline there is no definite end of build blocks, so it won't explicitly set SUCCESS, hence the previous workaround, where it was needed to run this before the notifier:

currentBuild.result = currentBuild.result ?: 'SUCCESS'

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The phab notifier build step need not necessarily be the last step. If someone uses pipeline and not this plugin, they would have to workaround this anyways, so this logic should not be in this plugin. This should be reported upstream to jenkins.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

null == success so far. IMHO it was a genuine missing null check in the plugin code. In freestyle it is always set and in pipeline it is always considered SUCCESS.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

return Result.SUCCESS;
} else {
return this.build.getResult();
}
}

/**
Expand Down
51 changes: 33 additions & 18 deletions src/main/java/com/uber/jenkins/phabricator/PhabricatorNotifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@
import com.uber.jenkins.phabricator.unit.UnitTestProvider;
import com.uber.jenkins.phabricator.utils.CommonUtils;
import com.uber.jenkins.phabricator.utils.Logger;

import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import jenkins.model.CauseOfInterruption;
import jenkins.model.InterruptedBuildAction;
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildStep;

import org.apache.commons.io.FilenameUtils;
import org.kohsuke.stapler.DataBoundConstructor;

Expand All @@ -55,7 +60,7 @@
import java.util.List;
import java.util.Set;

public class PhabricatorNotifier extends Notifier {
public class PhabricatorNotifier extends Notifier implements SimpleBuildStep {
public static final String COBERTURA_CLASS_NAME = "com.uber.jenkins.phabricator.coverage.CoberturaCoverageProvider";

private static final String JUNIT_PLUGIN_NAME = "junit";
Expand Down Expand Up @@ -105,8 +110,8 @@ public BuildStepMonitor getRequiredMonitorService() {
}

@Override
public final boolean perform(final AbstractBuild<?, ?> build, final Launcher launcher,
final BuildListener listener) throws InterruptedException, IOException {
public final void perform(final Run<?, ?> build, FilePath workspace, final Launcher launcher,
final TaskListener listener) throws InterruptedException, IOException {
EnvVars environment = build.getEnvironment(listener);
Logger logger = new Logger(listener.getLogger());

Expand All @@ -128,7 +133,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
if (cause instanceof PhabricatorCauseOfInterruption) {
logger.warn(ABORT_TAG, "Skipping notification step since this build was interrupted"
+ " by a newer build with the same differential revision");
return true;
return;
}
}
}
Expand All @@ -144,7 +149,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
build.addAction(PhabricatorPostbuildAction.createShortText(branch, null));
}

coverageProvider = getCoverageProvider(build, listener, Collections.<String>emptySet());
coverageProvider = getCoverageProvider(build, workspace, listener, Collections.<String>emptySet());
CodeCoverageMetrics coverageResult = null;
if (coverageProvider != null) {
coverageResult = coverageProvider.getMetrics();
Expand All @@ -160,7 +165,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau

// Ignore the result.
nonDifferentialBuildTask.run();
return true;
return;
}

ConduitAPIClient conduitClient;
Expand All @@ -169,7 +174,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
} catch (ConduitAPIException e) {
e.printStackTrace(logger.getStream());
logger.warn(CONDUIT_TAG, e.getMessage());
return false;
throw new AbortException();
}

final String buildUrl = environment.get("BUILD_URL");
Expand All @@ -183,7 +188,11 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
build.getResult(),
buildUrl
).run();
return result == Task.Result.SUCCESS;
if (result == Task.Result.SUCCESS) {
return;
} else {
throw new AbortException();
}
}

DifferentialClient diffClient = new DifferentialClient(diffID, conduitClient);
Expand All @@ -194,7 +203,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
e.printStackTrace(logger.getStream());
logger.warn(CONDUIT_TAG, "Unable to fetch differential from Conduit API");
logger.warn(CONDUIT_TAG, e.getMessage());
return true;
return;
}

if (needsDecoration) {
Expand All @@ -206,7 +215,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
includeFileNames.add(FilenameUtils.getName(file));
}

coverageProvider = getCoverageProvider(build, listener, includeFileNames);
coverageProvider = getCoverageProvider(build, workspace, listener, includeFileNames);
CodeCoverageMetrics coverageResult = null;
if (coverageProvider != null) {
coverageResult = coverageProvider.getMetrics();
Expand All @@ -215,6 +224,7 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau
BuildResultProcessor resultProcessor = new BuildResultProcessor(
logger,
build,
workspace,
diff,
diffClient,
environment.get(PhabricatorPlugin.PHID_FIELD),
Expand Down Expand Up @@ -247,14 +257,12 @@ public final boolean perform(final AbstractBuild<?, ?> build, final Launcher lau

// Fail the build if we can't report to Harbormaster
if (!resultProcessor.processHarbormaster()) {
return false;
throw new AbortException();
}

resultProcessor.processRemoteComment(commentFile, commentSize);

resultProcessor.sendComment(commentWithConsoleLinkOnFailure);

return true;
}

protected UberallsClient getUberallsClient(Logger logger, String gitUrl, String branch) {
Expand Down Expand Up @@ -291,9 +299,15 @@ private ConduitAPIClient getConduitClient(Job owner) throws ConduitAPIException
* @param listener The build listener
* @return The current cobertura coverage, if any
*/
private CoverageProvider getCoverageProvider(AbstractBuild build, BuildListener listener,
private CoverageProvider getCoverageProvider(Run<?, ?> build, FilePath workspace, TaskListener listener,
Set<String> includeFileNames) {
if (!build.getResult().isBetterOrEqualTo(Result.UNSTABLE)) {
Result buildResult = null;
if (build.getResult() == null) {
buildResult = Result.SUCCESS;
} else {
buildResult = build.getResult();
}
if (!buildResult.isBetterOrEqualTo(Result.UNSTABLE)) {
return null;
}

Expand All @@ -311,6 +325,7 @@ private CoverageProvider getCoverageProvider(AbstractBuild build, BuildListener
}

coverage.setBuild(build);
coverage.setWorkspace(workspace);
coverage.setIncludeFileNames(includeFileNames);
coverage.setCoverageReportPattern(coverageReportPattern);
if (coverage.hasCoverage()) {
Expand All @@ -321,7 +336,7 @@ private CoverageProvider getCoverageProvider(AbstractBuild build, BuildListener
}
}

private UnitTestProvider getUnitProvider(AbstractBuild build, BuildListener listener) {
private UnitTestProvider getUnitProvider(Run<?, ?> build, TaskListener listener) {
Logger logger = new Logger(listener.getLogger());

InstanceProvider<UnitTestProvider> provider = new InstanceProvider<UnitTestProvider>(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@
import com.uber.jenkins.phabricator.PhabricatorPostbuildAction;
import com.uber.jenkins.phabricator.PhabricatorPostbuildSummaryAction;

import hudson.EnvVars;
import hudson.model.AbstractBuild;

import hudson.model.Run;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Differential {
Expand Down Expand Up @@ -78,7 +74,7 @@ public String getPhabricatorLink(String phabricatorURL) {
}
}

public void decorate(AbstractBuild build, String phabricatorURL) {
public void decorate(Run<?, ?> build, String phabricatorURL) {
// Add a badge next to the build
build.addAction(PhabricatorPostbuildAction.createShortText(
getRevisionID(true),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import hudson.FilePath;
import hudson.model.AbstractBuild;
import hudson.model.Run;
import hudson.plugins.cobertura.Ratio;
import hudson.plugins.cobertura.targets.CoverageMetric;
import hudson.plugins.cobertura.targets.CoverageResult;
Expand Down Expand Up @@ -96,7 +97,8 @@ Map<String, List<Integer>> parseReports(CoberturaXMLParser parser, File[] report
}

private void computeCoverage() {
AbstractBuild build = getBuild();
Run<?, ?> build = getBuild();
FilePath workspace = getWorkspace();
if (build == null) {
mHasComputedCoverage = true;
return;
Expand All @@ -114,7 +116,7 @@ private void computeCoverage() {
}

// Fallback to scanning for the reports
copyCoverageToJenkinsMaster(build);
copyCoverageToJenkinsMaster(build, workspace);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just inline build.getWorkspace() here and avoid the local workspace variable

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the new Jenkins API you can't use Build, but must use Run, but the run interface won't provide a getWorkspace method. I can change this call to this:

copyCoverageToJenkinsMaster(build, getWorkspace());

but this is the best I can do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets leave it as is then

File[] reports = getCoberturaReports(build);
CoverageResult result = null;
if (reports != null) {
Expand Down Expand Up @@ -142,17 +144,14 @@ private void computeCoverage() {
}

private void computeLineCoverage() {
FilePath workspace = getBuild().getWorkspace();
FilePath workspace = getWorkspace();
File[] reports = getCoberturaReports(getBuild());
CoberturaXMLParser parser = new CoberturaXMLParser(workspace, getIncludeFileNames());
mLineCoverage = parseReports(parser, reports);
}

private void copyCoverageToJenkinsMaster(AbstractBuild build) {
final FilePath[] moduleRoots = build.getModuleRoots();
final boolean multipleModuleRoots =
moduleRoots != null && moduleRoots.length > 1;
final FilePath moduleRoot = multipleModuleRoots ? build.getWorkspace() : build.getModuleRoot();
private void copyCoverageToJenkinsMaster(Run<?, ?> build, FilePath workspace) {
final FilePath moduleRoot = workspace;
final File buildCoberturaDir = build.getRootDir();
FilePath buildTarget = new FilePath(buildCoberturaDir);

Expand Down Expand Up @@ -225,7 +224,7 @@ private static float getCoveragePercentage(CoverageResult result, CoverageMetric
return ratio.getPercentageFloat();
}

private File[] getCoberturaReports(AbstractBuild build) {
private File[] getCoberturaReports(Run<?, ?> build) {
return build.getRootDir().listFiles(CoberturaPublisher.COBERTURA_FILENAME_FILTER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

package com.uber.jenkins.phabricator.coverage;

import hudson.model.AbstractBuild;
import hudson.FilePath;
import hudson.model.Run;

import java.util.List;
import java.util.Map;
import java.util.Set;

public abstract class CoverageProvider {
private AbstractBuild<?, ?> build;
private Run<?, ?> build;
private FilePath workspace;
private Set<String> includeFileNames;
private String coverageReportPattern;

Expand All @@ -47,14 +49,22 @@ protected Set<String> getIncludeFileNames() {
* Set the owning build for this provider
* @param build The build that is associated with the current run
*/
public void setBuild(AbstractBuild<?, ?> build) {
public void setBuild(Run<?, ?> build) {
this.build = build;
}

protected AbstractBuild getBuild() {
protected Run<?, ?> getBuild() {
return build;
}

public void setWorkspace(FilePath workspace) {
this.workspace = workspace;
}

protected FilePath getWorkspace() {
return workspace;
}

/**
* Set the coverage report pattern to scan for
* @param coverageReportPattern The coverage report pattern to scan for
Expand Down
Loading