Skip to content

Commit

Permalink
Add links to downloaded datasets in logs after DownloadOperation exec…
Browse files Browse the repository at this point in the history
…ution (#89)
  • Loading branch information
callbacksin authored Nov 12, 2024
1 parent d6b3284 commit ae8477b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
/*
* Copyright (c) 2023-2024 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.steps
Expand Down Expand Up @@ -70,7 +74,8 @@ constructor(
zosConnection: ZOSConnection
) {
val workspace = build.executor?.currentWorkspace!!
downloadDSOrDSMemberByType(dsn, vol, returnEtag, listener, zosConnection, workspace)
val jenkinsJobUrl = build.getEnvironment(listener)["JOB_URL"]
downloadDSOrDSMemberByType(dsn, vol, returnEtag, listener, zosConnection, workspace, jenkinsJobUrl)
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
/*
* Copyright (c) 2022-2024 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.jobs

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
Expand Down Expand Up @@ -47,7 +54,8 @@ class DownloadFileDeclarative @DataBoundConstructor constructor(val dsn: String)
zosConnection: ZOSConnection
) {
val workspacePath = FilePath(null, workspace.remote.replace(workspace.name,""))
downloadDSOrDSMemberByType(dsn, vol, returnEtag, listener, zosConnection, workspacePath)
val jenkinsJobUrl = env["BUILD_URL"] + "/execution/node/3/"
downloadDSOrDSMemberByType(dsn, vol, returnEtag, listener, zosConnection, workspacePath, jenkinsJobUrl)
}


Expand Down
28 changes: 20 additions & 8 deletions src/main/kotlin/org/zowe/zdevops/logic/DownloadOperation.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
/*
* Copyright (c) 2023-2024 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.logic

import hudson.AbortException
import hudson.FilePath
import hudson.console.HyperlinkNote
import hudson.model.TaskListener
import org.apache.commons.io.IOUtils
import org.zowe.kotlinsdk.DatasetOrganization
Expand All @@ -24,7 +29,8 @@ import org.zowe.zdevops.Messages
import java.io.File
import java.io.InputStream
import java.io.StringWriter

import java.net.URLEncoder
import java.nio.charset.StandardCharsets


/**
Expand All @@ -36,14 +42,16 @@ import java.io.StringWriter
* @param zosConnection The connection to the z/OS system.
* @param workspace The workspace where the dataset will be downloaded.
* @param listener The listener for capturing task progress and logs.
* @param jenkinsJobUrl The job/pipeline URL
*/
fun downloadDS(
dsn: String,
vol: String?,
returnEtag: Boolean?,
zosConnection: ZOSConnection,
workspace: FilePath,
listener: TaskListener
listener: TaskListener,
jenkinsJobUrl: String?,
) {
var downloadedDSN: InputStream?
try {
Expand All @@ -55,7 +63,9 @@ fun downloadDS(
IOUtils.copy(downloadedDSN, writer, "UTF-8")
val file = File("$workspace\\$dsn")
file.writeText(writer.toString())
listener.logger.println(Messages.zdevops_declarative_DSN_downloaded_success(dsn))
val urlEncodedDsn = URLEncoder.encode(dsn, StandardCharsets.UTF_8.toString())
listener.logger.println(Messages.zdevops_declarative_DSN_downloaded_success(
HyperlinkNote.encodeTo("${jenkinsJobUrl}ws/$urlEncodedDsn/*view*/", dsn)))
}

/**
Expand All @@ -67,30 +77,32 @@ fun downloadDS(
* @param listener The listener for capturing task progress and logs.
* @param zosConnection The connection to the z/OS system.
* @param workspace The workspace where the dataset will be downloaded.
* @param jenkinsJobUrl The job/pipeline URL
*/
fun downloadDSOrDSMemberByType(
dsn: String,
vol: String?,
returnEtag: Boolean?,
listener: TaskListener,
zosConnection: ZOSConnection,
workspace: FilePath
workspace: FilePath,
jenkinsJobUrl: String?
) {
listener.logger.println(Messages.zdevops_declarative_DSN_downloading(dsn, vol, zosConnection.host, zosConnection.zosmfPort))
val dsnMemberPattern = Regex("[\\w#\$@.-]{1,}\\([\\w#\$@]{1,8}\\)") //means it's a PDS member
if (dsn.contains(dsnMemberPattern)) {
downloadDS(dsn, vol, returnEtag, zosConnection, workspace, listener)
downloadDS(dsn, vol, returnEtag, zosConnection, workspace, listener, jenkinsJobUrl)
} else {
val dsnList = ZosDsnList(zosConnection).listDsn(dsn, ListParams(vol))
if (dsnList.items.isEmpty()) {
throw AbortException("Can't find $dsn ${ if(vol.isNullOrBlank()) "" else "on volume $vol"}")
}
when (dsnList.items.first().datasetOrganization) {
DatasetOrganization.PS -> downloadDS(dsn, vol, returnEtag, zosConnection, workspace, listener)
DatasetOrganization.PS -> downloadDS(dsn, vol, returnEtag, zosConnection, workspace, listener, jenkinsJobUrl)
DatasetOrganization.PO, DatasetOrganization.POE -> {
listener.logger.println(Messages.zdevops_declarative_DSN_downloading_members(dsn))
ZosDsnList(zosConnection).listDsnMembers(dsn, ListParams(vol)).items.forEach {
downloadDS("${dsn}(${it.name})", vol, returnEtag, zosConnection, workspace, listener)
downloadDS("${dsn}(${it.name})", vol, returnEtag, zosConnection, workspace, listener, jenkinsJobUrl)
}
}
else -> listener.logger.println(Messages.zdevops_declarative_DSN_downloading_invalid_dsorg())
Expand Down
5 changes: 4 additions & 1 deletion src/main/kotlin/org/zowe/zdevops/logic/SubmitJobOperation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import org.zowe.zdevops.Messages
import org.zowe.zdevops.utils.extractSubmitJobMessage
import org.zowe.zdevops.utils.runMFTryCatchWrappedQuery
import java.io.File
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

/**
* Submits a z/OS job
Expand Down Expand Up @@ -93,10 +95,11 @@ fun submitJobSync(
val logPath = "$workspacePath/${finalResult.jobName}.${finalResult.jobId}"
val file = File(logPath)
file.writeText(fullLog)
val urlEncodedJobName = URLEncoder.encode(finalResult.jobName, StandardCharsets.UTF_8.toString())
listener.logger.println(Messages.zdevops_declarative_ZOSJobs_got_log(
HyperlinkNote.encodeTo(
linkBuilder(buildUrl, finalResult.jobName, finalResult.jobId),
"${finalResult.jobName}.${finalResult.jobId}"
"$urlEncodedJobName.${finalResult.jobId}"
)
))
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
package org.zowe.zdevops.classic.steps

import hudson.AbortException
import hudson.EnvVars
import hudson.FilePath
import hudson.model.Executor
import hudson.model.Item
import hudson.model.TaskListener
import hudson.util.FormValidation
import io.kotest.assertions.assertSoftly
import io.kotest.assertions.fail
Expand Down Expand Up @@ -68,6 +70,12 @@ class DownloadDatasetStepSpec : ShouldSpec({
every { mockInstance.currentWorkspace } returns FilePath(virtualChannel, mockDir.absolutePath)
return mockInstance
}

override fun getEnvironment(log: TaskListener): EnvVars {
val env = EnvVars()
env["JOB_URL"] = "TEST"
return env
}
}

afterEach {
Expand Down

0 comments on commit ae8477b

Please sign in to comment.