use reasonable defaults for sha and ref

enforce all detector vars present or not present
This commit is contained in:
E. Lynette Rayle
2025-05-28 18:21:20 +00:00
parent f3f97c6024
commit 3e507a8464
+41 -12
View File
@@ -7,6 +7,7 @@ import {
Manifest, Manifest,
submitSnapshot submitSnapshot
} from '@github/dependency-submission-toolkit' } from '@github/dependency-submission-toolkit'
import type {PullRequestEvent} from '@octokit/webhooks-types'
import { import {
processGoGraph, processGoGraph,
@@ -82,23 +83,32 @@ async function main () {
url: string; url: string;
version: string; version: string;
}; };
var snapshotDetector: SnapshotDetector; let snapshotDetector: SnapshotDetector;
// If detector name is passed in, then url and version are required inputs
// Otherwise, default values will be used to maintain backwards compatibility
const detectorName = core.getInput('detector-name'); const detectorName = core.getInput('detector-name');
if (detectorName !== '') { const detectorUrl = core.getInput('detector-url');
snapshotDetector = { const detectorVersion = core.getInput('detector-version');
name: detectorName,
url: core.getInput('detector-url', { required: true }), if (detectorName === '' && detectorUrl === '' && detectorVersion === '') {
version: core.getInput('detector-version', { required: true }) // use defaults if none are specified
}
} else {
snapshotDetector = { snapshotDetector = {
name: 'actions/go-dependency-submission', name: 'actions/go-dependency-submission',
url: 'https://github.com/actions/go-dependency-submission', url: 'https://github.com/actions/go-dependency-submission',
version: '0.0.1' version: '0.0.1'
} }
} else if (detectorName === '' || detectorUrl === '' || detectorVersion === '') {
// if any of detectorName, detectorUrl, or detectorVersion have value, then they are all required
throw new Error(
"Invalid input: if any of 'detector-name', 'detector-url', or 'detector-version' have value, then thay are all required."
);
} else {
// use inputs since all are specified
snapshotDetector = {
name: detectorName,
url: core.getInput('detector-url', { required: true }),
version: core.getInput('detector-version', { required: true })
}
} }
const snapshot = new Snapshot( const snapshot = new Snapshot(
@@ -110,10 +120,29 @@ async function main () {
} }
) )
snapshot.addManifest(manifest) snapshot.addManifest(manifest)
snapshot.sha = core.getInput('snapshot-sha') snapshot.sha = core.getInput('snapshot-sha', getShaFromContext())
snapshot.ref = core.getInput('snapshot-ref') snapshot.ref = core.getInput('snapshot-ref', github.context.ref)
submitSnapshot(snapshot) submitSnapshot(snapshot)
} }
function getShaFromContext(): string {
const context = github.context
const pullRequestEvents = [
'pull_request',
'pull_request_comment',
'pull_request_review',
'pull_request_review_comment'
// Note that pull_request_target is omitted here.
// That event runs in the context of the base commit of the PR,
// so the snapshot should not be associated with the head commit.
]
if (pullRequestEvents.includes(context.eventName)) {
const pr = (context.payload as PullRequestEvent).pull_request
return pr.head.sha
} else {
return context.sha
}
}
main() main()