forked from synopsys-sig/detect-action
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make detect-version optional by downloading latest (#8)
- Loading branch information
Showing
11 changed files
with
165 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,96 @@ | ||
import { ToolDownloader } from '../downloader/tool-downloader' | ||
import * as toolCache from '@actions/tool-cache' | ||
import path from 'path' | ||
import { HttpClient } from 'typed-rest-client/HttpClient' | ||
import { APPLICATION_NAME } from '../action/constants' | ||
import { IHeaders } from 'typed-rest-client/Interfaces' | ||
import { DetectToolsVersions } from './detect-tools-versions' | ||
import { DetectToolVersion } from './detect-tool-version' | ||
|
||
const DETECT_BINARY_REPO_URL = 'https://sig-repo.synopsys.com' | ||
export const TOOL_NAME = 'detect' | ||
|
||
export class DetectToolDownloader implements ToolDownloader { | ||
private readonly version: string | ||
private async getDetectVersions(): Promise<DetectToolsVersions> { | ||
const authenticationClient = new HttpClient(APPLICATION_NAME) | ||
const headers: IHeaders = { | ||
'X-Result-Detail': 'info' | ||
} | ||
|
||
constructor(version: string) { | ||
this.version = version | ||
const httpClientResponse = await authenticationClient.get( | ||
`${DETECT_BINARY_REPO_URL}/api/storage/bds-integrations-release/com/synopsys/integration/synopsys-detect?properties`, | ||
headers | ||
) | ||
const responseBody = await httpClientResponse.readBody() | ||
return JSON.parse(responseBody) as DetectToolsVersions | ||
} | ||
|
||
private getDetectDownloadUrl(): string { | ||
return `${DETECT_BINARY_REPO_URL}/bds-integrations-release/com/synopsys/integration/synopsys-detect/${this.version}/synopsys-detect-${this.version}.jar` | ||
private async findDetectVersion( | ||
version?: string | ||
): Promise<DetectToolVersion> { | ||
if (version?.match(/^[0-9]+.[0-9]+.[0-9]+$/)) { | ||
return { | ||
url: `${DETECT_BINARY_REPO_URL}/bds-integrations-release/com/synopsys/integration/synopsys-detect/${version}/synopsys-detect-${version}.jar`, | ||
version, | ||
jarName: `synopsys-detect-${version}.jar` | ||
} | ||
} | ||
|
||
let detectVersionKey = 'DETECT_LATEST_' | ||
|
||
if (version?.match(/^[0-9]+/)) { | ||
detectVersionKey = `DETECT_LATEST_${version}` | ||
} else if (version) { | ||
throw new Error(`Invalid input version '${version}'`) | ||
} | ||
|
||
const detectVersions = await this.getDetectVersions() | ||
const keys = Object.keys(detectVersions.properties) | ||
const key = keys.filter(x => x.match(detectVersionKey)).at(-1) | ||
if (!key) { | ||
throw new Error( | ||
`Cannot find matching key ${detectVersionKey} on detect versions!` | ||
) | ||
} | ||
const url = detectVersions.properties[key].at(-1) | ||
if (!url) { | ||
throw new Error(`Cannot find url for property ${key} on detect versions!`) | ||
} | ||
|
||
const jarName = url.substring(url.lastIndexOf('/') + 1) | ||
const resultVersion = jarName.substring( | ||
jarName.lastIndexOf('-') + 1, | ||
jarName.length - 4 | ||
) | ||
|
||
return { url, version: resultVersion, jarName } | ||
} | ||
|
||
async download(): Promise<string> { | ||
const jarName = `synopsys-detect-${this.version}.jar` | ||
async download(version?: string): Promise<string> { | ||
const detectVersion = await this.findDetectVersion(version) | ||
|
||
const cachedDetect = toolCache.find(TOOL_NAME, this.version) | ||
const cachedDetect = toolCache.find(TOOL_NAME, detectVersion.version) | ||
if (cachedDetect) { | ||
return path.resolve(cachedDetect, jarName) | ||
return path.resolve(cachedDetect, detectVersion.jarName) | ||
} | ||
|
||
const detectDownloadUrl = this.getDetectDownloadUrl() | ||
|
||
const detectDownloadPath = await toolCache.downloadTool(detectDownloadUrl) | ||
const detectDownloadPath = await toolCache.downloadTool(detectVersion.url) | ||
const cachedFolder = await toolCache.cacheFile( | ||
detectDownloadPath, | ||
jarName, | ||
detectVersion.jarName, | ||
TOOL_NAME, | ||
this.version | ||
detectVersion.version | ||
) | ||
|
||
return path.resolve(cachedFolder, jarName) | ||
return path.resolve(cachedFolder, detectVersion.jarName) | ||
} | ||
|
||
private static instance: DetectToolDownloader | null | ||
|
||
static getInstance(): DetectToolDownloader { | ||
if (!DetectToolDownloader.instance) { | ||
DetectToolDownloader.instance = new DetectToolDownloader() | ||
} | ||
return DetectToolDownloader.instance | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface DetectToolVersion { | ||
url: string | ||
version: string | ||
jarName: string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export interface DetectToolsVersions { | ||
properties: Record<string, string[]> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters