Skip to content

Commit 6718268

Browse files
Merge pull request hpsa#278 from bbokhonko-mf/octane-dev-latest
US: #2029018 Send custom properties to ALM Octane on Job completion
2 parents daab132 + a0ad83d commit 6718268

File tree

11 files changed

+288
-4
lines changed

11 files changed

+288
-4
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@
489489
<dependency>
490490
<artifactId>integrations-sdk</artifactId>
491491
<groupId>com.hpe.adm.octane.ciplugins</groupId>
492-
<version>2.7.7.0</version>
492+
<version>2.7.8.0</version>
493493
</dependency>
494494

495495
<!--BUILDER providers integration-->

src/main/java/com/microfocus/application/automation/tools/octane/events/AbstractBuildListenerOctaneImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ private void publishFinishEvent(AbstractBuild build) {
126126
.setParameters(ParameterProcessors.getInstances(build))
127127
.setResult(BuildHandlerUtils.translateRunResult(build))
128128
.setDuration(build.getDuration())
129-
.setTestResultExpected(hasTests);
129+
.setTestResultExpected(hasTests)
130+
.setEnvironmentOutputtedParameters(OutputEnvironmentParametersHelper.getOutputEnvironmentParams(build));
130131
CommonOriginRevision commonOriginRevision = getCommonOriginRevision(build);
131132
if (commonOriginRevision != null) {
132133
event
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.microfocus.application.automation.tools.octane.events;
2+
3+
import com.microfocus.application.automation.tools.octane.configuration.SDKBasedLoggerProvider;
4+
import com.microfocus.application.automation.tools.settings.OutputEnvironmentVariablesBuildWrapper;
5+
import com.microfocus.application.automation.tools.settings.RunnerMiscSettingsGlobalConfiguration;
6+
import com.microfocus.application.automation.tools.sse.common.StringUtils;
7+
import hudson.EnvVars;
8+
import hudson.model.AbstractBuild;
9+
import hudson.model.BuildableItemWithBuildWrappers;
10+
import hudson.model.Job;
11+
import hudson.model.Run;
12+
import org.apache.logging.log4j.Logger;
13+
14+
import java.io.IOException;
15+
import java.util.*;
16+
import java.util.stream.Collectors;
17+
import java.util.stream.Stream;
18+
19+
public class OutputEnvironmentParametersHelper {
20+
21+
public static final String SPLIT_SYMBOL = " ";
22+
23+
private static Logger logger = SDKBasedLoggerProvider.getLogger(OutputEnvironmentParametersHelper.class);
24+
25+
public static Map<String, String> getOutputEnvironmentParams(Run run) {
26+
EnvVars environment = getEnvironment(run);
27+
if (environment == null) {
28+
return Collections.emptyMap();
29+
} else {
30+
List<String> paramKeysList = new ArrayList<>();
31+
paramKeysList.addAll(getGlobalParamsList());
32+
paramKeysList.addAll(getJobParamsList(run));
33+
34+
if (paramKeysList.isEmpty()) return Collections.emptyMap();
35+
36+
Map<String, String> outputEnvParams = new HashMap<>();
37+
Set<String> sensitiveBuildVariables =null;
38+
if (run instanceof AbstractBuild) {
39+
sensitiveBuildVariables = ((AbstractBuild) run).getSensitiveBuildVariables();
40+
}
41+
42+
String value;
43+
for (String key : paramKeysList) {
44+
if (sensitiveBuildVariables != null && sensitiveBuildVariables.contains(key)) continue;
45+
value = environment.get(key);
46+
if (value != null) {
47+
outputEnvParams.put(key, value);
48+
}
49+
}
50+
return outputEnvParams;
51+
}
52+
}
53+
54+
private static EnvVars getEnvironment(Run run) {
55+
EnvVars environment = null;
56+
try {
57+
environment = run.getEnvironment(null);
58+
} catch (IOException | InterruptedException e) {
59+
logger.error("Can not get Run(id: " + run.getId() + ") Environment: " + e.getMessage());
60+
}
61+
return environment;
62+
}
63+
64+
private static List<String> getGlobalParamsList() {
65+
try {
66+
return Stream.of(RunnerMiscSettingsGlobalConfiguration.getInstance().getOutputEnvironmentParameters()
67+
.split(SPLIT_SYMBOL)).filter(p -> !StringUtils.isNullOrEmpty(p)).collect(Collectors.toList());
68+
} catch (NullPointerException ignored) {
69+
return Collections.emptyList();
70+
}
71+
}
72+
73+
private static List<String> getJobParamsList(Run run) {
74+
Job<?, ?> job = run.getParent();
75+
if (job instanceof BuildableItemWithBuildWrappers) {
76+
OutputEnvironmentVariablesBuildWrapper outputEnvVarsBuildWrapper = ((BuildableItemWithBuildWrappers) job)
77+
.getBuildWrappersList().get(OutputEnvironmentVariablesBuildWrapper.class);
78+
if (outputEnvVarsBuildWrapper != null) {
79+
String paramsStr = outputEnvVarsBuildWrapper.getOutputEnvironmentParameters();
80+
if (!StringUtils.isNullOrEmpty(paramsStr)) {
81+
String[] params = paramsStr.split(SPLIT_SYMBOL);
82+
return Stream.of(params).filter(p -> !StringUtils.isNullOrEmpty(p)).collect(Collectors.toList());
83+
}
84+
}
85+
}
86+
return Collections.emptyList();
87+
}
88+
}

src/main/java/com/microfocus/application/automation/tools/octane/events/WorkflowListenerOctaneImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ private void sendPipelineFinishedEvent(WorkflowRun parentRun) {
169169
.setDuration(parentRun.getDuration())
170170
.setResult(BuildHandlerUtils.translateRunResult(parentRun))
171171
.setCauses(CIEventCausesFactory.processCauses(parentRun))
172-
.setTestResultExpected(hasTests);
172+
.setTestResultExpected(hasTests)
173+
.setEnvironmentOutputtedParameters(OutputEnvironmentParametersHelper.getOutputEnvironmentParams(parentRun));
173174
CIJenkinsServicesImpl.publishEventToRelevantClients(event);
174175
}
175176

src/main/java/com/microfocus/application/automation/tools/octane/model/processors/projects/AbstractProjectProcessor.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import com.hp.octane.integrations.utils.SdkConstants;
3636
import com.hp.octane.integrations.utils.SdkStringUtils;
3737
import com.microfocus.application.automation.tools.octane.configuration.SDKBasedLoggerProvider;
38+
import com.microfocus.application.automation.tools.octane.events.OutputEnvironmentParametersHelper;
3839
import com.microfocus.application.automation.tools.octane.executor.UftConstants;
3940
import com.microfocus.application.automation.tools.octane.model.processors.builders.AbstractBuilderProcessor;
4041
import com.microfocus.application.automation.tools.octane.model.processors.builders.BuildTriggerProcessor;
@@ -209,6 +210,7 @@ public CIBuildStatusInfo getBuildStatus(String paramName, String paramValue) {
209210
} else {
210211
status.setBuildStatus(CIBuildStatus.FINISHED);
211212
status.setResult(BuildHandlerUtils.translateRunResult(aBuild));
213+
status.setEnvironmentOutputtedParameters(OutputEnvironmentParametersHelper.getOutputEnvironmentParams(aBuild));
212214
}
213215
status.setAllBuildParams(ParameterProcessors.getInstances(aBuild));
214216
status.setBuildCiId(BuildHandlerUtils.getBuildCiId(aBuild));
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.microfocus.application.automation.tools.settings;
2+
3+
import com.microfocus.application.automation.tools.sse.common.StringUtils;
4+
import hudson.EnvVars;
5+
import hudson.Extension;
6+
import hudson.Launcher;
7+
import hudson.model.AbstractBuild;
8+
import hudson.model.AbstractProject;
9+
import hudson.model.BuildListener;
10+
import hudson.tasks.BuildWrapper;
11+
import hudson.tasks.BuildWrapperDescriptor;
12+
import org.kohsuke.stapler.DataBoundConstructor;
13+
import org.kohsuke.stapler.DataBoundSetter;
14+
15+
import java.io.IOException;
16+
import java.io.Serializable;
17+
import java.util.Map;
18+
import java.util.stream.Collectors;
19+
import java.util.stream.Stream;
20+
21+
import static com.microfocus.application.automation.tools.octane.events.OutputEnvironmentParametersHelper.SPLIT_SYMBOL;
22+
23+
public class OutputEnvironmentVariablesBuildWrapper extends BuildWrapper implements Serializable {
24+
25+
private String outputEnvironmentParameters;
26+
@DataBoundConstructor
27+
public OutputEnvironmentVariablesBuildWrapper(String outputEnvironmentParameters) {
28+
setOutputEnvironmentParameters(outputEnvironmentParameters);
29+
}
30+
31+
public String getOutputEnvironmentParameters() {
32+
return outputEnvironmentParameters;
33+
}
34+
@DataBoundSetter
35+
public void setOutputEnvironmentParameters(String outputEnvironmentParameters) {
36+
this.outputEnvironmentParameters = getValidatedOutputEnvironmentParameters(outputEnvironmentParameters);
37+
}
38+
39+
@Override
40+
public Environment setUp(AbstractBuild build, Launcher launcher, BuildListener listener) throws IOException, InterruptedException {
41+
EnvVars envVars = build.getEnvironment(listener);
42+
return new Environment() {
43+
@Override
44+
public void buildEnvVars(Map<String, String> env) {
45+
env.putAll(envVars);
46+
}
47+
};
48+
}
49+
50+
private String getValidatedOutputEnvironmentParameters(String envParams) {
51+
String[] params = envParams.split("\\s++");
52+
return Stream.of(params).filter(p -> !StringUtils.isNullOrEmpty(p))
53+
.collect(Collectors.joining(SPLIT_SYMBOL));
54+
}
55+
56+
57+
@Extension
58+
public static final class DescriptorImpl extends BuildWrapperDescriptor {
59+
60+
@Override
61+
public boolean isApplicable(AbstractProject<?, ?> item) {
62+
return true;
63+
}
64+
65+
@Override
66+
public String getDisplayName() {
67+
return "Define list of Environment Variables to be sent to ALM Octane";
68+
}
69+
}
70+
71+
}

src/main/java/com/microfocus/application/automation/tools/settings/RunnerMiscSettingsGlobalConfiguration.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@
4343
import java.util.stream.Collectors;
4444
import java.util.stream.Stream;
4545

46+
import static com.microfocus.application.automation.tools.octane.events.OutputEnvironmentParametersHelper.SPLIT_SYMBOL;
47+
4648
@Extension(ordinal = 1, optional = true)
4749
public class RunnerMiscSettingsGlobalConfiguration extends GlobalConfiguration implements Serializable {
4850

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

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

5760
private String dateFormat;
5861
private String defaultBranches;
62+
private String outputEnvironmentParameters;
5963
private boolean agentToControllerEnabled;
6064

6165
@DataBoundConstructor
62-
public RunnerMiscSettingsGlobalConfiguration(String mfDateFormat, String defaultBranches,boolean agentToControllerEnabled) {
66+
public RunnerMiscSettingsGlobalConfiguration(String mfDateFormat, String defaultBranches, String outputEnvironmentParameters, boolean agentToControllerEnabled) {
6367
setDateFormat(mfDateFormat);
6468
setDefaultBranches(defaultBranches);
6569
setAgentToControllerEnabled(agentToControllerEnabled);
70+
setOutputEnvironmentParameters(outputEnvironmentParameters);
6671
}
6772

6873
public RunnerMiscSettingsGlobalConfiguration() {
@@ -102,14 +107,27 @@ public void setDefaultBranches(String defaultBranches) {
102107
save();
103108
}
104109

110+
public String getOutputEnvironmentParameters() {
111+
return outputEnvironmentParameters;
112+
}
105113

114+
public void setOutputEnvironmentParameters(String outputEnvironmentParameters) {
115+
this.outputEnvironmentParameters = getValidatedOutputEnvironmentParameters(outputEnvironmentParameters);
116+
save();
117+
}
106118

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

125+
private String getValidatedOutputEnvironmentParameters(String envParams) {
126+
String[] params = envParams.split("\\s++");
127+
return Stream.of(params).filter(p -> !StringUtils.isNullOrEmpty(p))
128+
.collect(Collectors.joining(SPLIT_SYMBOL));
129+
}
130+
113131
public DateTimeFormatter getDateFormatter() {
114132
return dateFormat != null ? DateTimeFormatter.ofPattern(dateFormat) : DateTimeFormatter.ofPattern(DEFAULT_UFT_DATE_PATTERN1);
115133
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?jelly escape-by-default='true'?>
2+
<!--
3+
~ Certain versions of software and/or documents ("Material") accessible here may contain branding from
4+
~ Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017,
5+
~ the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP
6+
~ and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
7+
~ marks are the property of their respective owners.
8+
~ __________________________________________________________________
9+
~ MIT License
10+
~
11+
~ (c) Copyright 2012-2021 Micro Focus or one of its affiliates.
12+
~
13+
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
14+
~ documentation files (the "Software"), to deal in the Software without restriction, including without limitation
15+
~ the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
16+
~ and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
17+
~
18+
~ The above copyright notice and this permission notice shall be included in all copies or
19+
~ substantial portions of the Software.
20+
~
21+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
22+
~ THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24+
~ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
~ SOFTWARE.
26+
~
27+
~ ___________________________________________________________________
28+
-->
29+
30+
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
31+
<f:entry title="List of output Environment Variables" field="outputEnvironmentParameters">
32+
<f:textarea name="mf.outputEnvironmentParameters" value="${instance.outputEnvironmentParameters}"/>
33+
</f:entry>
34+
</j:jelly>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<!--
2+
~ Certain versions of software and/or documents ("Material") accessible here may contain branding from
3+
~ Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017,
4+
~ the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP
5+
~ and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE
6+
~ marks are the property of their respective owners.
7+
~ __________________________________________________________________
8+
~ MIT License
9+
~
10+
~ (c) Copyright 2012-2021 Micro Focus or one of its affiliates.
11+
~
12+
~ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13+
~ documentation files (the "Software"), to deal in the Software without restriction, including without limitation
14+
~ the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
15+
~ and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16+
~
17+
~ The above copyright notice and this permission notice shall be included in all copies or
18+
~ substantial portions of the Software.
19+
~
20+
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
21+
~ THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23+
~ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24+
~ SOFTWARE.
25+
~
26+
~ ___________________________________________________________________
27+
-->
28+
29+
<div>
30+
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.
31+
<br/>
32+
Delimit multiple properties with a space or EOL.
33+
</div>

src/main/resources/com/microfocus/application/automation/tools/settings/RunnerMiscSettingsGlobalConfiguration/config.jelly

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@
3535
<f:entry title="Default branches" field="defaultBranches" >
3636
<f:textbox name="mf.defaultBranches" value="${instance.defaultBranches}" />
3737
</f:entry>
38+
<f:entry title="List of output Environment Variables" field="outputEnvironmentParameters" >
39+
<f:textarea name="mf.outputEnvironmentParameters" value="${instance.outputEnvironmentParameters}" default="${instance.DEFAULT_OUTPUT_ENVIRONMENT_PARAMETERS}" />
40+
</f:entry>
3841
<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.">
3942
<f:checkbox name="agentToControllerEnabled" checked="${instance.agentToControllerEnabled}"/>
4043
</f:entry>

0 commit comments

Comments
 (0)