From 70cd9f07e228b9012dcad6729433ad3faae5f8d7 Mon Sep 17 00:00:00 2001 From: Valentin Uchkunev Date: Fri, 24 Jan 2025 14:48:02 +0200 Subject: [PATCH] dist update --- src/download.ts | 66 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/download.ts diff --git a/src/download.ts b/src/download.ts new file mode 100644 index 00000000..b80e50f9 --- /dev/null +++ b/src/download.ts @@ -0,0 +1,66 @@ +import fs from 'fs' +import { debug, info } from '@actions/core' +import { downloadTool } from '@actions/tool-cache' +import { isEnterpriseStep } from './enterprise' +import { + getReleaseAssetUrl, + getTag, + GITHUB_COM_SERVER_URL +} from './github' +import { fetchRetry } from './fetch' + +export async function downloadPiperBinary ( + stepName: string, version: string, apiURL: string, token: string, owner: string, repo: string +): Promise { + const isEnterprise = isEnterpriseStep(stepName) + if (isEnterprise && token === '') throw new Error('Token is not provided for enterprise step') + if (owner === '') throw new Error('owner is not provided') + if (repo === '') throw new Error('repository is not provided') + + let binaryURL + const headers: any = {} + const piperBinaryName = await getPiperBinaryNameFromInputs(isEnterprise, version) + if (token !== '') { + debug('Fetching binary from GitHub API') + headers.Accept = 'application/octet-stream' + headers.Authorization = `token ${token}` + + const [binaryAssetURL, tag] = await getReleaseAssetUrl(piperBinaryName, version, apiURL, token, owner, repo) + binaryURL = binaryAssetURL + version = tag + } else { + debug('Fetching binary from URL') + binaryURL = await getPiperDownloadURL(piperBinaryName, version) + version = binaryURL.split('/').slice(-2)[0] + } + version = version.replace(/\./g, '_') + const piperPath = `${process.cwd()}/${version}/${piperBinaryName}` + if (fs.existsSync(piperPath)) { + return piperPath + } + + info(`Downloading '${binaryURL}' as '${piperPath}'`) + await downloadTool( + binaryURL, + piperPath, + undefined, + headers + ) + + return piperPath +} +async function getPiperDownloadURL (piper: string, version?: string): Promise { + const tagURL = `${GITHUB_COM_SERVER_URL}/SAP/jenkins-library/releases/${getTag(version, false)}` + const response = await fetchRetry(tagURL, 'HEAD') + .catch(async (err) => { + throw new Error(`Can't get the tag: ${err}`) + }) + return await Promise.resolve(response.url.replace(/tag/, 'download') + `/${piper}`) +} + +async function getPiperBinaryNameFromInputs (isEnterpriseStep: boolean, version?: string): Promise { + if (version === 'master') { + info('using _master binaries is deprecated. Using latest release version instead.') + } + return isEnterpriseStep ? 'sap-piper' : 'piper' +}