-
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.
- Loading branch information
1 parent
6d649dd
commit 2e98329
Showing
5 changed files
with
134 additions
and
41 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Empty file.
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,22 +1,81 @@ | ||
import path from 'path'; | ||
import * as input from '../app.input'; | ||
import { DefaultArtifactClient } from '@actions/artifact'; | ||
import * as exec from '@actions/exec'; | ||
import * as inputs from '../app.input'; | ||
import { ScannerResults } from './result.interfaces'; | ||
import fs from 'fs'; | ||
import * as core from '@actions/core'; | ||
import * as path from 'path'; | ||
|
||
const artifact = new DefaultArtifactClient(); | ||
|
||
export async function uploadResults(): Promise<void> { | ||
await artifact.uploadArtifact( | ||
path.basename(input.OUTPUT_FILEPATH), | ||
[input.OUTPUT_FILEPATH], | ||
path.dirname(input.OUTPUT_FILEPATH) | ||
path.basename(inputs.OUTPUT_FILEPATH), | ||
[inputs.OUTPUT_FILEPATH], | ||
path.dirname(inputs.OUTPUT_FILEPATH) | ||
); | ||
} | ||
|
||
export function commandBuilder(): string { | ||
return `docker run -v "${input.REPO_DIR}":"/scanoss" ghcr.io/scanoss/scanoss-py:v1.9.0 scan . | ||
--output ${input.OUTPUT_FILEPATH} | ||
${input.DEPENDENCIES_ENABLED ? `--dependencies` : ''} | ||
${input.SBOM_ENABLED ? `--${input.SBOM_TYPE} ${input.SBOM_FILEPATH}` : ''} | ||
${input.API_URL ? `--apiurl ${input.API_URL}` : ''} | ||
${input.API_KEY ? `--key ${input.API_KEY}` : ''}`.replace(/\n/gm, ''); | ||
export interface Options { | ||
sbomType?: string; | ||
sbomEnabled?: boolean; | ||
sbomFilepath?: string; | ||
|
||
dependenciesEnabled?: boolean; | ||
|
||
apiKey?: string; | ||
apiUrl?: string; | ||
|
||
outputFilepath: string; | ||
inputFilepath: string; | ||
} | ||
|
||
export class ScanService { | ||
private options: Options; | ||
constructor(options?: Options) { | ||
this.options = options || { | ||
sbomFilepath: inputs.SBOM_FILEPATH, | ||
sbomType: inputs.SBOM_TYPE, | ||
sbomEnabled: inputs.SBOM_ENABLED, | ||
apiKey: inputs.API_KEY, | ||
apiUrl: inputs.API_URL, | ||
dependenciesEnabled: inputs.DEPENDENCIES_ENABLED, | ||
outputFilepath: inputs.OUTPUT_FILEPATH, | ||
inputFilepath: inputs.REPO_DIR | ||
}; | ||
} | ||
async scan(): Promise<{ scan: ScannerResults; stdout: string; stderr: string }> { | ||
const command = await this.buildCommand(); | ||
const { stdout, stderr } = await exec.getExecOutput(command, []); | ||
const scan = await this.parseResult(); | ||
return { scan, stdout, stderr }; | ||
} | ||
|
||
private async buildCommand(): Promise<string> { | ||
return `docker run -v "${this.options.inputFilepath}":"/scanoss" ghcr.io/scanoss/scanoss-py:v1.9.0 scan . | ||
--output ${this.options.outputFilepath} | ||
${this.options.dependenciesEnabled ? `--dependencies` : ''} | ||
${await this.detectSbom()} | ||
${this.options.apiUrl ? `--apiurl ${this.options.apiUrl}` : ''} | ||
${this.options.apiKey ? `--key ${this.options.apiKey}` : ''}`.replace(/\n/gm, ' '); | ||
} | ||
|
||
private async detectSbom(): Promise<string> { | ||
if (!this.options.sbomEnabled || !this.options.sbomFilepath) return ''; | ||
|
||
try { | ||
await fs.promises.access(this.options.sbomFilepath, fs.constants.F_OK); | ||
return `--${this.options.sbomType} ${this.options.sbomFilepath}`; | ||
} catch (error) { | ||
core.warning('SBOM not found'); | ||
return ''; | ||
} | ||
} | ||
|
||
private async parseResult(): Promise<ScannerResults> { | ||
const content = await fs.promises.readFile(this.options.outputFilepath, 'utf-8'); | ||
return JSON.parse(content) as ScannerResults; | ||
} | ||
} | ||
|
||
export const scanService = new ScanService(); |