From 02a09d20ed18aa348908949a3c47745ac8ebb6c1 Mon Sep 17 00:00:00 2001 From: Anatoli Kalbasin Date: Tue, 14 Jan 2025 12:15:46 +0100 Subject: [PATCH] remove duplicate methods Signed-off-by: Anatoli Kalbasin --- README.md | 6 +- .../files/dsn/WriteDirToDatasetStep.kt | 6 +- .../files/dsn/WriteFileToDatasetStep.kt | 14 +- .../files/dsn/WriteFileToMemberStep.kt | 11 +- .../classic/files/dsn/WriteToDatasetStep.kt | 14 +- .../classic/files/dsn/WriteToMemberStep.kt | 11 +- .../files/dsn/WriteDirToDatasetDeclarative.kt | 4 +- .../files/dsn/WriteFIleToMemberDeclarative.kt | 30 ++-- .../dsn/WriteFileToDatasetDeclarative.kt | 34 ++-- .../files/dsn/WriteToDatasetDeclarative.kt | 15 +- .../files/dsn/WriteToMemberDeclarative.kt | 16 +- .../org/zowe/zdevops/logic/WriteOperation.kt | 155 ++++++++---------- .../dsn/WriteFileToDatasetStep/config.jelly | 2 +- .../dsn/WriteFileToMemberStep/config.jelly | 2 + .../files/dsn/WriteToDatasetStep/config.jelly | 2 +- .../files/dsn/WriteToMemberStep/config.jelly | 3 + .../files/dsn/WriteFileToMemberStepSpec.kt | 8 +- .../files/dsn/WriteToMemberStepSpec.kt | 8 +- .../dsn/WriteDirToDatasetDeclarativeSpec.kt | 16 +- .../dsn/WriteFileToMemberDeclarativeSpec.kt | 8 +- 20 files changed, 198 insertions(+), 167 deletions(-) diff --git a/README.md b/README.md index 8a6358a..f0d7b26 100644 --- a/README.md +++ b/README.md @@ -47,11 +47,11 @@ stage ("stage-name") { downloadDS dsn:"EXAMPLE.DATASET(MEMBER)", vol:"VOL001" allocateDS dsn:"EXAMPLE.DATASET", alcUnit:"TRK", dsOrg:"PS", primary:1, secondary:1, recFm:"FB", failOnExist:"False" writeFileToDS dsn:"EXAMPLE.DATASET", file:"workspaceFile" + writeFileToDS dsn:"EXAMPLE.DATASET(MEMBER)", file:"workspaceFile" writeFileToDS dsn:"EXAMPLE.DATASET", file:"D:\\files\\localFile" + writeFileToDS dsn:"EXAMPLE.DATASET(MEMBER)", file:"D:\\files\\localFile" writeToDS dsn:"EXAMPLE.DATASET", text:"Write this string to dataset" - writeFileToMember dsn:"EXAMPLE.DATASET", member:"MEMBER", file:"workspaceFile" - writeFileToMember dsn:"EXAMPLE.DATASET", member:"MEMBER", file:"D:\\files\\localFile" - writeToMember dsn:"EXAMPLE.DATASET", member:"MEMBER", text:"Write this string to member" + writeToDS dsn:"EXAMPLE.DATASET(MEMBER)", text:"Write this string to dataset member" writeDirToDS dir: "app/src/main/cbl/", dsn: "EXAMPLE.DATASET" writeDirToDS dir: "D:\\resources\\cbl", dsn: "EXAMPLE.DATASET", isLocalPath: true diff --git a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteDirToDatasetStep.kt b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteDirToDatasetStep.kt index d7d1282..c5be038 100644 --- a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteDirToDatasetStep.kt +++ b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteDirToDatasetStep.kt @@ -24,14 +24,14 @@ import org.kohsuke.stapler.DataBoundSetter import org.kohsuke.stapler.QueryParameter import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.classic.core.AbstractBuildStep -import org.zowe.zdevops.logic.writeDirectoryToDataset +import org.zowe.zdevops.logic.writeDirectoryToPdsJenkins import org.zowe.zdevops.utils.validateDatasetName import org.zowe.zdevops.utils.validateFieldIsNotEmpty /** * A freestyle job Jenkins class for writing the contents of a directory to a PDS/E dataset. - * @see org.zowe.zdevops.declarative.jobs.WriteDirToDatasetDeclarative + * @see org.zowe.zdevops.declarative.files.dsn.WriteDirToDatasetDeclarative */ class WriteDirToDatasetStep @DataBoundConstructor @@ -58,7 +58,7 @@ constructor( zosConnection: ZOSConnection ) { val workspace = build.executor?.currentWorkspace ?: throw AbortException("'build.executor' was null") - writeDirectoryToDataset(dsn, dir, isLocalPath, workspace, listener, zosConnection) + writeDirectoryToPdsJenkins(dsn, dir, isLocalPath, workspace, listener, zosConnection) } @Extension diff --git a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep.kt b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep.kt index 0db438b..4c26aa5 100644 --- a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep.kt +++ b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2023-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2023 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.files.dsn @@ -24,8 +28,8 @@ import org.kohsuke.stapler.bind.JavaScriptMethod import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.Messages import org.zowe.zdevops.classic.core.AbstractBuildStep -import org.zowe.zdevops.logic.writeToDataset -import org.zowe.zdevops.utils.validateDatasetName +import org.zowe.zdevops.logic.writeTextToDatasetJenkins +import org.zowe.zdevops.utils.validateDsnOrDsnMemberName import org.zowe.zdevops.utils.validateFieldIsNotEmpty import java.io.File @@ -93,7 +97,7 @@ constructor( listener.logger.println(Messages.zdevops_declarative_writing_DS_from_file(dsn, file?.name, zosConnection.host, zosConnection.zosmfPort)) val fileContent = file?.readText() if (fileContent != null) { - writeToDataset(listener, zosConnection, dsn, fileContent) + writeTextToDatasetJenkins(listener, zosConnection, dsn, fileContent) } } @@ -155,7 +159,7 @@ constructor( * @return FormValidation.ok() if the dataset name is valid, or an error message otherwise */ fun doCheckDsn(@QueryParameter dsn: String): FormValidation? { - return validateDatasetName(dsn) + return validateDsnOrDsnMemberName(dsn) } /** diff --git a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep.kt b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep.kt index d412e6a..a08a4e4 100644 --- a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep.kt +++ b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2023-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2023 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.files.dsn @@ -24,7 +28,7 @@ import org.kohsuke.stapler.bind.JavaScriptMethod import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.Messages import org.zowe.zdevops.classic.core.AbstractBuildStep -import org.zowe.zdevops.logic.writeToMember +import org.zowe.zdevops.logic.writeTextToDatasetJenkins import org.zowe.zdevops.utils.validateDatasetName import org.zowe.zdevops.utils.validateFieldIsNotEmpty import org.zowe.zdevops.utils.validateMemberName @@ -94,9 +98,10 @@ constructor( else -> throw AbortException(Messages.zdevops_classic_write_options_invalid()) } listener.logger.println(Messages.zdevops_declarative_writing_DS_from_file(dsn, file?.name, zosConnection.host, zosConnection.zosmfPort)) + listener.logger.println("[WARNING] - The method is deprecated. Please, consider switching to `Write File to Dataset` step.") val fileContent = file?.readText() if (fileContent != null) { - writeToMember(listener, zosConnection, dsn, member, fileContent) + writeTextToDatasetJenkins(listener, zosConnection, "$dsn($member)", fileContent) } } diff --git a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep.kt b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep.kt index a63843e..eeb1752 100644 --- a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep.kt +++ b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2023-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2023 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.files.dsn @@ -20,8 +24,8 @@ import org.kohsuke.stapler.QueryParameter import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.Messages import org.zowe.zdevops.classic.core.AbstractBuildStep -import org.zowe.zdevops.logic.writeToDataset -import org.zowe.zdevops.utils.validateDatasetName +import org.zowe.zdevops.logic.writeTextToDatasetJenkins +import org.zowe.zdevops.utils.validateDsnOrDsnMemberName import org.zowe.zdevops.utils.validateFieldIsNotEmpty /** @@ -57,7 +61,7 @@ constructor( zosConnection: ZOSConnection ) { listener.logger.println(Messages.zdevops_declarative_writing_DS_from_input(dsn, zosConnection.host, zosConnection.zosmfPort)) - writeToDataset(listener, zosConnection, dsn, text) + writeTextToDatasetJenkins(listener, zosConnection, dsn, text) } /** @@ -72,7 +76,7 @@ constructor( * @return FormValidation.ok() if the dataset name is valid, or an error message otherwise */ fun doCheckDsn(@QueryParameter dsn: String): FormValidation? { - return validateDatasetName(dsn) + return validateDsnOrDsnMemberName(dsn) } /** diff --git a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep.kt b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep.kt index c0eb99b..aa1d76c 100644 --- a/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep.kt +++ b/src/main/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2023-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2023 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.files.dsn @@ -20,7 +24,7 @@ import org.kohsuke.stapler.QueryParameter import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.Messages import org.zowe.zdevops.classic.core.AbstractBuildStep -import org.zowe.zdevops.logic.writeToMember +import org.zowe.zdevops.logic.writeTextToDatasetJenkins import org.zowe.zdevops.utils.validateDatasetName import org.zowe.zdevops.utils.validateFieldIsNotEmpty import org.zowe.zdevops.utils.validateMemberName @@ -60,7 +64,8 @@ constructor( zosConnection: ZOSConnection ) { listener.logger.println(Messages.zdevops_declarative_writing_DS_from_input(dsn, zosConnection.host, zosConnection.zosmfPort)) - writeToMember(listener, zosConnection, dsn, member, text) + listener.logger.println("[WARNING] - The method is deprecated. Please, consider switching to `Write to Dataset` step.") + writeTextToDatasetJenkins(listener, zosConnection, "$dsn($member)", text) } /** diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarative.kt index ed1ec28..ecf82d5 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarative.kt @@ -25,7 +25,7 @@ import org.kohsuke.stapler.DataBoundConstructor import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.declarative.core.AbstractZosmfAction import org.zowe.zdevops.declarative.jobs.zMessages -import org.zowe.zdevops.logic.writeDirectoryToDataset +import org.zowe.zdevops.logic.writeDirectoryToPdsJenkins /** * A declarative class for writing the contents of a directory to a PDS/E dataset. @@ -67,7 +67,7 @@ class WriteDirToDatasetDeclarative listener: TaskListener, zosConnection: ZOSConnection ) { - writeDirectoryToDataset(dsn, dir, isLocalPath, workspace, listener, zosConnection) + writeDirectoryToPdsJenkins(dsn, dir, isLocalPath, workspace, listener, zosConnection) } @Symbol("writeDirToDS") diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFIleToMemberDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFIleToMemberDeclarative.kt index 20856e5..d85549b 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFIleToMemberDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFIleToMemberDeclarative.kt @@ -1,25 +1,31 @@ /* + * Copyright (c) 2022-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2022 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.files.dsn -import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection -import org.zowe.kotlinsdk.zowe.client.sdk.zosfiles.ZosDsn -import org.zowe.zdevops.declarative.core.AbstractZosmfAction -import hudson.* +import hudson.EnvVars +import hudson.Extension import hudson.FilePath +import hudson.Launcher import hudson.model.Run import hudson.model.TaskListener import org.jenkinsci.Symbol import org.kohsuke.stapler.DataBoundConstructor +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.declarative.core.AbstractZosmfAction import org.zowe.zdevops.declarative.jobs.zMessages +import org.zowe.zdevops.logic.writeTextToDatasetJenkins import java.io.File import java.nio.file.Paths @@ -47,18 +53,8 @@ class WriteFIleToMemberDeclarative @DataBoundConstructor constructor(private val File("$workspacePath$file") } - val targetDS = ZosDsn(zosConnection).getDatasetInfo(dsn) - val targetDSLRECL = targetDS.recordLength ?: throw AbortException(zMessages.zdevops_declarative_writing_DS_no_info(dsn)) - val ineligibleStrings = textFile - .readLines() - .map { it.length } - .fold(0) { result, currStrLength -> if (currStrLength > targetDSLRECL) result + 1 else result } - if (ineligibleStrings > 0) { - throw AbortException(zMessages.zdevops_declarative_writing_DS_ineligible_strings(ineligibleStrings, dsn)) - } - val textString = textFile.readText().replace("\r","") - ZosDsn(zosConnection).writeDsn(dsn, member, textString.toByteArray()) - listener.logger.println(zMessages.zdevops_declarative_writing_DS_success(dsn)) + listener.logger.println("[WARNING] - The method `writeFileToMember` is deprecated. Please, consider switching to `writeFileToDataset`.") + writeTextToDatasetJenkins(listener, zosConnection, "$dsn($member)", textFile.readText()) } diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToDatasetDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToDatasetDeclarative.kt index 78a8fd7..63c11d9 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToDatasetDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToDatasetDeclarative.kt @@ -1,25 +1,31 @@ /* + * Copyright (c) 2022-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2022 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.files.dsn -import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection -import org.zowe.kotlinsdk.zowe.client.sdk.zosfiles.ZosDsn -import org.zowe.zdevops.declarative.core.AbstractZosmfAction -import hudson.* +import hudson.EnvVars +import hudson.Extension import hudson.FilePath +import hudson.Launcher import hudson.model.Run import hudson.model.TaskListener import org.jenkinsci.Symbol import org.kohsuke.stapler.DataBoundConstructor +import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection +import org.zowe.zdevops.declarative.core.AbstractZosmfAction import org.zowe.zdevops.declarative.jobs.zMessages +import org.zowe.zdevops.logic.writeTextToDatasetJenkins import java.io.File import java.nio.file.Paths @@ -46,23 +52,7 @@ class WriteFileToDatasetDeclarative @DataBoundConstructor constructor(private va File("$workspacePath$file") } - val targetDS = ZosDsn(zosConnection).getDatasetInfo(dsn) - if (targetDS.recordLength == null) { - throw AbortException(zMessages.zdevops_declarative_writing_DS_no_info(dsn)) - } - var ineligibleStrings = 0 - textFile.readLines().forEach { - if (it.length > targetDS.recordLength!!) { - ineligibleStrings++ - } - } - if (ineligibleStrings > 0) { - throw AbortException(zMessages.zdevops_declarative_writing_DS_ineligible_strings(ineligibleStrings,dsn)) - } else { - val textString = textFile.readText().replace("\r","") - ZosDsn(zosConnection).writeDsn(dsn, textString.toByteArray()) - listener.logger.println(zMessages.zdevops_declarative_writing_DS_success(dsn)) - } + writeTextToDatasetJenkins(listener, zosConnection, dsn, textFile.readText()) } diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToDatasetDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToDatasetDeclarative.kt index 82a7cec..3c7a717 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToDatasetDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToDatasetDeclarative.kt @@ -1,16 +1,23 @@ /* + * Copyright (c) 2022-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2022 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.files.dsn -import hudson.* +import hudson.EnvVars +import hudson.Extension +import hudson.FilePath +import hudson.Launcher import hudson.model.Run import hudson.model.TaskListener import org.jenkinsci.Symbol @@ -18,7 +25,7 @@ import org.kohsuke.stapler.DataBoundConstructor import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.declarative.core.AbstractZosmfAction import org.zowe.zdevops.declarative.jobs.zMessages -import org.zowe.zdevops.logic.writeToDataset +import org.zowe.zdevops.logic.writeTextToDatasetJenkins class WriteToDatasetDeclarative @DataBoundConstructor constructor(private val dsn: String, private val text: String) : @@ -34,7 +41,7 @@ class WriteToDatasetDeclarative @DataBoundConstructor constructor(private val ds listener: TaskListener, zosConnection: ZOSConnection ) { - writeToDataset(listener, zosConnection, dsn, text) + writeTextToDatasetJenkins(listener, zosConnection, dsn, text) } diff --git a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToMemberDeclarative.kt b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToMemberDeclarative.kt index f65554f..0be0936 100644 --- a/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToMemberDeclarative.kt +++ b/src/main/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteToMemberDeclarative.kt @@ -1,16 +1,23 @@ /* + * Copyright (c) 2022-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2022 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.files.dsn -import hudson.* +import hudson.EnvVars +import hudson.Extension +import hudson.FilePath +import hudson.Launcher import hudson.model.Run import hudson.model.TaskListener import org.jenkinsci.Symbol @@ -18,7 +25,7 @@ import org.kohsuke.stapler.DataBoundConstructor import org.zowe.kotlinsdk.zowe.client.sdk.core.ZOSConnection import org.zowe.zdevops.declarative.core.AbstractZosmfAction import org.zowe.zdevops.declarative.jobs.zMessages -import org.zowe.zdevops.logic.writeToMember +import org.zowe.zdevops.logic.writeTextToDatasetJenkins class WriteToMemberDeclarative @DataBoundConstructor constructor(private val dsn: String, private val member: String, @@ -35,7 +42,8 @@ class WriteToMemberDeclarative @DataBoundConstructor constructor(private val dsn listener: TaskListener, zosConnection: ZOSConnection ) { - writeToMember(listener, zosConnection, dsn, member, text) + listener.logger.println("[WARNING] - The method `writeToMember` is deprecated. Please, consider switching to `writeToDS`.") + writeTextToDatasetJenkins(listener, zosConnection, "$dsn($member)", text) } diff --git a/src/main/kotlin/org/zowe/zdevops/logic/WriteOperation.kt b/src/main/kotlin/org/zowe/zdevops/logic/WriteOperation.kt index fd6da66..59e5902 100644 --- a/src/main/kotlin/org/zowe/zdevops/logic/WriteOperation.kt +++ b/src/main/kotlin/org/zowe/zdevops/logic/WriteOperation.kt @@ -27,87 +27,46 @@ import java.io.File /** - * Validates the text to be written to a dataset + * Parse dataset name and member name out of full dataset name * - * @param listener The listener for logging messages - * @param zosConnection The ZOSConnection for interacting with z/OS - * @param dsn The name of the dataset - * @param text The text content to be written - * @throws AbortException if the text is empty or contains ineligible strings. + * @param fullName The name of a dataset in the form `DATASET.NAME` or `DATASET.NAME(MEMBER)` + * @return (dataset, member) pair, where member name can be null */ -private fun validateTextForDataset( - listener: TaskListener, - zosConnection: ZOSConnection, - dsn: String, - text: String, - ) { - if(text == "") { - listener.logger.println(Messages.zdevops_declarative_writing_skip()) - return - } - - val stringList = text.split('\n') - val targetDS = ZosDsn(zosConnection).getDatasetInfo(dsn) - if (targetDS.recordLength == null) { - throw AbortException(Messages.zdevops_declarative_writing_DS_no_info(dsn)) - } - var ineligibleStrings = 0 - stringList.forEach { - if (it.length > targetDS.recordLength!!) { - ineligibleStrings++ - } - } - if (ineligibleStrings > 0) { - throw AbortException(Messages.zdevops_declarative_writing_DS_ineligible_strings(ineligibleStrings,dsn)) - } +fun parseDatasetName(fullName: String): Pair { + val dataset = fullName.substringBefore("(") + val member = if (fullName.contains("(")) { + fullName.substringAfter("(").substringBefore(")").takeIf { it.isNotEmpty() } + } else { + null + } + return dataset to member } /** - * Writes the text content to a dataset + * Jenkins method that writes a text to a dataset * * @param listener The listener for logging messages * @param zosConnection The ZOSConnection for interacting with z/OS - * @param dsn The name of the dataset + * @param dsn The name of a dataset in the form `DATASET.NAME` or `DATASET.NAME(MEMBER)` * @param text The text content to be written * @throws AbortException if the text is not valid for the dataset or an error occurs during the write operation */ -fun writeToDataset(listener: TaskListener, - zosConnection: ZOSConnection, - dsn: String, - text: String, +fun writeTextToDatasetJenkins(listener: TaskListener, + zosConnection: ZOSConnection, + dsn: String, + text: String, ) { - validateTextForDataset(listener, zosConnection, dsn, text) - val textByteArray = text.replace("\r","").toByteArray() - runMFTryCatchWrappedQuery(listener) { - ZosDsn(zosConnection).writeDsn(dsn, textByteArray) - } - listener.logger.println(Messages.zdevops_declarative_writing_DS_success(dsn)) + val (dataset, member) = parseDatasetName(dsn) + validateExceedingLines(zosConnection, dataset, text.split('\n')) + if (member.isNullOrBlank()) { + writeToPS(text, dataset, zosConnection) + } else { + writeToPDS(text, dataset, member, zosConnection) + } + listener.logger.println(Messages.zdevops_declarative_writing_DS_success(dsn)) } -/** - * Writes the text content to a member - * - * @param listener The listener for logging messages - * @param zosConnection The ZOSConnection for interacting with z/OS - * @param dsn The name of the dataset - * @param member The name of the member - * @param text The text content to be written - * @throws AbortException if the text is not valid for the dataset or an error occurs during the write operation - */ -fun writeToMember(listener: TaskListener, - zosConnection: ZOSConnection, - dsn: String, - member: String, - text: String,) { - validateTextForDataset(listener, zosConnection, dsn, text) - val textByteArray = text.replace("\r","").toByteArray() - runMFTryCatchWrappedQuery(listener) { - ZosDsn(zosConnection).writeDsn(dsn, member, textByteArray) - } - listener.logger.println(Messages.zdevops_declarative_writing_DS_success(dsn)) -} - //TODO: docs fun writeToFile(listener: TaskListener, zosConnection: ZOSConnection, @@ -131,12 +90,13 @@ fun writeToFile(listener: TaskListener, /** * Finds the line numbers in a file where the line length exceeds a specified record length (LRECL). * - * @param file the file to be read and analyzed. +// * @param file the file to be read and analyzed. + * @param text the text to validate for lines exceeding the target dataset's record length * @param lrecl the maximum allowed record length for each line. * @return a list of line numbers where the line length exceeds the specified LRECL. */ -fun findLinesExceedingRecordLength(file: File, lrecl: Int): List { - return file.readLines().mapIndexedNotNull { index, line -> +fun findLinesExceedingRecordLength(text: List, lrecl: Int): List { + return text.mapIndexedNotNull { index, line -> if(line.length > lrecl) index + 1 else null } } @@ -153,7 +113,7 @@ fun findLinesExceedingRecordLength(file: File, lrecl: Int): List { * @throws AbortException if the dataset information cannot be retrieved. * @throws IllegalArgumentException if the provided directory path does not exist or is invalid. */ -fun writeDirectoryToDataset( +fun writeDirectoryToPdsJenkins( dsn: String, dir: String, isLocalPath: Boolean, @@ -163,9 +123,6 @@ fun writeDirectoryToDataset( ) { listener.logger.println(zMessages.zdevops_declarative_writing_DS_from_dir(dsn, dir, zosConnection.host, zosConnection.zosmfPort)) - val targetDS = ZosDsn(zosConnection).getDatasetInfo(dsn) - val lrecl = targetDS.recordLength ?: throw AbortException(zMessages.zdevops_declarative_writing_DS_no_info(dsn)) - val localOrWsDirPath = resolveDirectoryPath(dir, isLocalPath, listener, workspace) validatePathExists(localOrWsDirPath) validatePathLeadsToDirectory(localOrWsDirPath) @@ -176,7 +133,7 @@ fun writeDirectoryToDataset( } listener.logger.println("Found ${directoryEntries.size} entries in the directory:") - directoryEntries.forEachIndexed { index, entry -> processEntry(index, entry, dsn, listener, lrecl, zosConnection) } + directoryEntries.forEachIndexed { index, entry -> processEntry(index, entry, dsn, listener, zosConnection) } listener.logger.println(zMessages.zdevops_declarative_writing_DS_success(dsn)) } @@ -233,7 +190,6 @@ fun validatePathLeadsToDirectory(dir: File) { * @param entry the file or directory being processed. * @param dsn the dataset name (DSN) where the file will be written. * @param listener the task listener used for logging progress and feedback. - * @param lrecl the record length of the target dataset. * @param zosConnection the connection information for interacting with z/OS system. * * Logs details about the entry and writes the file to the dataset if the entry is a regular file. @@ -244,7 +200,6 @@ private fun processEntry( entry: File, dsn: String, listener: TaskListener, - lrecl: Int, zosConnection: ZOSConnection ) { if (entry.isDirectory) { @@ -252,7 +207,7 @@ private fun processEntry( return } listener.logger.println("[$index] - Processing entry: '${entry.name}'") - writeFileToDataset(entry, dsn, lrecl, zosConnection) + writeFileToPDS(entry, dsn, zosConnection) } /** @@ -260,33 +215,48 @@ private fun processEntry( * * @param file the file whose contents will be written to the dataset. * @param dsn the dataset name (DSN) where the file content will be written. - * @param lrecl the record length of the target dataset. * @param zosConnection the connection information for interacting with the z/OS system. * * @throws AbortException if the file contains lines exceeding the dataset's record length. * - * This function validates that all lines in the file conform to the specified record length (LRECL). - * If any line exceeds this length, an error is logged, and the operation is aborted. - * Upon successful validation, the file content is written to a member in the specified dataset, + * The file content is written to a member in the specified dataset, * using the file's name (without extension) as the member name. */ -fun writeFileToDataset( +fun writeFileToPDS( file: File, dsn: String, - lrecl: Int, zosConnection: ZOSConnection, ) { - val exceedingLines = findLinesExceedingRecordLength(file, lrecl) + validateExceedingLines(zosConnection, dsn, file.readLines()) + writeToPDS(file.readText(), dsn, file.nameWithoutExtension, zosConnection) +} + +/** + * This function validates that text conform to the specified record length (LRECL). + * If any line exceeds this length, an error is logged, and the operation is aborted. + * + * @param dsn the dataset name (DSN) where the file content will be written. + * @param text the text to validate for lines exceeding the target dataset's record length + * @param zosConnection the connection information for interacting with the z/OS system. + * @throws AbortException if the file contains lines exceeding the dataset's record length. + */ +fun validateExceedingLines( + zosConnection: ZOSConnection, + dsn: String, + text: List +) { + val targetDS = ZosDsn(zosConnection).getDatasetInfo(dsn) + val lrecl = targetDS.recordLength ?: throw AbortException(zMessages.zdevops_declarative_writing_DS_no_info(dsn)) + + val exceedingLines = findLinesExceedingRecordLength(text, lrecl) if (exceedingLines.isNotEmpty()) { throw AbortException( - "Error: File '${file.name}' contains lines exceeding record length '$lrecl' of dataset '$dsn'. " + + "Error: Text contains lines exceeding record length '$lrecl' of dataset '$dsn'. " + "Exceeding line numbers: $exceedingLines" ) } - writeToPDS(file.readText(), dsn, file.nameWithoutExtension, zosConnection) } -// TODO How to handle exceptions /** * Writes the provided text to a specific member of a Partitioned Data Set (PDS) on z/OS. * @@ -299,6 +269,17 @@ fun writeToPDS(text: String, dsn: String, member: String, zosConnection: ZOSConn ZosDsn(zosConnection).writeDsn(dsn, member, prepareTextForWritingToDS(text)) } +/** + * Writes the provided text to a Sequential Data Set (PS) on z/OS. + * + * @param text the content to write to the dataset. + * @param dsn the dataset name (DSN). + * @param zosConnection the connection information for interacting with the z/OS system. + */ +fun writeToPS(text: String, dsn: String, zosConnection: ZOSConnection) { + ZosDsn(zosConnection).writeDsn(dsn, prepareTextForWritingToDS(text)) +} + /** * Prepares the provided text for writing to a dataset by removing carriage return characters and converting it to a byte array. * diff --git a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep/config.jelly b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep/config.jelly index c549491..2031cfa 100644 --- a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep/config.jelly +++ b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToDatasetStep/config.jelly @@ -9,7 +9,7 @@ - + diff --git a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep/config.jelly b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep/config.jelly index ca04f62..0fe2acc 100644 --- a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep/config.jelly +++ b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStep/config.jelly @@ -6,6 +6,8 @@ +

The method is deprecated. Please, consider switching to `Write File to Dataset` step.

+ diff --git a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep/config.jelly b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep/config.jelly index 790c607..776e474 100644 --- a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep/config.jelly +++ b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToDatasetStep/config.jelly @@ -6,7 +6,7 @@ - + diff --git a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep/config.jelly b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep/config.jelly index 28010fd..d25015b 100644 --- a/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep/config.jelly +++ b/src/main/resources/org/zowe/zdevops/classic/files/dsn/WriteToMemberStep/config.jelly @@ -3,6 +3,9 @@ + +

The method is deprecated. Please, consider switching to `Write to Dataset` step.

+ diff --git a/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStepSpec.kt b/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStepSpec.kt index 8768abf..71c7853 100644 --- a/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStepSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteFileToMemberStepSpec.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2023-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2023 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.files.dsn @@ -74,6 +78,8 @@ class WriteFileToMemberStepSpec : ShouldSpec({ isWritingToDataset = true } else if (firstArg().contains("Data has been written to dataset")) { isWritten = true + } else if (firstArg().contains("[WARNING] - The method is deprecated")) { + // ignore deprecation warning in tests } else { fail("Unexpected logger message: ${firstArg()}") } diff --git a/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStepSpec.kt b/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStepSpec.kt index 650a3f0..1608067 100644 --- a/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStepSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/classic/files/dsn/WriteToMemberStepSpec.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2023-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2023 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.classic.files.dsn @@ -72,6 +76,8 @@ class WriteToMemberStepSpec : ShouldSpec({ isWritingToDataset = true } else if (firstArg().contains("Data has been written to dataset")) { isWritten = true + } else if (firstArg().contains("[WARNING] - The method is deprecated")) { + // ignore deprecation warning in tests } else { fail("Unexpected logger message: ${firstArg()}") } diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarativeSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarativeSpec.kt index 3a20d18..6aef574 100644 --- a/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarativeSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteDirToDatasetDeclarativeSpec.kt @@ -35,7 +35,7 @@ import org.zowe.zdevops.MockServerFactory import org.zowe.zdevops.declarative.* import org.zowe.zdevops.logic.validatePathExists import org.zowe.zdevops.logic.validatePathLeadsToDirectory -import org.zowe.zdevops.logic.writeFileToDataset +import org.zowe.zdevops.logic.writeFileToPDS import java.io.File import java.io.PrintStream import java.nio.file.Paths @@ -99,15 +99,23 @@ class WriteDirToDatasetDeclarativeSpec : ShouldSpec({ } should("should throw exception if file contains lines exceeding LRECL") { + responseDispatcher.injectEndpoint( + "${this.testCase.name.testName}_listDataSets", + { it?.requestLine?.contains("zosmf/restfiles/ds") ?: false }, + { MockResponse().setBody(responseDispatcher.readMockJson("listDataSets") ?: "") } + ) + val theDataset = "TEST.IJMP.DATASET3" + val tempFile = File.createTempFile("temp", null).apply { - writeText("This is a very long line exceeding the LRECL limit.") + writeText("This is a very long line exceeding the LRECL limit. Even bigger than record length of the " + + "$theDataset dataset") } try { val exception = shouldThrow { - writeFileToDataset(tempFile, "TEST.DATASET", 10, zosConnection) + writeFileToPDS(tempFile, theDataset, zosConnection) } - exception.message shouldBe "Error: File '${tempFile.name}' contains lines exceeding record length '10' of dataset 'TEST.DATASET'. Exceeding line numbers: [1]" + exception.message shouldBe "Error: Text contains lines exceeding record length '80' of dataset '$theDataset'. Exceeding line numbers: [1]" } finally { tempFile.delete() } diff --git a/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToMemberDeclarativeSpec.kt b/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToMemberDeclarativeSpec.kt index 9e6c040..51d8c89 100644 --- a/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToMemberDeclarativeSpec.kt +++ b/src/test/kotlin/org/zowe/zdevops/declarative/files/dsn/WriteFileToMemberDeclarativeSpec.kt @@ -1,11 +1,15 @@ /* + * Copyright (c) 2022-2025 IBA Group. + * * This program and the accompanying materials are made available under the terms of the * Eclipse Public License v2.0 which accompanies this distribution, and is available at * https://www.eclipse.org/legal/epl-v20.html * * SPDX-License-Identifier: EPL-2.0 * - * Copyright IBA Group 2022 + * Contributors: + * IBA Group + * Zowe Community */ package org.zowe.zdevops.declarative.files.dsn @@ -76,6 +80,8 @@ class WriteFileToMemberDeclarativeSpec : ShouldSpec({ isWritingToDataset = true } else if (firstArg().contains("Data has been written to dataset")) { isWritten = true + } else if (firstArg().contains("is deprecated. Please, consider switching")) { + // ignore deprecation warning in tests } else { fail("Unexpected logger message: ${firstArg()}") }