Skip to content

Commit

Permalink
Merge pull request hpsa#278 from bbokhonko-mf/octane-dev-latest
Browse files Browse the repository at this point in the history
US: #2029018 Send custom properties to ALM Octane on Job completion
  • Loading branch information
nissimshitrit authored Feb 1, 2023
2 parents daab132 + a0ad83d commit 6718268
Show file tree
Hide file tree
Showing 11 changed files with 288 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@
<dependency>
<artifactId>integrations-sdk</artifactId>
<groupId>com.hpe.adm.octane.ciplugins</groupId>
<version>2.7.7.0</version>
<version>2.7.8.0</version>
</dependency>

<!--BUILDER providers integration-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ private void publishFinishEvent(AbstractBuild build) {
.setParameters(ParameterProcessors.getInstances(build))
.setResult(BuildHandlerUtils.translateRunResult(build))
.setDuration(build.getDuration())
.setTestResultExpected(hasTests);
.setTestResultExpected(hasTests)
.setEnvironmentOutputtedParameters(OutputEnvironmentParametersHelper.getOutputEnvironmentParams(build));
CommonOriginRevision commonOriginRevision = getCommonOriginRevision(build);
if (commonOriginRevision != null) {
event
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package com.microfocus.application.automation.tools.octane.events;

import com.microfocus.application.automation.tools.octane.configuration.SDKBasedLoggerProvider;
import com.microfocus.application.automation.tools.settings.OutputEnvironmentVariablesBuildWrapper;
import com.microfocus.application.automation.tools.settings.RunnerMiscSettingsGlobalConfiguration;
import com.microfocus.application.automation.tools.sse.common.StringUtils;
import hudson.EnvVars;
import hudson.model.AbstractBuild;
import hudson.model.BuildableItemWithBuildWrappers;
import hudson.model.Job;
import hudson.model.Run;
import org.apache.logging.log4j.Logger;

import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class OutputEnvironmentParametersHelper {

public static final String SPLIT_SYMBOL = " ";

private static Logger logger = SDKBasedLoggerProvider.getLogger(OutputEnvironmentParametersHelper.class);

public static Map<String, String> getOutputEnvironmentParams(Run run) {
EnvVars environment = getEnvironment(run);
if (environment == null) {
return Collections.emptyMap();
} else {
List<String> paramKeysList = new ArrayList<>();
paramKeysList.addAll(getGlobalParamsList());
paramKeysList.addAll(getJobParamsList(run));

if (paramKeysList.isEmpty()) return Collections.emptyMap();

Map<String, String> outputEnvParams = new HashMap<>();
Set<String> sensitiveBuildVariables =null;
if (run instanceof AbstractBuild) {
sensitiveBuildVariables = ((AbstractBuild) run).getSensitiveBuildVariables();
}

String value;
for (String key : paramKeysList) {
if (sensitiveBuildVariables != null && sensitiveBuildVariables.contains(key)) continue;
value = environment.get(key);
if (value != null) {
outputEnvParams.put(key, value);
}
}
return outputEnvParams;
}
}

private static EnvVars getEnvironment(Run run) {
EnvVars environment = null;
try {
environment = run.getEnvironment(null);
} catch (IOException | InterruptedException e) {
logger.error("Can not get Run(id: " + run.getId() + ") Environment: " + e.getMessage());
}
return environment;
}

private static List<String> getGlobalParamsList() {
try {
return Stream.of(RunnerMiscSettingsGlobalConfiguration.getInstance().getOutputEnvironmentParameters()
.split(SPLIT_SYMBOL)).filter(p -> !StringUtils.isNullOrEmpty(p)).collect(Collectors.toList());
} catch (NullPointerException ignored) {
return Collections.emptyList();
}
}

private static List<String> getJobParamsList(Run run) {
Job<?, ?> job = run.getParent();
if (job instanceof BuildableItemWithBuildWrappers) {
OutputEnvironmentVariablesBuildWrapper outputEnvVarsBuildWrapper = ((BuildableItemWithBuildWrappers) job)
.getBuildWrappersList().get(OutputEnvironmentVariablesBuildWrapper.class);
if (outputEnvVarsBuildWrapper != null) {
String paramsStr = outputEnvVarsBuildWrapper.getOutputEnvironmentParameters();
if (!StringUtils.isNullOrEmpty(paramsStr)) {
String[] params = paramsStr.split(SPLIT_SYMBOL);
return Stream.of(params).filter(p -> !StringUtils.isNullOrEmpty(p)).collect(Collectors.toList());
}
}
}
return Collections.emptyList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ private void sendPipelineFinishedEvent(WorkflowRun parentRun) {
.setDuration(parentRun.getDuration())
.setResult(BuildHandlerUtils.translateRunResult(parentRun))
.setCauses(CIEventCausesFactory.processCauses(parentRun))
.setTestResultExpected(hasTests);
.setTestResultExpected(hasTests)
.setEnvironmentOutputtedParameters(OutputEnvironmentParametersHelper.getOutputEnvironmentParams(parentRun));
CIJenkinsServicesImpl.publishEventToRelevantClients(event);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import com.hp.octane.integrations.utils.SdkConstants;
import com.hp.octane.integrations.utils.SdkStringUtils;
import com.microfocus.application.automation.tools.octane.configuration.SDKBasedLoggerProvider;
import com.microfocus.application.automation.tools.octane.events.OutputEnvironmentParametersHelper;
import com.microfocus.application.automation.tools.octane.executor.UftConstants;
import com.microfocus.application.automation.tools.octane.model.processors.builders.AbstractBuilderProcessor;
import com.microfocus.application.automation.tools.octane.model.processors.builders.BuildTriggerProcessor;
Expand Down Expand Up @@ -209,6 +210,7 @@ public CIBuildStatusInfo getBuildStatus(String paramName, String paramValue) {
} else {
status.setBuildStatus(CIBuildStatus.FINISHED);
status.setResult(BuildHandlerUtils.translateRunResult(aBuild));
status.setEnvironmentOutputtedParameters(OutputEnvironmentParametersHelper.getOutputEnvironmentParams(aBuild));
}
status.setAllBuildParams(ParameterProcessors.getInstances(aBuild));
status.setBuildCiId(BuildHandlerUtils.getBuildCiId(aBuild));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.microfocus.application.automation.tools.settings;

import com.microfocus.application.automation.tools.sse.common.StringUtils;
import hudson.EnvVars;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.BuildListener;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.IOException;
import java.io.Serializable;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.microfocus.application.automation.tools.octane.events.OutputEnvironmentParametersHelper.SPLIT_SYMBOL;

public class OutputEnvironmentVariablesBuildWrapper extends BuildWrapper implements Serializable {

private String outputEnvironmentParameters;
@DataBoundConstructor
public OutputEnvironmentVariablesBuildWrapper(String outputEnvironmentParameters) {
setOutputEnvironmentParameters(outputEnvironmentParameters);
}

public String getOutputEnvironmentParameters() {
return outputEnvironmentParameters;
}
@DataBoundSetter
public void setOutputEnvironmentParameters(String outputEnvironmentParameters) {
this.outputEnvironmentParameters = getValidatedOutputEnvironmentParameters(outputEnvironmentParameters);
}

@Override
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
EnvVars envVars = build.getEnvironment(listener);
return new Environment() {
@Override
public void buildEnvVars(Map<String, String> env) {
env.putAll(envVars);
}
};
}

private String getValidatedOutputEnvironmentParameters(String envParams) {
String[] params = envParams.split("\\s++");
return Stream.of(params).filter(p -> !StringUtils.isNullOrEmpty(p))
.collect(Collectors.joining(SPLIT_SYMBOL));
}


@Extension
public static final class DescriptorImpl extends BuildWrapperDescriptor {

@Override
public boolean isApplicable(AbstractProject<?, ?> item) {
return true;
}

@Override
public String getDisplayName() {
return "Define list of Environment Variables to be sent to ALM Octane";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.microfocus.application.automation.tools.octane.events.OutputEnvironmentParametersHelper.SPLIT_SYMBOL;

@Extension(ordinal = 1, optional = true)
public class RunnerMiscSettingsGlobalConfiguration extends GlobalConfiguration implements Serializable {

Expand All @@ -53,16 +55,19 @@ public class RunnerMiscSettingsGlobalConfiguration extends GlobalConfiguration i
public static final List<String> DEFAULT_UFT_DATE_PATTERNS = Arrays.asList(DEFAULT_UFT_DATE_PATTERN1, DEFAULT_UFT_DATE_PATTERN2, DEFAULT_UFT_DATE_PATTERN3);

public static final String DEFAULT_BRANCHES = "master main trunk mainline";
public static final String DEFAULT_OUTPUT_ENVIRONMENT_PARAMETERS = "BUILD_DISPLAY_NAME BUILD_TAG BUILD_URL";

private String dateFormat;
private String defaultBranches;
private String outputEnvironmentParameters;
private boolean agentToControllerEnabled;

@DataBoundConstructor
public RunnerMiscSettingsGlobalConfiguration(String mfDateFormat, String defaultBranches,boolean agentToControllerEnabled) {
public RunnerMiscSettingsGlobalConfiguration(String mfDateFormat, String defaultBranches, String outputEnvironmentParameters, boolean agentToControllerEnabled) {
setDateFormat(mfDateFormat);
setDefaultBranches(defaultBranches);
setAgentToControllerEnabled(agentToControllerEnabled);
setOutputEnvironmentParameters(outputEnvironmentParameters);
}

public RunnerMiscSettingsGlobalConfiguration() {
Expand Down Expand Up @@ -102,14 +107,27 @@ public void setDefaultBranches(String defaultBranches) {
save();
}

public String getOutputEnvironmentParameters() {
return outputEnvironmentParameters;
}

public void setOutputEnvironmentParameters(String outputEnvironmentParameters) {
this.outputEnvironmentParameters = getValidatedOutputEnvironmentParameters(outputEnvironmentParameters);
save();
}

private String getValidatedDefaultBranches(String defaultBranches) {
String[] branches = defaultBranches.split(" ");
return Stream.of(branches).filter(branch -> !StringUtils.isNullOrEmpty(branch))
.collect(Collectors.joining(" "));
}

private String getValidatedOutputEnvironmentParameters(String envParams) {
String[] params = envParams.split("\\s++");
return Stream.of(params).filter(p -> !StringUtils.isNullOrEmpty(p))
.collect(Collectors.joining(SPLIT_SYMBOL));
}

public DateTimeFormatter getDateFormatter() {
return dateFormat != null ? DateTimeFormatter.ofPattern(dateFormat) : DateTimeFormatter.ofPattern(DEFAULT_UFT_DATE_PATTERN1);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?jelly escape-by-default='true'?>
<!--
~ Certain versions of software and/or documents ("Material") accessible here may contain branding from
~ Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017,
~ the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP
~ and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
~ marks are the property of their respective owners.
~ __________________________________________________________________
~ MIT License
~
~ (c) Copyright 2012-2021 Micro Focus or one of its affiliates.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
~ documentation files (the "Software"), to deal in the Software without restriction, including without limitation
~ the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
~ and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all copies or
~ substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
~ THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
~ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
~
~ ___________________________________________________________________
-->

<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry title="List of output Environment Variables" field="outputEnvironmentParameters">
<f:textarea name="mf.outputEnvironmentParameters" value="${instance.outputEnvironmentParameters}"/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
~ Certain versions of software and/or documents ("Material") accessible here may contain branding from
~ Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017,
~ the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP
~ and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
~ marks are the property of their respective owners.
~ __________________________________________________________________
~ MIT License
~
~ (c) Copyright 2012-2021 Micro Focus or one of its affiliates.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
~ documentation files (the "Software"), to deal in the Software without restriction, including without limitation
~ the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
~ and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all copies or
~ substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
~ THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
~ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
~
~ ___________________________________________________________________
-->

<div>
Define list of Environment Variables of a specific job to be sent to ALM Octane, additional to the properties specified in the global settings. After the job is done, Jenkins will pass these properties to a respective Auto Action.
<br/>
Delimit multiple properties with a space or EOL.
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<f:entry title="Default branches" field="defaultBranches" >
<f:textbox name="mf.defaultBranches" value="${instance.defaultBranches}" />
</f:entry>
<f:entry title="List of output Environment Variables" field="outputEnvironmentParameters" >
<f:textarea name="mf.outputEnvironmentParameters" value="${instance.outputEnvironmentParameters}" default="${instance.DEFAULT_OUTPUT_ENVIRONMENT_PARAMETERS}" />
</f:entry>
<f:entry title="Enable Agent to Controller access" description="If checked, when you execute a build on an agent, the agent will access the controller to write results that will be reported to ALM Octane.">
<f:checkbox name="agentToControllerEnabled" checked="${instance.agentToControllerEnabled}"/>
</f:entry>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<!--
~ Certain versions of software and/or documents ("Material") accessible here may contain branding from
~ Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017,
~ the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP
~ and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
~ marks are the property of their respective owners.
~ __________________________________________________________________
~ MIT License
~
~ (c) Copyright 2012-2021 Micro Focus or one of its affiliates.
~
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
~ documentation files (the "Software"), to deal in the Software without restriction, including without limitation
~ the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
~ and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
~
~ The above copyright notice and this permission notice shall be included in all copies or
~ substantial portions of the Software.
~
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
~ THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
~ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
~ SOFTWARE.
~
~ ___________________________________________________________________
-->

<div>
Define global list of environment variables to be passed to ALM Octane. This setting apply to all jobs. After the job is done, Jenkins passes listed parameters to a respective Auto Action. Delimit multiple parameters with a space or EOL.
<br/>
To define list of environment variables for a specific job, go to the Build Environment tab of that job.
</div>

0 comments on commit 6718268

Please sign in to comment.