Skip to content

Commit

Permalink
Add option to inject common build variables as ATX constants (#156)
Browse files Browse the repository at this point in the history
- BUILD_NUMBER, BUILD_URL, JOB_NAME
  • Loading branch information
cpoenisch authored Mar 13, 2020
1 parent 72d9ea3 commit 8ec503f
Show file tree
Hide file tree
Showing 11 changed files with 93 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public class ATXPublisher extends AbstractReportPublisher {
private ATXInstallation atxInstallation;
/**
* Specifies whether to fail the build if upload is enabled and the server is offline.
*
* @since 2.11
*/
private boolean failOnOffline;
Expand All @@ -63,6 +64,12 @@ public class ATXPublisher extends AbstractReportPublisher {
* @since 2.14
*/
private boolean usePersistedSettings;
/**
* Specifies whether to inject common build variables as ATX constants.
*
* @since 2.15
*/
private boolean injectBuildVars;

/**
* Instantiates a new {@link ATXPublisher}.
Expand Down Expand Up @@ -112,6 +119,15 @@ public void setUsePersistedSettings(final boolean usePersistedSettings) {
this.usePersistedSettings = usePersistedSettings;
}

public boolean isInjectBuildVars() {
return injectBuildVars;
}

@DataBoundSetter
public void setInjectBuildVars(final boolean injectBuildVars) {
this.injectBuildVars = injectBuildVars;
}

@Override
public void performReport(final Run<?, ?> run, final FilePath workspace, final Launcher launcher,
final TaskListener listener) throws InterruptedException, IOException, ETPluginException {
Expand Down Expand Up @@ -157,8 +173,8 @@ public void performReport(final Run<?, ?> run, final FilePath workspace, final L
}

/**
* Publishes the ATX reports by first generating them and depending
* on whether ATX upload is enabled also starting the upload.
* Publishes the ATX reports by first generating them and depending on whether ATX upload is enabled also starting
* the upload.
*
* @param installation the installation
* @param run the run
Expand All @@ -171,15 +187,16 @@ public void performReport(final Run<?, ?> run, final FilePath workspace, final L
*/
private boolean publishReports(final ATXInstallation installation, final Run<?, ?> run, final FilePath workspace,
final Launcher launcher, final TaskListener listener)
throws IOException, InterruptedException {
throws IOException, InterruptedException {
final TTConsoleLogger logger = getLogger();
final List<FilePath> reportDirs = getReportDirs(run, workspace, launcher);
final boolean isUploadEnabled = isUploadEnabled(installation);
final boolean isServerReachable = isServerReachable(installation, run, launcher, listener);
if (isUploadEnabled && isServerReachable) {
logger.logInfo("- Generating and uploading ATX reports...");
final ATXReportUploader uploader = new ATXReportUploader(installation);
return uploader.upload(reportDirs, isAllowMissing(), isUsePersistedSettings(), run, launcher, listener);
return uploader.upload(reportDirs, isUsePersistedSettings(), isInjectBuildVars(), isAllowMissing(),
run, launcher, listener);
} else if (isUploadEnabled && failOnOffline) {
logger.logError("-> TEST-GUIDE server is not reachable, setting build status to FAILURE!");
return false;
Expand All @@ -191,8 +208,8 @@ private boolean publishReports(final ATXInstallation installation, final Run<?,
final FilePath archiveTarget = getArchiveTarget(run);

final ATXReportGenerator generator = new ATXReportGenerator(installation);
return generator.generate(archiveTarget, reportDirs, isAllowMissing(), isUsePersistedSettings(),
isArchiving(), isKeepAll(), run, launcher, listener);
return generator.generate(archiveTarget, reportDirs, isUsePersistedSettings(), isInjectBuildVars(),
isAllowMissing(), isArchiving(), isKeepAll(), run, launcher, listener);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,11 @@ public ATXReportGenerator(final ATXInstallation installation) {
*
* @param archiveTarget the archive target directory
* @param reportDirs the report directories
* @param usePersistedSettings specifies whether to use read settings from persisted configurations file
* @param injectBuildVars specifies whether to inject common build variables as ATX constants
* @param allowMissing specifies whether missing reports are allowed
* @param isArchiving specifies whether archiving artifacts is enabled
* @param keepAll specifies whether to keep all artifacts
* @param usePersistedSettings specifies whether to use read settings from persisted configurations file
* @param run the run
* @param launcher the launcher
* @param listener the listener
Expand All @@ -60,17 +61,17 @@ public ATXReportGenerator(final ATXInstallation installation) {
*/
@SuppressWarnings({"checkstyle:cyclomaticcomplexity", "checkstyle:npathcomplexity"})
public boolean generate(final FilePath archiveTarget, final List<FilePath> reportDirs,
final boolean usePersistedSettings, final boolean injectBuildVars,
final boolean allowMissing, final boolean isArchiving, final boolean keepAll,
final boolean usePersistedSettings, final Run<?, ?> run, final Launcher launcher,
final TaskListener listener)
throws IOException, InterruptedException {
final Run<?, ?> run, final Launcher launcher, final TaskListener listener)
throws IOException, InterruptedException {
final TTConsoleLogger logger = new TTConsoleLogger(listener);
final List<FilePath> reportFiles = new ArrayList<>();
for (final FilePath reportDir : reportDirs) {
final FilePath reportFile = AbstractReportPublisher.getFirstReportFile(reportDir);
if (reportFile != null && reportFile.exists()) {
reportFiles.addAll(Arrays.asList(
reportDir.list(TRFPublisher.TRF_INCLUDES, TRFPublisher.TRF_EXCLUDES)));
reportDir.list(TRFPublisher.TRF_INCLUDES, TRFPublisher.TRF_EXCLUDES)));
} else {
if (!allowMissing) {
logger.logError(String.format("Specified TRF file '%s' does not exist.", reportFile));
Expand All @@ -86,8 +87,8 @@ public boolean generate(final FilePath archiveTarget, final List<FilePath> repor

// Generate ATX reports
final boolean isGenerated = launcher.getChannel().call(
new GenerateReportCallable(getInstallation().getConfig(), reportFiles, usePersistedSettings,
run.getEnvironment(listener), listener));
new GenerateReportCallable(getInstallation().getConfig(), reportFiles,
usePersistedSettings, injectBuildVars, run.getEnvironment(listener), listener));

if (isArchiving) {
// Removing old artifacts at project level
Expand Down Expand Up @@ -202,27 +203,31 @@ private static final class GenerateReportCallable extends AbstractReportCallable
private static final long serialVersionUID = 1L;

private final boolean usePersistedSettings;
private final boolean injectBuildVars;

/**
* Instantiates a new {@link GenerateReportCallable}.
*
* @param config the ATX configuration
* @param reportFiles the list of TRF files
* @param usePersistedSettings specifies whether to use read settings from persisted configurations file
* @param injectBuildVars specifies whether to inject common build variables as ATX constants
* @param envVars the environment variables
* @param listener the listener
*/
GenerateReportCallable(final ATXConfig config, final List<FilePath> reportFiles,
final boolean usePersistedSettings, final EnvVars envVars, final TaskListener listener) {
final boolean usePersistedSettings, final boolean injectBuildVars,
final EnvVars envVars, final TaskListener listener) {
super(config, reportFiles, envVars, listener);
this.usePersistedSettings = usePersistedSettings;
this.injectBuildVars = injectBuildVars;
}

@Override
public Boolean call() {
boolean isGenerated = true;
final TTConsoleLogger logger = new TTConsoleLogger(getListener());
final Map<String, String> configMap = getConfigMap(false);
final Map<String, String> configMap = getConfigMap(false, injectBuildVars);
final String progId = ETComProperty.getInstance().getProgId();
try (ETComClient comClient = new ETComClient(progId)) {
final TestEnvironment testEnv = (TestEnvironment) comClient.getTestEnvironment();
Expand All @@ -237,12 +242,12 @@ public Boolean call() {
final FilePath reportDir = reportFile.getParent();
final FilePath configPath = reportDir.child(ATX_TEMPLATE_NAME + ".xml");
logger.logInfo(String.format("- Using persisted settings from configuration: %s",
configPath.getRemote()));
configPath.getRemote()));
testEnv.generateTestReportDocument(reportFile.getRemote(),
reportDir.getRemote(), configPath.getRemote(), true);
reportDir.getRemote(), configPath.getRemote(), true);
} else {
testEnv.generateTestReportDocumentFromDB(reportFile.getRemote(),
outDir.getRemote(), ATX_TEMPLATE_NAME, true, configMap);
outDir.getRemote(), ATX_TEMPLATE_NAME, true, configMap);
}
comClient.waitForIdle(0);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,20 @@ public ATXReportUploader(final ATXInstallation installation) {
* Generates and uploads {@link ATXReport}s.
*
* @param reportDirs the report directories
* @param usePersistedSettings specifies whether to use report generator settings from persisted configurations
* file
* @param injectBuildVars specifies whether to inject common build variables as ATX constants
* @param allowMissing specifies whether missing reports are allowed
* @param usePersistedSettings whether to use report generator settings from persisted configurations file
* @param run the run
* @param launcher the launcher
* @param listener the listener
* @return {@code true} if upload succeeded, {@code false} otherwise
* @throws IOException signals that an I/O exception has occurred
* @throws InterruptedException if the build gets interrupted
*/
public boolean upload(final List<FilePath> reportDirs, final boolean allowMissing,
final boolean usePersistedSettings, final Run<?, ?> run,
final Launcher launcher, final TaskListener listener)
public boolean upload(final List<FilePath> reportDirs, final boolean usePersistedSettings,
final boolean injectBuildVars, final boolean allowMissing,
final Run<?, ?> run, final Launcher launcher, final TaskListener listener)
throws IOException, InterruptedException {
final TTConsoleLogger logger = new TTConsoleLogger(listener);
final List<ATXReport> atxReports = new ArrayList<>();
Expand All @@ -115,7 +117,8 @@ public boolean upload(final List<FilePath> reportDirs, final boolean allowMissin

// Upload ATX reports
final UploadInfoHolder uploadInfo = launcher.getChannel().call(
new UploadReportCallable(config, uploadFiles, usePersistedSettings, envVars, listener));
new UploadReportCallable(config, uploadFiles, usePersistedSettings, injectBuildVars,
envVars, listener));

if (uploadInfo.isUploaded()) {
// Prepare ATX report links
Expand Down Expand Up @@ -371,27 +374,31 @@ private static final class UploadReportCallable extends AbstractReportCallable<U
private static final String SUCCESS_FILE_NAME = "success.json";

private final boolean usePersistedSettings;
private final boolean injectBuildVars;

/**
* Instantiates a new {@link UploadReportCallable}.
*
* @param config the ATX configuration
* @param reportFiles the list of TRF files
* @param usePersistedSettings specifies whether to use read settings from persisted configurations file
* @param injectBuildVars specifies whether to inject common build variables as ATX constants
* @param envVars the environment variables
* @param listener the listener
*/
UploadReportCallable(final ATXConfig config, final List<FilePath> reportFiles,
final boolean usePersistedSettings, final EnvVars envVars, final TaskListener listener) {
final boolean usePersistedSettings, final boolean injectBuildVars,
final EnvVars envVars, final TaskListener listener) {
super(config, reportFiles, envVars, listener);
this.usePersistedSettings = usePersistedSettings;
this.injectBuildVars = injectBuildVars;
}

@Override
public UploadInfoHolder call() throws IOException {
final UploadInfoHolder uploadInfo = new UploadInfoHolder(false);
final TTConsoleLogger logger = new TTConsoleLogger(getListener());
final Map<String, String> configMap = getConfigMap(true);
final Map<String, String> configMap = getConfigMap(true, injectBuildVars);
final String progId = ETComProperty.getInstance().getProgId();
try (ETComClient comClient = new ETComClient(progId)) {
final TestEnvironment testEnv = (TestEnvironment) comClient.getTestEnvironment();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jenkins.security.MasterToSlaveCallable;

import java.io.IOException;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -91,10 +92,11 @@ public TaskListener getListener() {
* Converts the ATX configuration to a map containing all setting names and their current value.
* Parameterized values are expanded by given environment variables.
*
* @param uploadToServer specifies whether ATX upload is enabled or not
* @param uploadToServer specifies whether ATX upload is enabled or not
* @param injectBuildVars specifies whether to inject common build variables as ATX constants
* @return the configuration map
*/
protected Map<String, String> getConfigMap(final boolean uploadToServer) {
protected Map<String, String> getConfigMap(final boolean uploadToServer, final boolean injectBuildVars) {
final Map<String, String> configMap = new LinkedHashMap<>();
for (final ATXSetting<?> setting : config.getSettings()) {
if (setting instanceof ATXBooleanSetting) {
Expand All @@ -116,7 +118,25 @@ protected Map<String, String> getConfigMap(final boolean uploadToServer) {
configMap.put(setting.getName(), envVars.expand(((ATXCustomTextSetting) setting).getValue()));
}
}
if (injectBuildVars) {
final List<String> constants = Arrays.asList(
configMap.get("setConstants"),
formatConstant("BUILD_NUMBER"),
formatConstant("BUILD_URL"),
formatConstant("JOB_NAME"));
configMap.replace("setConstants", String.join(";", constants));
}
return configMap;
}

/**
* Formats the ATX constant to be added as key-value pair (TT_JENKINS_&lt;VAR&gt;=&lt;VAR&gt;).
*
* @param variableName the variable name
* @return the formatted ATX constant
*/
private String formatConstant(final String variableName) {
return String.format("%s=%s", "TT_JENKINS_" + variableName, envVars.get(variableName));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,9 @@
field="usePersistedSettings">
<f:checkbox default="false"/>
</f:entry>
<f:entry title="${%injectBuildVars.title}" description="${%injectBuildVars.description}"
field="injectBuildVars">
<f:checkbox default="false"/>
</f:entry>
<st:include class="de.tracetronic.jenkins.plugins.ecutest.report.AbstractReportPublisher" page="config.jelly"/>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ atxName.title=TEST-GUIDE Installation
failOnOffline.description=Mark the build as failed if upload is enabled and the TEST-GUIDE server is offline, \
otherwise ATX reports will be generated for manual upload.
failOnOffline.title=Fail on Offline
injectBuildVars.description=Inject common build variables (BUILD_NUMBER, BUILD_URL, JOB_NAME) as ATX constants \
automatically.
injectBuildVars.title=Inject Build Variables
usePersistedSettings.description=Load the persisted report generator settings from XML configuration file (ATX.xml) \
stored next to TRF (since ECU-TEST 8.0).
usePersistedSettings.title=Persisted Settings
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ atxName.title=TEST-GUIDE-Installation
failOnOffline.description=Markiert den Build als fehlgeschlagen, falls der Upload aktiviert ist und der \
TEST-GUIDE-Server nicht erreichbar ist, andernfalls werden die ATX-Reports zum manuellen Upload generiert.
failOnOffline.title=Fehlschlag wenn offline
injectBuildVars.description=F\u00FCgt allgemeine Buildvariablen (BUILD_NUMBER, BUILD_URL, JOB_NAME) automatisch als \
ATX-Konstanten hinzu.
injectBuildVars.title=Buildvariablen einf\u00FCgen
usePersistedSettings.description=L\u00E4dt die gespeicherten Reportgenerator-Einstellungen aus der XML-Konfiguration \
(ATX.xml), die neben der TRF liegt (ab ECU-TEST 8.0).
usePersistedSettings.title=Gepeicherte Einstellungen
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void testConfigRoundTripStep() throws Exception {
final ATXPublisher before = new ATXPublisher("TEST-GUIDE");
before.setFailOnOffline(false);
before.setUsePersistedSettings(false);
before.setInjectBuildVars(false);
before.setRunOnFailed(false);
before.setAllowMissing(false);
before.setRunOnFailed(false);
Expand All @@ -97,6 +98,7 @@ public void testConfigView() throws Exception {
final ATXPublisher publisher = new ATXPublisher("TEST-GUIDE");
publisher.setFailOnOffline(true);
publisher.setUsePersistedSettings(true);
publisher.setInjectBuildVars(true);
publisher.setAllowMissing(true);
publisher.setRunOnFailed(true);
publisher.setArchiving(false);
Expand All @@ -109,6 +111,7 @@ public void testConfigView() throws Exception {
jenkins.assertXPath(page, "//option[@value='TEST-GUIDE']");
jenkins.assertXPath(page, "//input[@name='_.failOnOffline' and @checked='true']");
jenkins.assertXPath(page, "//input[@name='_.usePersistedSettings' and @checked='true']");
jenkins.assertXPath(page, "//input[@name='_.injectBuildVars' and @checked='true']");
jenkins.assertXPath(page, "//input[@name='_.allowMissing' and @checked='true']");
jenkins.assertXPath(page, "//input[@name='_.runOnFailed' and @checked='true']");
jenkins.assertXPath(page, "//input[@name='_.archiving']");
Expand Down
Loading

0 comments on commit 8ec503f

Please sign in to comment.