diff --git a/__tests__/subject.test.ts b/__tests__/subject.test.ts index 3868e53..2474e7b 100644 --- a/__tests__/subject.test.ts +++ b/__tests__/subject.test.ts @@ -5,6 +5,10 @@ import path from 'path' import { subjectFromInputs } from '../src/subject' describe('subjectFromInputs', () => { + beforeEach(() => { + process.env['INPUT_PUSH-TO-REGISTRY'] = 'false' + }) + afterEach(() => { process.env['INPUT_SUBJECT-PATH'] = '' process.env['INPUT_SUBJECT-DIGEST'] = '' @@ -45,12 +49,12 @@ describe('subjectFromInputs', () => { }) describe('when specifying a subject digest', () => { - const name = 'subject' + const name = 'Subject' describe('when the digest is malformed', () => { beforeEach(() => { process.env['INPUT_SUBJECT-DIGEST'] = 'digest' - process.env['INPUT_SUBJECT-NAME'] = 'subject' + process.env['INPUT_SUBJECT-NAME'] = name }) it('throws an error', async () => { @@ -63,7 +67,7 @@ describe('subjectFromInputs', () => { describe('when the alogrithm is not supported', () => { beforeEach(() => { process.env['INPUT_SUBJECT-DIGEST'] = 'md5:deadbeef' - process.env['INPUT_SUBJECT-NAME'] = 'subject' + process.env['INPUT_SUBJECT-NAME'] = name }) it('throws an error', async () => { @@ -76,7 +80,7 @@ describe('subjectFromInputs', () => { describe('when the sha256 digest is malformed', () => { beforeEach(() => { process.env['INPUT_SUBJECT-DIGEST'] = 'sha256:deadbeef' - process.env['INPUT_SUBJECT-NAME'] = 'subject' + process.env['INPUT_SUBJECT-NAME'] = name }) it('throws an error', async () => { @@ -105,6 +109,28 @@ describe('subjectFromInputs', () => { expect(subject[0].digest).toEqual({ [alg]: digest }) }) }) + + describe('when the push-to-registry is true', () => { + const imageName = 'ghcr.io/FOO/bar' + const alg = 'sha256' + const digest = + '7d070f6b64d9bcc530fe99cc21eaaa4b3c364e0b2d367d7735671fa202a03b32' + + beforeEach(() => { + process.env['INPUT_SUBJECT-DIGEST'] = `${alg}:${digest}` + process.env['INPUT_SUBJECT-NAME'] = imageName + process.env['INPUT_PUSH-TO-REGISTRY'] = 'true' + }) + + it('returns the subject (with name downcased)', async () => { + const subject = await subjectFromInputs() + + expect(subject).toBeDefined() + expect(subject).toHaveLength(1) + expect(subject[0].name).toEqual(imageName.toLowerCase()) + expect(subject[0].digest).toEqual({ [alg]: digest }) + }) + }) }) describe('when specifying a subject path', () => { diff --git a/dist/index.js b/dist/index.js index ea5fd1a..8f61d17 100644 --- a/dist/index.js +++ b/dist/index.js @@ -80186,6 +80186,9 @@ const subjectFromInputs = async () => { const subjectPath = core.getInput('subject-path', { required: false }); const subjectDigest = core.getInput('subject-digest', { required: false }); const subjectName = core.getInput('subject-name', { required: false }); + const pushToRegistry = core.getBooleanInput('push-to-registry', { + required: false + }); if (!subjectPath && !subjectDigest) { throw new Error('One of subject-path or subject-digest must be provided'); } @@ -80195,11 +80198,14 @@ const subjectFromInputs = async () => { if (subjectDigest && !subjectName) { throw new Error('subject-name must be provided when using subject-digest'); } + // If push-to-registry is enabled, ensure the subject name is lowercase + // to conform to OCI image naming conventions + const name = pushToRegistry ? subjectName.toLowerCase() : subjectName; if (subjectPath) { - return await getSubjectFromPath(subjectPath, subjectName); + return await getSubjectFromPath(subjectPath, name); } else { - return [getSubjectFromDigest(subjectDigest, subjectName)]; + return [getSubjectFromDigest(subjectDigest, name)]; } }; exports.subjectFromInputs = subjectFromInputs; diff --git a/src/subject.ts b/src/subject.ts index a3aaf2c..fb11155 100644 --- a/src/subject.ts +++ b/src/subject.ts @@ -17,6 +17,9 @@ export const subjectFromInputs = async (): Promise => { const subjectPath = core.getInput('subject-path', { required: false }) const subjectDigest = core.getInput('subject-digest', { required: false }) const subjectName = core.getInput('subject-name', { required: false }) + const pushToRegistry = core.getBooleanInput('push-to-registry', { + required: false + }) if (!subjectPath && !subjectDigest) { throw new Error('One of subject-path or subject-digest must be provided') @@ -32,10 +35,14 @@ export const subjectFromInputs = async (): Promise => { throw new Error('subject-name must be provided when using subject-digest') } + // If push-to-registry is enabled, ensure the subject name is lowercase + // to conform to OCI image naming conventions + const name = pushToRegistry ? subjectName.toLowerCase() : subjectName + if (subjectPath) { - return await getSubjectFromPath(subjectPath, subjectName) + return await getSubjectFromPath(subjectPath, name) } else { - return [getSubjectFromDigest(subjectDigest, subjectName)] + return [getSubjectFromDigest(subjectDigest, name)] } }