Skip to content

Commit

Permalink
downcase subject name for OCI images (#63)
Browse files Browse the repository at this point in the history
Signed-off-by: Brian DeHamer <bdehamer@github.com>
  • Loading branch information
bdehamer committed May 14, 2024
1 parent 60d0be1 commit 38ff958
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
34 changes: 30 additions & 4 deletions __tests__/subject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = ''
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand Down Expand Up @@ -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', () => {
Expand Down
10 changes: 8 additions & 2 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export const subjectFromInputs = async (): Promise<Subject[]> => {
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')
Expand All @@ -32,10 +35,14 @@ export const subjectFromInputs = async (): Promise<Subject[]> => {
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)]
}
}

Expand Down

0 comments on commit 38ff958

Please sign in to comment.