Skip to content

Commit

Permalink
[FIXED JENKINS-38022] Create a verbose logging option for Nuget trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
Greybird committed Sep 7, 2016
1 parent 57d1ec4 commit 731e359
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 16 deletions.
Expand Up @@ -7,6 +7,9 @@
import org.jenkinsci.plugins.nuget.Messages;
import org.jenkinsci.plugins.nuget.NugetCause;
import org.jenkinsci.plugins.nuget.NugetGlobalConfiguration;
import org.jenkinsci.plugins.nuget.triggers.logs.InfoTriggerLog;
import org.jenkinsci.plugins.nuget.triggers.logs.TriggerLog;
import org.jenkinsci.plugins.nuget.triggers.logs.VerboseTriggerLog;
import org.jenkinsci.plugins.nuget.utils.NugetUpdater;
import hudson.Extension;
import hudson.model.AbstractProject;
Expand All @@ -29,17 +32,23 @@
public class NugetTrigger extends AbstractTrigger {

private boolean checkPrerelease;
private boolean useVerboseLogs;

@DataBoundConstructor
public NugetTrigger(String cronTabSpec, boolean checkPrerelease) throws ANTLRException {
public NugetTrigger(String cronTabSpec, boolean checkPrerelease, boolean useVerboseLogs) throws ANTLRException {
super(cronTabSpec);
this.checkPrerelease = checkPrerelease;
this.useVerboseLogs = useVerboseLogs;
}

public boolean getCheckPrerelease() {
return checkPrerelease;
}

public boolean getUseVerboseLogs() {
return useVerboseLogs;
}

@Override
protected File getLogFile() {
return new File(job.getRootDir(), "nuget-polling.log");
Expand Down Expand Up @@ -67,7 +76,8 @@ protected boolean checkIfModified(Node node, XTriggerLog xtl) throws XTriggerExc
}
AbstractProject project = (AbstractProject) job;
NugetGlobalConfiguration configuration = GlobalConfiguration.all().get(NugetGlobalConfiguration.class);
NugetUpdater updater = new NugetUpdater(project.getSomeWorkspace(), configuration, checkPrerelease, xtl);
TriggerLog log = useVerboseLogs ? new VerboseTriggerLog(xtl) : new InfoTriggerLog(xtl);
NugetUpdater updater = new NugetUpdater(project.getSomeWorkspace(), configuration, checkPrerelease, log);
return updater.performUpdate();
}

Expand Down
@@ -0,0 +1,79 @@
package org.jenkinsci.plugins.nuget.triggers.logs;

import hudson.model.TaskListener;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.nio.file.Path;

/**
* @author Arnaud TAMAILLON
*/
public class InfoTriggerLog implements TriggerLog {

protected final XTriggerLog log;

public InfoTriggerLog(XTriggerLog log) {
this.log = log;
}

@Override
public TaskListener getListener() {
return log.getListener();
}

@Override
public void checkingPackageFile(Path packageFile) {
log.info(String.format("Checking packages file: %s", packageFile.toAbsolutePath().toString()));
}

@Override
public void packageHasBeenUpdated(String id, String version, String latest) {
if (latest == null) {
log.info(String.format("Package %s v%s: no version found.", id, version));
} else {
log.info(String.format("Package %s v%s should update to v%s.", id, version, latest));
}
}

@Override
public void errorWhileParsingPackageConfigFile(SAXException exception) {
log.error(exception.toString());
}

@Override
public void errorVisitingFile(IOException exception) {
log.error(exception.toString());
}

@Override
public void skippingFileWithNoFileName() {

}

@Override
public void skippedFileNotPackagesConfig(Path fileName) {

}

@Override
public void packageVersionRetrieved(String id, String latest) {

}

@Override
public void reusingCachedPackageVersion(String id) {

}

@Override
public void error(String s) {
log.error(s);
}

@Override
public void info(String s) {
log.info(s);
}
}
@@ -0,0 +1,32 @@
package org.jenkinsci.plugins.nuget.triggers.logs;

import hudson.model.TaskListener;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.nio.file.Path;

/**
* @author Arnaud TAMAILLON
*/
public interface TriggerLog {

TaskListener getListener();

void checkingPackageFile(Path packageFile);
void packageHasBeenUpdated(String id, String version, String latest);

void errorWhileParsingPackageConfigFile(SAXException exception);
void errorVisitingFile(IOException exc);

void skippingFileWithNoFileName();
void skippedFileNotPackagesConfig(Path fileName);

void packageVersionRetrieved(String id, String latest);
void reusingCachedPackageVersion(String id);

void error(String s);
void info(String s);
}

@@ -0,0 +1,39 @@
package org.jenkinsci.plugins.nuget.triggers.logs;

import org.jenkinsci.lib.xtrigger.XTriggerLog;

import java.nio.file.Path;

/**
* @author Arnaud TAMAILLON
*/
public class VerboseTriggerLog extends InfoTriggerLog {

public VerboseTriggerLog(XTriggerLog log) {
super(log);
}

@Override
public void skippingFileWithNoFileName() {
log.info("Skipped file (no file name can be retrieved)");
}

@Override
public void skippedFileNotPackagesConfig(Path fileName) {
log.info(String.format("Skipped file (not packages.config): %s", fileName.toAbsolutePath().toString()));
}

@Override
public void packageVersionRetrieved(String id, String latest) {
if (latest == null) {
log.info(String.format("Latest version for Package %s: no version found.", id));
} else {
log.info(String.format("Latest version for Package %s is v%s.", id, latest)); }

}

@Override
public void reusingCachedPackageVersion(String id) {
log.info(String.format("Reusing cached version for Package %s.", id));
}
}
Expand Up @@ -5,6 +5,7 @@
import hudson.util.ArgumentListBuilder;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.nuget.NugetGlobalConfiguration;
import org.jenkinsci.plugins.nuget.triggers.logs.TriggerLog;

import java.io.IOException;
import java.io.OutputStream;
Expand All @@ -18,10 +19,10 @@ class NugetGetLatestPackageVersionCommand extends NugetCommandBase {
private ForkingOutputStream fork;
private final String packageName;
private final boolean checkPrerelease;
private final XTriggerLog log;
private final TriggerLog log;
private String version;

NugetGetLatestPackageVersionCommand(XTriggerLog log, NugetGlobalConfiguration configuration, FilePath workDir, String packageName, boolean checkPrerelease) {
NugetGetLatestPackageVersionCommand(TriggerLog log, NugetGlobalConfiguration configuration, FilePath workDir, String packageName, boolean checkPrerelease) {
super(log.getListener(), configuration, workDir);
this.log = log;
this.packageName = packageName;
Expand Down
Expand Up @@ -4,6 +4,7 @@
import hudson.FilePath;
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.nuget.NugetGlobalConfiguration;
import org.jenkinsci.plugins.nuget.triggers.logs.TriggerLog;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
Expand All @@ -26,7 +27,7 @@
class NugetPackageCheckerVisitor extends SimpleFileVisitor<Path> {

private final Map<String, String> latestPackageVersions = Maps.newHashMap();
private final XTriggerLog log;
private final TriggerLog log;
private final boolean preReleaseChecked;
private final FilePath workspaceRoot;
private final DocumentBuilder builder;
Expand All @@ -37,7 +38,7 @@ boolean isUpdated() {
return updated;
}

NugetPackageCheckerVisitor(XTriggerLog log, NugetGlobalConfiguration configuration, boolean preReleaseChecked, FilePath workspaceRoot) throws ParserConfigurationException {
NugetPackageCheckerVisitor(TriggerLog log, NugetGlobalConfiguration configuration, boolean preReleaseChecked, FilePath workspaceRoot) throws ParserConfigurationException {
this.log = log;
this.configuration = configuration;
this.preReleaseChecked = preReleaseChecked;
Expand All @@ -49,16 +50,18 @@ boolean isUpdated() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) throws IOException {
Path fileName = file.getFileName();
if (fileName == null) {
log.skippingFileWithNoFileName();
return FileVisitResult.CONTINUE;
}
if (!fileName.toString().equalsIgnoreCase("packages.config")) {
log.skippedFileNotPackagesConfig(file);
return FileVisitResult.CONTINUE;
}
log.checkingPackageFile(file);
return checkPackageFile(file);
}

private FileVisitResult checkPackageFile(Path file) throws IOException {
log.info(String.format("Checking packages file: %s", file.toAbsolutePath().toString()));
try {
Document doc = builder.parse(file.toFile());

Expand All @@ -69,27 +72,29 @@ private FileVisitResult checkPackageFile(Path file) throws IOException {
String id = p.getAttribute("id");
String version = p.getAttribute("version");
String latest = getPackageVersion(workspaceRoot, id);
log.packageVersionRetrieved(id, latest);

if (latest == null || !version.equals(latest)) {
log.info(String.format("Package %s v%s should update to v%s.", id, version, latest));
log.packageHasBeenUpdated(id, version, latest);
updated = true;
return FileVisitResult.TERMINATE;
}
}
} catch (SAXException ex) {
log.error(ex.toString());
log.errorWhileParsingPackageConfigFile(ex);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
log.error(exc.toString());
log.errorVisitingFile(exc);
return FileVisitResult.CONTINUE;
}

private String getPackageVersion(FilePath workspaceRoot, String packageName) throws IOException {
if (latestPackageVersions.containsKey(packageName)) {
log.reusingCachedPackageVersion(packageName);
return latestPackageVersions.get(packageName);
}
NugetGetLatestPackageVersionCommand command = new NugetGetLatestPackageVersionCommand(log, configuration, workspaceRoot, packageName, preReleaseChecked);
Expand Down
Expand Up @@ -6,6 +6,7 @@
import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.nuget.NugetGlobalConfiguration;
import org.jenkinsci.plugins.nuget.triggers.NugetTrigger;
import org.jenkinsci.plugins.nuget.triggers.logs.TriggerLog;

import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
Expand All @@ -18,10 +19,10 @@
*/
class NugetPackagesCheckerCallable extends MasterToSlaveFileCallable<Boolean> {
private final boolean preReleaseChecked;
private final XTriggerLog log;
private final TriggerLog log;
private final NugetGlobalConfiguration configuration;

NugetPackagesCheckerCallable(NugetGlobalConfiguration configuration, boolean preReleaseChecked, XTriggerLog log) {
NugetPackagesCheckerCallable(NugetGlobalConfiguration configuration, boolean preReleaseChecked, TriggerLog log) {
this.configuration = configuration;
this.preReleaseChecked = preReleaseChecked;
this.log = log;
Expand Down
Expand Up @@ -6,16 +6,17 @@

import org.jenkinsci.lib.xtrigger.XTriggerLog;
import org.jenkinsci.plugins.nuget.NugetGlobalConfiguration;
import org.jenkinsci.plugins.nuget.triggers.logs.TriggerLog;

/**
* @author bgregg
*/
public class NugetUpdater {
private final FilePath solutionDir;
private final NugetPackagesCheckerCallable nugetPackagesCheckerCallable;
private final XTriggerLog log;
private final TriggerLog log;

public NugetUpdater(FilePath solutionDir, NugetGlobalConfiguration configuration, boolean checkPrerelease, XTriggerLog log) {
public NugetUpdater(FilePath solutionDir, NugetGlobalConfiguration configuration, boolean checkPrerelease, TriggerLog log) {
this.solutionDir = solutionDir;
this.log = log;
this.nugetPackagesCheckerCallable = new NugetPackagesCheckerCallable(configuration, checkPrerelease, log);
Expand Down
Expand Up @@ -9,4 +9,7 @@
<f:entry title="${%Prerelease}">
<f:checkbox name="checkPrerelease" checked="${instance.checkPrerelease}"/>
</f:entry>
<f:entry title="${%VerboseLogs}">
<f:checkbox name="useVerboseLogs" checked="${instance.useVerboseLogs}"/>
</f:entry>
</j:jelly>
@@ -1 +1,2 @@
Prerelease=Allow prerelease
Prerelease=Allow prerelease
VerboseLogs=Verbose Logs
@@ -1 +1,2 @@
Prerelease=Accepter les versions préliminaires
Prerelease=Accepter les versions préliminaires
VerboseLogs=Logs détaillés

0 comments on commit 731e359

Please sign in to comment.