Skip to content

Commit

Permalink
dist update
Browse files Browse the repository at this point in the history
  • Loading branch information
bitmaskit committed Jan 24, 2025
1 parent 56e70cd commit 70cd9f0
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions src/download.ts
Original file line number Diff line number Diff line change
@@ -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<string> {
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<string> {
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<string> {
if (version === 'master') {
info('using _master binaries is deprecated. Using latest release version instead.')
}
return isEnterpriseStep ? 'sap-piper' : 'piper'
}

0 comments on commit 70cd9f0

Please sign in to comment.