diff --git a/src/publish-beta/package.spec.ts b/src/publish-beta/package.spec.ts index b97b81e..c2b6995 100644 --- a/src/publish-beta/package.spec.ts +++ b/src/publish-beta/package.spec.ts @@ -25,13 +25,15 @@ describe('package', () => { it('generatePackageBetaTag', async () => { process.env['GITHUB_REF'] = '/ref/2406/branch'; + process.env['GITHUB_SHA'] = '1234'; const packageBetaTag = generatePackageBetaTag(); - assert.ok(packageBetaTag.startsWith('2406-')); + assert.ok(packageBetaTag.startsWith('PR.2406-')); }); it('test packageJSON Update and add /src/ to files', async () => { const filePath = path.join(tmpdir(), 'packageUpdate/package.json'); process.env['GITHUB_REF'] = '/ref/87/branch'; + process.env['GITHUB_SHA'] = '12345678ad90'; await writeFile( path.join(tmpdir(), 'packageUpdate/package.json'), JSON.stringify({ name: 'testpackage', version: '1.2.10', files: ['/dist/'] }) @@ -40,7 +42,7 @@ describe('package', () => { await packageJSONUpdate(path.join(tmpdir(), 'packageUpdate')); const rawUpdatedFile = await readFile(filePath, 'utf8'); - assert.ok(JSON.parse(rawUpdatedFile).version.startsWith('1.2.10-beta.87-')); + assert.ok(JSON.parse(rawUpdatedFile).version === '1.2.10-PR.87-ad90'); assert.deepEqual(JSON.parse(rawUpdatedFile).files.sort(), ['/dist/', '/src/'].sort()); }); diff --git a/src/publish-beta/package.ts b/src/publish-beta/package.ts index 36ef17f..b9e4b04 100644 --- a/src/publish-beta/package.ts +++ b/src/publish-beta/package.ts @@ -4,7 +4,6 @@ import path from 'node:path'; import { readFile, writeFile } from 'node:fs/promises'; import { debug } from 'debug'; -import shortId from './short-id'; import { getPRNumber } from './github'; import { removeNonTSFiles } from './files'; @@ -16,10 +15,16 @@ interface PackageJSON { files: string[]; } +const NUMBER_OF_CHARS_TO_USE_FROM_COMMIT_SHA = 4; + export function generatePackageBetaTag(): string { - const id = shortId(); + const commentSha = process.env['GITHUB_SHA']; + if (!commentSha) { + throw new Error('Unable to get GITHUB_SHA'); + } + const id = commentSha.slice(-NUMBER_OF_CHARS_TO_USE_FROM_COMMIT_SHA, commentSha.length); const prNumber = getPRNumber(); - return `${prNumber}-${id}`; + return `PR.${prNumber}-${id}`; } function checkFilesPropertyExists(packageJSON: string): void { @@ -45,7 +50,7 @@ export async function packageJSONUpdate(rootProjectDirectory: string): Promise { - it('Length and multiple calls', async () => { - const id1 = shortId(); - assert.equal(id1.length, 5); - const id2 = shortId(); - assert.notEqual(id1, id2); - }); -}); diff --git a/src/publish-beta/short-id.ts b/src/publish-beta/short-id.ts deleted file mode 100644 index 39d762e..0000000 --- a/src/publish-beta/short-id.ts +++ /dev/null @@ -1,11 +0,0 @@ -// publish-beta/short-id.ts -const SHORT_ID_LENGTH = 5; -export default function (): string { - let text = ''; - // eslint-disable-next-line no-secrets/no-secrets - const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - for (let index = 0; index < SHORT_ID_LENGTH; index++) { - text += possible.charAt(Math.floor(Math.random() * possible.length)); - } - return text; -}