Skip to content

Commit

Permalink
Merge aec9887 into e4d7c90
Browse files Browse the repository at this point in the history
  • Loading branch information
kageiit committed Oct 19, 2018
2 parents e4d7c90 + aec9887 commit ffa688c
Show file tree
Hide file tree
Showing 38 changed files with 1,427 additions and 1,829 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Expand Up @@ -4,9 +4,6 @@ sudo: true
matrix:
fast_finish: true
include:
- jdk: openjdk7
env: TASKS="check"
dist: precise
- jdk: oraclejdk8
env: TASKS="check"
- jdk: oraclejdk8
Expand Down
6 changes: 2 additions & 4 deletions build.gradle
Expand Up @@ -11,8 +11,8 @@ archivesBaseName = 'phabricator-plugin'
version = '1.9.8-SNAPSHOT'
description = 'Integrate Jenkins with Phabricator Differentials and Uberalls'

sourceCompatibility = 1.6
targetCompatibility = 1.6
sourceCompatibility = 1.8
targetCompatibility = 1.8

tasks.withType(JavaCompile) {
options.encoding = 'UTF-8'
Expand Down Expand Up @@ -47,8 +47,6 @@ 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.11@jar'
optionalJenkinsPlugins 'org.jenkins-ci.plugins:jacoco:2.2.1@jar'
providedCompile 'org.jacoco:org.jacoco.report:0.7.8'

testCompile 'junit:junit:4.12'
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Expand Up @@ -78,7 +78,7 @@ Jenkins Job
2. Click the **Add post-build action** button and select **Post to Phabricator**.
3. Make sure the **Comment on Success** and **Comment with console link on Failure** checkboxes are selected.
4. Optionally:
1. If you have [Uberalls](https://github.com/uber/uberalls) enabled, enter a path to scan for Cobertura reports.
1. If you have [Uberalls](https://github.com/uber/uberalls) enabled, enter a path to scan for Coverage reports.
2. If you want to post additional text to Phabricator other than "Pass" and "Fail", select the **Add Custom Comment** checkbox. Then create a `.phabricator-comment` file and enter the text you want Jenkins to add to the build status comment in Phabricator.
![Add post-build action](/docs/configure-job-post-build.png)

Expand Down
Expand Up @@ -293,7 +293,7 @@ void processCoverage(CoverageProvider coverageProvider) {
logger.info(LOGGING_TAG, "No coverage provider available.");
return;
}
Map<String, List<Integer>> lineCoverage = coverageProvider.readLineCoverage();
Map<String, List<Integer>> lineCoverage = coverageProvider.getLineCoverage();
if (lineCoverage == null || lineCoverage.isEmpty()) {
logger.info(LOGGING_TAG, "No line coverage available to post to Harbormaster.");
return;
Expand Down
91 changes: 63 additions & 28 deletions src/main/java/com/uber/jenkins/phabricator/PhabricatorNotifier.java
Expand Up @@ -26,6 +26,7 @@
import com.uber.jenkins.phabricator.conduit.DifferentialClient;
import com.uber.jenkins.phabricator.coverage.CodeCoverageMetrics;
import com.uber.jenkins.phabricator.coverage.CoverageProvider;
import com.uber.jenkins.phabricator.coverage.XmlCoverageProvider;
import com.uber.jenkins.phabricator.credentials.ConduitCredentials;
import com.uber.jenkins.phabricator.provider.InstanceProvider;
import com.uber.jenkins.phabricator.tasks.NonDifferentialBuildTask;
Expand All @@ -38,10 +39,12 @@

import org.kohsuke.stapler.DataBoundConstructor;

import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

Expand All @@ -61,8 +64,13 @@

public class PhabricatorNotifier extends Notifier implements SimpleBuildStep {

private static final String DEFAULT_XML_COVERAGE_REPORT_PATTERN = "**/coverage*.xml, **/cobertura*.xml, "
+ "**/jacoco*.xml";
private static final CoverageReportFilenameFilter COVERAGE_FILENAME_FILTER = new CoverageReportFilenameFilter();

private static final String ABORT_TAG = "abort";
private static final String UBERALLS_TAG = "uberalls";
private static final String COVERAGE_TAG = "coverage";
private static final String CONDUIT_TAG = "conduit";
// Post a comment on success. Useful for lengthy builds.
private final boolean commentOnSuccess;
Expand Down Expand Up @@ -98,7 +106,7 @@ public PhabricatorNotifier(
this.commentWithConsoleLinkOnFailure = commentWithConsoleLinkOnFailure;
this.customComment = customComment;
this.processLint = processLint;
this.coverageReportPattern = coverageReportPattern;
this.coverageReportPattern = coverageReportPattern != null ? coverageReportPattern : DEFAULT_XML_COVERAGE_REPORT_PATTERN;
}

public BuildStepMonitor getRequiredMonitorService() {
Expand Down Expand Up @@ -149,7 +157,7 @@ public final void perform(
build.addAction(PhabricatorPostbuildAction.createShortText(branch, null));
}

coverageProvider = getCoverageProvider(build, workspace, listener, Collections.<String>emptySet());
coverageProvider = getCoverageProvider(build, workspace, listener, Collections.emptySet());
CodeCoverageMetrics coverageResult = null;
if (coverageProvider != null) {
coverageResult = coverageProvider.getMetrics();
Expand Down Expand Up @@ -299,11 +307,11 @@ private ConduitAPIClient getConduitClient(Job owner) throws ConduitAPIException
}

/**
* Get the cobertura coverage for the build
* Get the coverage provider for the build
*
* @param build The current build
* @param listener The build listener
* @return The current cobertura coverage, if any
* @return The current coverage, if any
*/
private CoverageProvider getCoverageProvider(
Run<?, ?> build, FilePath workspace,
Expand All @@ -319,36 +327,55 @@ private CoverageProvider getCoverageProvider(
return null;
}

Logger logger = new Logger(listener.getLogger());
List<CoverageProvider> coverageProviders = new ArrayList<CoverageProvider>();
copyCoverageToJenkinsMaster(build, workspace, listener);
CoverageProvider coverageProvider = new XmlCoverageProvider(getCoverageReports(build), includeFiles);
coverageProvider.computeCoverageIfNeeded();
cleanupCoverageFilesOnJenkinsMaster(build);

CoverageProvider coberturaCoverage = InstanceProvider.getCoberturaCoverageProvider(build,
workspace, includeFiles, coverageReportPattern, logger);
if (coberturaCoverage != null) {
coverageProviders.add(coberturaCoverage);
}

CoverageProvider jacocoCoverage = InstanceProvider.getJacocoCoverageProvider(build,
workspace, includeFiles, coverageReportPattern, logger);
if (jacocoCoverage != null) {
coverageProviders.add(jacocoCoverage);
if (coverageProvider.hasCoverage()) {
return coverageProvider;
} else {
Logger logger = new Logger(listener.getLogger());
logger.info(UBERALLS_TAG, "No coverage results found");
return null;
}
}

for (Iterator<CoverageProvider> i = coverageProviders.iterator(); i.hasNext(); ) {
CoverageProvider coverageProvider = i.next();

if (!coverageProvider.hasCoverage()) {
i.remove();
private void copyCoverageToJenkinsMaster(Run<?, ?> build, FilePath workspace, TaskListener listener) {
Logger logger = new Logger(listener.getLogger());
final FilePath moduleRoot = workspace;
final File buildCoberturaDir = build.getRootDir();
FilePath buildTarget = new FilePath(buildCoberturaDir);

if (moduleRoot != null) {
try {
int i = 0;
for (FilePath report : moduleRoot.list(coverageReportPattern)) {
final FilePath targetPath = new FilePath(buildTarget, "coverage" + (i == 0 ? "" : i) + ".xml");
report.copyTo(targetPath);
i++;
}
} catch (InterruptedException | IOException e) {
e.printStackTrace();
logger.warn(COVERAGE_TAG, "Unable to copy coverage to " + buildTarget);
}
}
if (!coverageProviders.isEmpty()) {
CoverageProvider provider = coverageProviders.get(0);
logger.info(UBERALLS_TAG, "Selected Coverage Provider: " + provider);
return provider;
}

private void cleanupCoverageFilesOnJenkinsMaster(Run<?, ?> build) {
for (File report : getCoverageReports(build)) {
report.delete();
}
}

logger.info(UBERALLS_TAG, "No coverage results found");
return null;
private Set<File> getCoverageReports(Run<?, ?> build) {
Set<File> reports = new HashSet<>();

File[] foundReports = build.getRootDir().listFiles(COVERAGE_FILENAME_FILTER);
if (foundReports != null) {
Collections.addAll(reports, foundReports);
}
return reports;
}

private UnitTestProvider getUnitProvider(Run<?, ?> build, TaskListener listener) {
Expand Down Expand Up @@ -449,4 +476,12 @@ private String getPhabricatorURL(Job owner) {
public PhabricatorNotifierDescriptor getDescriptor() {
return (PhabricatorNotifierDescriptor) super.getDescriptor();
}

private static class CoverageReportFilenameFilter implements FilenameFilter {

@Override
public boolean accept(File dir, String name) {
return name.startsWith("coverage") && name.endsWith("xml");
}
}
}

0 comments on commit ffa688c

Please sign in to comment.