diff --git a/__tests__/main.test.ts b/__tests__/main.test.ts index 3b838f8..29c8b60 100644 --- a/__tests__/main.test.ts +++ b/__tests__/main.test.ts @@ -166,77 +166,86 @@ describe('action', () => { expect(removeDirMock).toHaveBeenCalledWith('/tmp/test') }) - it('uploads and returns the manifest & package URL if all succeeds', async () => { - // Mock the environment - process.env.GITHUB_REPOSITORY = 'test/test' - github.context.eventName = 'release' - github.context.payload = { - release: { - id: '123', - tag_name: 'v1.0.0' - } - } - getInputMock.mockImplementation((name: string) => { - if (name === 'path') { - return 'test' - } else if (name === 'registry') { - return 'https://ghcr.io' - } - return '' - }) + it('successfully uploads if the release tag is a semver without v prefix', async () => { + await testHappyPath('1.0.0') + }) - isDirectoryMock.mockImplementation(() => true) - - createTempDirMock.mockImplementation(() => '/tmp/test') - - createArchivesMock.mockImplementation(() => { - return { - zipFile: { - path: 'test', - size: 5, - sha256: '123' - }, - tarFile: { - path: 'test2', - size: 52, - sha256: '1234' - } - } - }) - - publishOCIArtifactMock.mockImplementation(() => { - return new URL('https://ghcr.io/v2/test/test:1.0.0') - }) - - // Run the action - await main.run() - - expect(publishOCIArtifactMock).toHaveBeenCalledTimes(1) - - // Check manifest is in output - expect(setOutputMock).toHaveBeenCalledWith( - 'package-url', - 'https://ghcr.io/v2/test/test:1.0.0' - ) - expect(setOutputMock).toHaveBeenCalledWith( - 'package-manifest', - expect.any(String) - ) - - // Validate the manifest - const manifest = JSON.parse(setOutputMock.mock.calls[1][1]) - expect(manifest.mediaType).toEqual( - 'application/vnd.oci.image.manifest.v1+json' - ) - expect(manifest.config.mediaType).toEqual( - 'application/vnd.github.actions.package.config.v1+json' - ) - expect(manifest.layers.length).toEqual(3) - expect(manifest.annotations['com.github.package.type']).toEqual( - 'actions_oci_pkg' - ) - - // Expect the files to be cleaned up - expect(removeDirMock).toHaveBeenCalledWith('/tmp/test') + it('successfully uploads if the release tag is a semver with v prefix', async () => { + await testHappyPath('v1.0.0') }) }) + +// Test that main successfully uploads and returns the manifest & package URL +async function testHappyPath(version: string): Promise { + // Mock the environment + process.env.GITHUB_REPOSITORY = 'test/test' + github.context.eventName = 'release' + github.context.payload = { + release: { + id: '123', + tag_name: version + } + } + getInputMock.mockImplementation((name: string) => { + if (name === 'path') { + return 'test' + } else if (name === 'registry') { + return 'https://ghcr.io' + } + return '' + }) + + isDirectoryMock.mockImplementation(() => true) + + createTempDirMock.mockImplementation(() => '/tmp/test') + + createArchivesMock.mockImplementation(() => { + return { + zipFile: { + path: 'test', + size: 5, + sha256: '123' + }, + tarFile: { + path: 'test2', + size: 52, + sha256: '1234' + } + } + }) + + publishOCIArtifactMock.mockImplementation(() => { + return new URL('https://ghcr.io/v2/test/test:1.0.0') + }) + + // Run the action + await main.run() + + expect(publishOCIArtifactMock).toHaveBeenCalledTimes(1) + + // Check manifest is in output + expect(setOutputMock).toHaveBeenCalledWith( + 'package-url', + 'https://ghcr.io/v2/test/test:1.0.0' + ) + expect(setOutputMock).toHaveBeenCalledWith( + 'package-manifest', + expect.any(String) + ) + + // Validate the manifest + const manifest = JSON.parse(setOutputMock.mock.calls[1][1]) + expect(manifest.mediaType).toEqual( + 'application/vnd.oci.image.manifest.v1+json' + ) + expect(manifest.config.mediaType).toEqual( + 'application/vnd.github.actions.package.config.v1+json' + ) + expect(manifest.layers.length).toEqual(3) + expect(manifest.annotations['com.github.package.type']).toEqual( + 'actions_oci_pkg' + ) + + // Expect the files to be cleaned up + expect(removeDirMock).toHaveBeenCalledWith('/tmp/test') +} diff --git a/dist/index.js b/dist/index.js index 9dfb345..048e747 100644 --- a/dist/index.js +++ b/dist/index.js @@ -72902,6 +72902,7 @@ async function run() { // https://docs.github.com/en/actions/creating-actions/releasing-and-maintaining-actions const targetVersion = semver_1.default.parse(releaseTag.replace(/^v/, '')); if (!targetVersion) { + // TODO: We may want to limit semvers to only x.x.x, without the pre-release tags, but for now we'll allow them. core.setFailed(`${releaseTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.`); return; } diff --git a/src/main.ts b/src/main.ts index 1024bda..529ec88 100644 --- a/src/main.ts +++ b/src/main.ts @@ -30,6 +30,7 @@ export async function run(): Promise { // https://docs.github.com/en/actions/creating-actions/releasing-and-maintaining-actions const targetVersion = semver.parse(releaseTag.replace(/^v/, '')) if (!targetVersion) { + // TODO: We may want to limit semvers to only x.x.x, without the pre-release tags, but for now we'll allow them. core.setFailed( `${releaseTag} is not a valid semantic version, and so cannot be uploaded as an Immutable Action.` )