Adding command line support to run this action from other CI
This commit is contained in:
+8
-7
@@ -71,9 +71,10 @@ export class MavenDependencyGraph {
|
||||
return this.cache.countPackages();
|
||||
}
|
||||
|
||||
createManifest(): Manifest {
|
||||
const manifest = new Manifest(this.getProjectName());
|
||||
|
||||
createManifest(filePath?: string): Manifest {
|
||||
// The project name is not shown in the UI when you utilize a file path currently, but the file path is required to link up to the repository file
|
||||
// which is more beneficial at this point.
|
||||
const manifest = new Manifest(this.getProjectName(), filePath);
|
||||
const packageUrlToArtifact = this.packageUrlToArtifact;
|
||||
|
||||
this.directDependencies.forEach(depPackage => {
|
||||
@@ -107,7 +108,7 @@ export class MavenDependencyGraph {
|
||||
let rootPackageNumericId = 0;
|
||||
|
||||
const dependencyIdMap = dependencyMap(graph.dependencies);
|
||||
const idToPackageCachePackage : Map<string, Package> = new Map<string, Package>();
|
||||
const idToPackageCachePackage: Map<string, Package> = new Map<string, Package>();
|
||||
|
||||
// Create the packages for all known artifacts
|
||||
graph.artifacts.forEach((artifact: DepgraphArtifact) => {
|
||||
@@ -144,7 +145,7 @@ export class MavenDependencyGraph {
|
||||
});
|
||||
}
|
||||
});
|
||||
this.directDependencies = getDirectDependencies(rootPackageNumericId, graph.dependencies).map(id => {return idToPackageCachePackage[id]});
|
||||
this.directDependencies = getDirectDependencies(rootPackageNumericId, graph.dependencies).map(id => { return idToPackageCachePackage[id] });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -154,10 +155,10 @@ export function parseDependencyJson(file: string): Depgraph {
|
||||
try {
|
||||
const depGraph: Depgraph = JSON.parse(data.toString('utf-8'));
|
||||
return depGraph;
|
||||
} catch(err: any) {
|
||||
} catch (err: any) {
|
||||
throw new Error(`Failed to parse JSON payload: ${err.message}`);
|
||||
}
|
||||
} catch(err: any) {
|
||||
} catch (err: any) {
|
||||
throw new Error(`Failed to load file ${file}: ${err}`);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
import { Snapshot, submitSnapshot } from '@github/dependency-submission-toolkit';
|
||||
import { generateSnapshot } from '../snapshot-generator';
|
||||
import pkg from '../../package.json';
|
||||
|
||||
const { program } = require('commander');
|
||||
program.name('maven-dependency-submission');
|
||||
program.version(pkg.version);
|
||||
|
||||
program.requiredOption('-t, --token <token>', 'GitHub access token');
|
||||
program.requiredOption('-r --repository <repository>', 'GitHub repository, owner/repo_name format');
|
||||
program.requiredOption('-b --branch-ref <ref>', 'GitHub repository branch reference');
|
||||
program.requiredOption('-s --sha <commitSha>', 'GitHub repository commit SHA');
|
||||
|
||||
program.option('-d --directory <maven-project-directory>', 'the directory containing the Maven POM file', '.');
|
||||
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');
|
||||
|
||||
program.parse(process.argv);
|
||||
|
||||
async function execute() {
|
||||
const opts = program.opts();
|
||||
|
||||
// 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;
|
||||
|
||||
let snapshot: Snapshot | undefined;
|
||||
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()}`
|
||||
};
|
||||
|
||||
snapshot = await generateSnapshot(opts.directory, context, job);
|
||||
|
||||
} 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();
|
||||
@@ -3,19 +3,18 @@ import * as core from '@actions/core';
|
||||
|
||||
import * as path from 'path';
|
||||
|
||||
import {Snapshot} from '@github/dependency-submission-toolkit';
|
||||
import { Snapshot } from '@github/dependency-submission-toolkit';
|
||||
import { MavenDependencyGraph, parseDependencyJson } from './depgraph';
|
||||
|
||||
const version = require('../package.json')['version'];
|
||||
|
||||
|
||||
export async function generateSnapshot(directory: string) {
|
||||
export async function generateSnapshot(directory: string, context?: any, job?: any) {
|
||||
const depgraph = await generateDependencyGraph(directory);
|
||||
|
||||
try {
|
||||
const mavenDependencies = new MavenDependencyGraph(depgraph);
|
||||
const manifest = mavenDependencies.createManifest();
|
||||
const snapshot = new Snapshot(getDetector());
|
||||
const manifest = mavenDependencies.createManifest(path.join(directory, 'pom.xml'));
|
||||
const snapshot = new Snapshot(getDetector(), context, job);
|
||||
snapshot.addManifest(manifest);
|
||||
|
||||
return snapshot;
|
||||
@@ -61,7 +60,7 @@ export async function generateDependencyGraph(directory: string) {
|
||||
core.info(executionOutput);
|
||||
core.info(errors);
|
||||
core.endGroup();
|
||||
} catch(err: any) {
|
||||
} catch (err: any) {
|
||||
core.error(err);
|
||||
throw new Error(`A problem was encountered generating dependency files, please check execution logs for details; ${err.message}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user