Skip to content

Commit

Permalink
Fix duplicate charts on Job page
Browse files Browse the repository at this point in the history
Also, remvoe deprecates and unnecessary exception handling blocks
  • Loading branch information
shanbin committed Sep 24, 2015
1 parent ba9c72e commit 3911968
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 74 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</parent>

<artifactId>scoverage</artifactId>
<version>1.2.1</version>
<version>1.2.2</version>
<name>Scoverage Plugin</name>
<packaging>hpi</packaging>
<url>http://wiki.jenkins-ci.org/display/JENKINS/Scoverage+Plugin</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ScoverageBuildAction(Run<?, ?> run, FilePath buildPath, ScoverageResult r
}

public String getIconFileName() {
return Functions.getResourcePath()+"/plugin/scoverage/images/scoverage.png";
return Functions.getResourcePath() + "/plugin/scoverage/images/scoverage.png";
}

public String getDisplayName() {
Expand Down Expand Up @@ -80,6 +80,5 @@ public DirectoryBrowserSupport doDynamic(StaplerRequest req, StaplerResponse rsp
@Override
public Collection<? extends Action> getProjectActions() {
return Collections.<Action>singleton(new ScoverageProjectAction(run.getParent()));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,8 @@ public ScoverageProjectAction(Job<?, ?> job) {
this.job = job;
}

@Deprecated
public ScoverageProjectAction(AbstractProject<?, ?> project) {
this((Job) project);
}

public String getIconFileName() {
return Functions.getResourcePath()+"/plugin/scoverage/images/scoverage.png";
return Functions.getResourcePath() + "/plugin/scoverage/images/scoverage.png";
}

public String getDisplayName() {
Expand Down
107 changes: 42 additions & 65 deletions src/main/java/org/jenkinsci/plugins/scoverage/ScoveragePublisher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
Expand All @@ -29,6 +28,7 @@
import java.io.FileReader;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -53,32 +53,19 @@ public String getReportFile() {
return reportFile == null || reportFile.trim().length() == 0 ? "scoverage.xml" : reportFile;
}

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

listener.getLogger().println("Publishing Scoverage XML and HTML report ...");
perform(build, build.getWorkspace(), launcher, listener);

return true;
}

@Override
public void perform(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
throws InterruptedException, IOException {

listener.getLogger().println("Publishing Scoverage XML and HTML report ...");

final File buildDir = run.getRootDir();
final FilePath buildPath = new FilePath(buildDir);
final FilePath scovPath = workspace.child(reportDir);

try {
copyReport(scovPath, buildPath, new BuildListenerAdapter(listener));
ScoverageResult result = processReport(run, buildPath);
run.addAction(new ScoverageBuildAction(run, buildPath, result));
} catch (IOException e) {
listener.getLogger().println("Unable to copy scoverage report from " + scovPath + " to " + buildPath);
throw e;
}
copyReport(scovPath, buildPath, new BuildListenerAdapter(listener));
ScoverageResult result = processReport(run, buildPath);
run.addAction(new ScoverageBuildAction(run, buildPath, result));
}

private static final class ScovFinder implements FilePath.FileCallable<File> {
Expand All @@ -101,7 +88,7 @@ public boolean accept(File dir, String s) {
return dir.isDirectory();
}
}, TrueFileFilter.INSTANCE);
return list.iterator().next();
return list == null ? null : list.iterator().next();
}

@Override
Expand All @@ -111,62 +98,52 @@ public void checkRoles(RoleChecker roleChecker) throws SecurityException {
}

private void copyReport(FilePath coverageDir, FilePath buildPath, BuildListener listener)
throws IOException, InterruptedException {
try {
if (coverageDir.exists()) {
// search index.html recursively and copy its parent tree
final FilePath indexPath = new FilePath(coverageDir.act(new ScovFinder()));
throws IOException, InterruptedException {
if (coverageDir.exists()) {
final FilePath toFile = buildPath.child(getReportFile());
coverageDir.child(getReportFile()).copyTo(toFile); // copy XML report
// search index.html recursively and copy its parent tree
final FilePath indexPath = new FilePath(coverageDir.act(new ScovFinder()));
if (indexPath != null) {
final FilePath indexDir = new FilePath(coverageDir.getChannel(), indexPath.getParent().getRemote());
final FilePath toFile = buildPath.child(getReportFile());
indexDir.copyRecursiveTo(buildPath.child(ActionUrls.BUILD_URL.toString())); // copy HTML report
coverageDir.child(getReportFile()).copyTo(toFile); // copy XML report
} else {
throw new IOException(coverageDir.getRemote() + " not exists");
listener.getLogger().println("Unable to find HTML reports under " + coverageDir.getRemote());
}
} catch (IOException e) {
listener.getLogger().println(e.getMessage());
throw e;
} catch (InterruptedException e) {
listener.getLogger().println(e.getMessage());
throw e;
} else {
throw new IOException(coverageDir.getRemote() + " not exists");
}
}

private ScoverageResult processReport(Run<?, ?> build, FilePath path) throws IOException, InterruptedException {
String[] ext = {"html"};
double statement = 0;
double condition = 0;
try {
// Fix HTML reports to use relative href
Collection<File> list = FileUtils.listFiles(new File(path.toURI()), ext, true);
for (File f : list) {
String content = FileUtils.readFileToString(f);
String pattern = f.getParent().replaceAll(".*scoverage-report", "scoverage-report");
String relativeFix = content.replaceAll("href=\"/", "href=\"")
.replaceAll("href=\".*" + pattern + "/", "href=\"");
FileUtils.writeStringToFile(f, relativeFix);
// Fix HTML reports to use relative href
Collection<File> list = FileUtils.listFiles(new File(path.toURI()), ext, true);
for (File f : list) {
String content = FileUtils.readFileToString(f);
String pattern = f.getParent().replaceAll(".*scoverage-report", "scoverage-report");
String relativeFix = content.replaceAll("href=\"/", "href=\"")
.replaceAll("href=\".*" + pattern + "/", "href=\"");
FileUtils.writeStringToFile(f, relativeFix);
}
// Parse scoverage.xml
File report = new File(path.child(reportFile).toURI());
Pattern pattern = Pattern.compile("^.* statement-rate=\"(.+?)\" branch-rate=\"(.+?)\"");
BufferedReader in = new BufferedReader(new FileReader(report));
String line;
while ((line = in.readLine()) != null) {
boolean found = false;
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
statement = Double.parseDouble(matcher.group(1));
condition = Double.parseDouble(matcher.group(2));
found = true;
}
// Parse scoverage.xml
File report = new File(path.child(reportFile).toURI());
Pattern pattern = Pattern.compile("^.* statement-rate=\"(.+?)\" branch-rate=\"(.+?)\"");
BufferedReader in = new BufferedReader(new FileReader(report));
String line;
while ((line = in.readLine()) != null) {
boolean found = false;
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
statement = Double.parseDouble(matcher.group(1));
condition = Double.parseDouble(matcher.group(2));
found = true;
}
if (found) {
break;
}
if (found) {
break;
}
} catch (IOException e) {
throw e;
} catch (InterruptedException e) {
throw e;
}
return new ScoverageResult(statement, condition, build.number);
}
Expand Down Expand Up @@ -207,8 +184,8 @@ public BuildStepMonitor getRequiredMonitorService() {
}

@Override
public Action getProjectAction(AbstractProject<?, ?> project) {
return new ScoverageProjectAction(project);
public Collection<? extends Action> getProjectActions(AbstractProject<?,?> project) {
return Collections.emptySet();
}
}

0 comments on commit 3911968

Please sign in to comment.