2022-09-26 14:53:51 +00:00
import pkg from '../../package.json' ;
2024-04-29 09:28:27 +00:00
import { program } from 'commander' ;
2022-09-26 14:53:51 +00:00
2022-10-26 18:04:32 +00:00
program . name ( pkg . name ) ;
2022-09-26 14:53:51 +00:00
program . version ( pkg . version ) ;
program . requiredOption ( '-t, --token <token>' , 'GitHub access token' ) ;
program . requiredOption ( '-r --repository <repository>' , 'GitHub repository, owner/repo_name format' ) ;
2022-10-26 18:04:32 +00:00
program . requiredOption ( '-b --branch-ref <ref>' , 'GitHub repository branch reference, e.g. refs/heads/main' ) ;
2022-09-26 14:53:51 +00:00
program . requiredOption ( '-s --sha <commitSha>' , 'GitHub repository commit SHA' ) ;
program . option ( '-d --directory <maven-project-directory>' , 'the directory containing the Maven POM file' , '.' ) ;
2022-10-26 18:04:32 +00:00
program . option ( '--settings-file <settings-file>' , 'path to the Maven settings file' ) ;
program . option ( '--ignore-maven-wrapper' , 'ingore Maven wrappers, if present, and use Maven from the PATH' ) ;
program . option ( '--maven-args <maven-args>' , 'additional arguments to pass to Maven' ) ;
2022-09-26 14:53:51 +00:00
program . option ( '--github-api-url <url>' , 'GitHub API URL' , 'https://api.github.com' ) ;
program . option ( '-j --job-name <jobName>' , 'Optional name for the activity creating and submitting the graph' , 'maven-dependency-submission-cli' ) ;
program . option ( '-i --run-id <jobName>' , 'Optional Run ID number for the activity that is providing the graph' ) ;
2023-12-18 07:41:27 +00:00
program . option ( '--snapshot-exclude-file-name' , 'exclude the file name in the dependency snapshot report. If false the name of the artifactor from the POM will be used, but any links in GitHub will not work.' ) ;
2024-07-03 09:52:52 +00:00
program . option ( '--detector-name <detectorName>' , 'optional name of the detector that generated the snapshot' ) ;
program . option ( '--detector-url <detectorUrl>' , 'optional URL of the detector that generated the snapshot, but not optional if you specify an detector-name' ) ;
program . option ( '--detector-version <detectorVersion>' , 'optional version of the detector that generated the snapshot, but not optional if you specify an detector-name' ) ;
2022-09-26 14:53:51 +00:00
program . parse ( process . argv ) ;
2022-10-26 18:04:32 +00:00
const opts = program . opts ( ) ;
2022-09-26 14:53:51 +00:00
2022-10-26 18:04:32 +00:00
// Inject some required environment variables like the Actions INPUTs and special environment variables
process . env [ 'INPUT_TOKEN' ] = opts . token ;
process . env [ 'GITHUB_REPOSITORY' ] = opts . repository ;
process . env [ 'GITHUB_API_URL' ] = opts . githubApiUrl ;
2022-09-26 14:53:51 +00:00
2022-10-26 18:04:32 +00:00
// The above injection of environment variables is required before the submission APIs are imported
import { Snapshot , submitSnapshot } from '@github/dependency-submission-toolkit' ;
2023-11-22 17:23:23 +00:00
import { SnapshotConfig , generateSnapshot } from '../snapshot-generator' ;
2022-10-26 18:04:32 +00:00
async function execute() {
2022-09-26 14:53:51 +00:00
let snapshot : Snapshot | undefined ;
2022-10-26 18:04:32 +00:00
// The dependency submission API requires a formatted ref reference so check early fo now
2023-04-19 10:52:52 +00:00
if ( /^refs\// . exec ( opts . branchRef ) === null ) {
2022-10-26 18:04:32 +00:00
console . error ( ` Branch reference must be in path form, e.g. 'refs/heads/main' for the 'main' branch. ` ) ;
2023-04-19 10:52:52 +00:00
console . error ( ` provided value was: ' ${ opts . branchRef } ' ` ) ;
2022-10-26 18:04:32 +00:00
program . help ( { error : true } ) ;
process . exit ( 1 ) ;
}
2024-07-03 09:52:52 +00:00
// If the detector-name is provided, then the other detector options become mandatory, check these early
let detector ;
if ( opts . detectorName ) {
if ( ! opts . detectorUrl ) {
console . error ( ` Error: detector-url is required when detector-name is provided \ n ` ) ;
program . help ( { error : true } ) ;
}
if ( ! opts . detectorVersion ) {
console . error ( ` Error: detector-version is required when detector-name is provided \ n ` ) ;
program . help ( { error : true } ) ;
}
detector = {
name : opts.detectorName ,
url : opts.detectorUrl ,
version : opts.detectorVersion ,
}
}
2022-09-26 14:53:51 +00:00
try {
// Build a fake GitHub Actions context so that values for the submission APIs can be retrieved
const context = {
sha : opts.sha ,
ref : opts.branchRef ,
} ;
const job = {
correlator : opts.jobName ,
id : ` ${ opts . runId || Date . now ( ) } `
} ;
2022-10-26 18:04:32 +00:00
const mvnConfig = {
directory : opts.directory ,
settingsFile : opts.settingsFile ,
ignoreMavenWrapper : opts.ignoreMavenWrapper ,
mavenArgs : opts.mavenArgs
} ;
2022-10-26 15:11:32 +00:00
2023-11-22 17:23:23 +00:00
const snapshotConfig : SnapshotConfig = {
2022-10-28 16:38:02 +00:00
context ,
job ,
2023-11-30 10:50:02 +00:00
sha : opts.sha ,
ref : opts.branchRef ,
2023-12-18 07:41:27 +00:00
2024-07-03 09:52:52 +00:00
detector : detector
2022-10-28 16:38:02 +00:00
}
2024-07-03 09:52:52 +00:00
2022-10-28 16:38:02 +00:00
snapshot = await generateSnapshot ( opts . directory , mvnConfig , snapshotConfig ) ;
2022-09-26 14:53:51 +00:00
} catch ( err : any ) {
console . error ( ` Failed to generate a dependency snapshot, check logs for more details, ${ err } ` ) ;
console . log ( err . stack ) ;
console . error ( err . message ) ;
program . help ( { error : true } ) ;
process . exit ( 1 ) ;
}
if ( snapshot ) {
console . log ( ` Submitting Snapshot... ` ) ;
try {
await submitSnapshot ( snapshot ) ;
console . log ( ` completed. ` )
} catch ( err : any ) {
console . error ( ` Failed to submit the dependency snapshot, check logs for more details, ${ err } ` ) ;
console . log ( err . stack ) ;
console . error ( err . message ) ;
program . help ( { error : true } ) ;
process . exit ( 1 ) ;
}
}
}
execute ( ) ;