Skip to content

Commit

Permalink
+ --artifactoryUrlFeature param option to specify mirror url domain (p…
Browse files Browse the repository at this point in the history
…roject-chip#1120)

* + --artifactoryUrl option to download-artifactory, enabling downloads from local artifactory repos.

BUG: ZAPP-1256

* code clean up

* compile download-artifact
  • Loading branch information
Jing T authored Sep 1, 2023
1 parent a75c922 commit 1aa832c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
44 changes: 30 additions & 14 deletions src-script/download-artifact.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,14 @@ var DEFAULT_COMMIT_LATEST = 'commit_latest'
var DEFAULT_BRANCH = 'master'
var DEFAULT_OWNER = 'SiliconLabs'
var DEFAULT_REPO = 'zap'
var ARTIFACTORY_SERVER = 'https://artifactory.silabs.net'
var ARTIFACTORY_URL_DOMAIN_DEFAULT = 'artifactory.silabs.net'
var ARTIFACTORY_REPO_NAME = 'zap-release-package'
var nexusCachedBranches = ['master', 'rel']
var cachedBranches = ['master', 'rel']
// cheap and secure
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'
function artifactoryServerUrl(opts) {
return 'https://'.concat(opts.artifactoryUrl)
}
function artifactoryGetLatestFolder(opt) {
return __awaiter(this, void 0, void 0, function () {
var _a, folders, paths, folder
Expand Down Expand Up @@ -271,7 +274,7 @@ function artifactoryStorageGet(dlOptions, uri) {
return __generator(this, function (_a) {
url = ''
.concat(
ARTIFACTORY_SERVER,
artifactoryServerUrl(dlOptions),
'/artifactory/api/storage/gsdk-generic-production/'
)
.concat(ARTIFACTORY_REPO_NAME, '/')
Expand Down Expand Up @@ -650,10 +653,14 @@ function artifactoryDownloadArtifacts(
if (!(json && artifacts && artifacts.length > 0))
return [3 /*break*/, 6]
baseUri = latest.paths.join('').replace('/api/storage', '')
baseUri = baseUri.replace(
ARTIFACTORY_URL_DOMAIN_DEFAULT,
dlOptions.artifactoryUrl
)
console.log('Repo: '.concat(baseUri))
return [4 /*yield*/, httpGet(baseUri + json)]
case 2:
jsonContent = _c.sent()
console.log('Repo: '.concat(baseUri))
console.log(
'Commit: '.concat(jsonContent.workflow_run.head_sha.substring(0, 7))
)
Expand Down Expand Up @@ -885,8 +892,14 @@ function configureBuildCommand() {
.option('src', {
description: 'URL source for obtaining ZAP binaries',
type: 'string',
default: 'nexus',
choices: ['github', 'nexus'],
default: 'artifactory' /* ARTIFACTORY */,
choices: ['github' /* GITHUB */, 'artifactory' /* ARTIFACTORY */],
})
.option('artifactoryUrl', {
description:
'Specify Artifactory URL domain used for downloading binaries from the artifact repo',
type: 'string',
default: ARTIFACTORY_URL_DOMAIN_DEFAULT,
})
.help('h')
.alias('h', 'help')
Expand All @@ -912,6 +925,7 @@ function main() {
src: y.argv.src,
mirror: y.argv.mirror,
nameOnly: y.argv.nameOnly,
artifactoryUrl: y.argv.artifactoryUrl,
}
return [
4 /*yield*/,
Expand All @@ -920,34 +934,35 @@ function main() {
]
case 1:
githubBranches = _a.sent()
if (!(dlOptions.src === 'nexus')) return [3 /*break*/, 3]
if (!((dlOptions.src === 'artifactory') /* ARTIFACTORY */))
return [3 /*break*/, 3]
return [
4 /*yield*/,
(0, is_reachable_1['default'])(ARTIFACTORY_SERVER, {
(0, is_reachable_1['default'])(artifactoryServerUrl(dlOptions), {
timeout: 10000,
}),
]
case 2:
if (!_a.sent()) {
console.log(
'Unable to reach Artifactory server ('.concat(
ARTIFACTORY_SERVER,
artifactoryServerUrl(dlOptions),
'). Defaulting to Github instead.'
)
)
dlOptions.src = 'github'
dlOptions.src = 'github' /* GITHUB */
} else if (
githubBranches.includes(dlOptions.branch) &&
!nexusCachedBranches.includes(dlOptions.branch)
!cachedBranches.includes(dlOptions.branch)
) {
console.log(
'Branch '.concat(
dlOptions.branch,
' is not cached on Artifactory. Defaulting to Github instead.'
)
)
dlOptions.src = 'github'
} else if (!nexusCachedBranches.includes(dlOptions.branch)) {
dlOptions.src = 'github' /* GITHUB */
} else if (!cachedBranches.includes(dlOptions.branch)) {
console.log(
'Branch '.concat(
dlOptions.branch,
Expand All @@ -958,7 +973,8 @@ function main() {
}
_a.label = 3
case 3:
if (!(dlOptions.src === 'nexus')) return [3 /*break*/, 6]
if (!((dlOptions.src === 'artifactory') /* ARTIFACTORY */))
return [3 /*break*/, 6]
return [4 /*yield*/, artifactoryGetLatestFolder(dlOptions)]
case 4:
latest = _a.sent()
Expand Down
59 changes: 43 additions & 16 deletions src-script/download-artifact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,22 @@ const DEFAULT_COMMIT_LATEST = 'commit_latest'
const DEFAULT_BRANCH = 'master'
const DEFAULT_OWNER = 'SiliconLabs'
const DEFAULT_REPO = 'zap'
const ARTIFACTORY_SERVER = 'https://artifactory.silabs.net'
const ARTIFACTORY_URL_DOMAIN_DEFAULT = 'artifactory.silabs.net'
const ARTIFACTORY_REPO_NAME = 'zap-release-package'
const nexusCachedBranches = ['master', 'rel']
const cachedBranches = ['master', 'rel']

const enum DownloadSources {
GITHUB = 'github',
ARTIFACTORY = 'artifactory',
}

// cheap and secure
process.env['NODE_TLS_REJECT_UNAUTHORIZED'] = '0'

function artifactoryServerUrl(opts: DlOptions) {
return `https://${opts.artifactoryUrl}`
}

async function artifactoryGetLatestFolder(
opt: DlOptions
): Promise<LatestFolder> {
Expand Down Expand Up @@ -90,7 +99,11 @@ async function artifactoryStorageGet(
dlOptions: DlOptions,
uri: string = ''
): Promise<any> {
const url = `${ARTIFACTORY_SERVER}/artifactory/api/storage/gsdk-generic-production/${ARTIFACTORY_REPO_NAME}/${dlOptions.owner}/${dlOptions.repo}/${dlOptions.branch}/${uri}`
const url = `${artifactoryServerUrl(
dlOptions
)}/artifactory/api/storage/gsdk-generic-production/${ARTIFACTORY_REPO_NAME}/${
dlOptions.owner
}/${dlOptions.repo}/${dlOptions.branch}/${uri}`
return httpGet(url)
}

Expand Down Expand Up @@ -334,9 +347,13 @@ async function artifactoryDownloadArtifacts(
let artifacts = files.filter((x) => !x.toLowerCase().endsWith('.json'))

if (json && artifacts && artifacts.length > 0) {
const baseUri = latest.paths.join('').replace('/api/storage', '')
const jsonContent = await httpGet(baseUri + json)
let baseUri = latest.paths.join('').replace('/api/storage', '')
baseUri = baseUri.replace(
ARTIFACTORY_URL_DOMAIN_DEFAULT,
dlOptions.artifactoryUrl
)
console.log(`Repo: ${baseUri}`)
const jsonContent = await httpGet(baseUri + json)
console.log(
`Commit: ${jsonContent.workflow_run.head_sha.substring(0, 7)}`
)
Expand All @@ -355,7 +372,6 @@ async function artifactoryDownloadArtifacts(
) {
continue
}

await download(downloadUrl, outputDir, undefined, artifact)
}
}
Expand Down Expand Up @@ -499,8 +515,13 @@ function configureBuildCommand() {
.option('src', {
description: `URL source for obtaining ZAP binaries`,
type: 'string',
default: 'nexus',
choices: ['github', 'nexus'],
default: DownloadSources.ARTIFACTORY,
choices: [DownloadSources.GITHUB, DownloadSources.ARTIFACTORY],
})
.option('artifactoryUrl', {
description: `Specify Artifactory URL domain used for downloading binaries from the artifact repo`,
type: 'string',
default: ARTIFACTORY_URL_DOMAIN_DEFAULT,
})
.help('h')
.alias('h', 'help')
Expand All @@ -523,6 +544,7 @@ interface DlOptions {
src: string
mirror: boolean
nameOnly: boolean
artifactoryUrl: string
}

async function main() {
Expand All @@ -541,26 +563,31 @@ async function main() {
src: y.argv.src,
mirror: y.argv.mirror,
nameOnly: y.argv.nameOnly,
artifactoryUrl: y.argv.artifactoryUrl,
}

let githubBranches = await getExistingGithubBranches(dlOptions)

// evaluate artifact source
if (dlOptions.src === 'nexus') {
if (!(await isReachable(ARTIFACTORY_SERVER, { timeout: 10000 }))) {
if (dlOptions.src === DownloadSources.ARTIFACTORY) {
if (
!(await isReachable(artifactoryServerUrl(dlOptions), { timeout: 10000 }))
) {
console.log(
`Unable to reach Artifactory server (${ARTIFACTORY_SERVER}). Defaulting to Github instead.`
`Unable to reach Artifactory server (${artifactoryServerUrl(
dlOptions
)}). Defaulting to Github instead.`
)
dlOptions.src = 'github'
dlOptions.src = DownloadSources.GITHUB
} else if (
githubBranches.includes(dlOptions.branch) &&
!nexusCachedBranches.includes(dlOptions.branch)
!cachedBranches.includes(dlOptions.branch)
) {
console.log(
`Branch ${dlOptions.branch} is not cached on Artifactory. Defaulting to Github instead.`
)
dlOptions.src = 'github'
} else if (!nexusCachedBranches.includes(dlOptions.branch)) {
dlOptions.src = DownloadSources.GITHUB
} else if (!cachedBranches.includes(dlOptions.branch)) {
console.log(
`Branch ${dlOptions.branch} is not cached on Artifactory. Defaulting to master branch instead.`
)
Expand All @@ -569,7 +596,7 @@ async function main() {
}

// Download site sources: Artifactory, Github
if (dlOptions.src === 'nexus') {
if (dlOptions.src === DownloadSources.ARTIFACTORY) {
let latest = await artifactoryGetLatestFolder(dlOptions)
await artifactoryDownloadArtifacts(
latest,
Expand Down

0 comments on commit 1aa832c

Please sign in to comment.