From 3162c096bdb5f4ae77ee9acc1d16029ca624501b Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Mon, 17 Jun 2024 22:04:28 +0200 Subject: [PATCH 1/3] github(summary): use relative path for artifact link Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- .github/workflows/test.yml | 1 + src/github.ts | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7bb02cc..4a59c5a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -149,6 +149,7 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} CTN_BUILDER_NAME: ${{ steps.builder.outputs.name }} + TEST_FOR_SUMMARY: ${{ secrets.TEST_FOR_SUMMARY }} - name: Check coverage run: | diff --git a/src/github.ts b/src/github.ts index 39bfdcf..2075b8a 100644 --- a/src/github.ts +++ b/src/github.ts @@ -68,10 +68,18 @@ export class GitHub { return `${github.context.repo.owner}/${github.context.repo.repo}`; } - public static workflowRunURL(setAttempts?: boolean): string { + static get runId(): number { + return process.env.GITHUB_RUN_ID ? +process.env.GITHUB_RUN_ID : github.context.runId; + } + + static get runAttempt(): number { // TODO: runAttempt is not yet part of github.context but will be in a // future release of @actions/github package: https://github.com/actions/toolkit/commit/faa425440f86f9c16587a19dfb59491253a2c92a - return `${GitHub.serverURL}/${GitHub.repository}/actions/runs/${github.context.runId}${setAttempts ? `/attempts/${process.env.GITHUB_RUN_ATTEMPT || 1}` : ''}`; + return process.env.GITHUB_RUN_ATTEMPT ? +process.env.GITHUB_RUN_ATTEMPT : 1; + } + + public static workflowRunURL(setAttempts?: boolean): string { + return `${GitHub.serverURL}/${GitHub.repository}/actions/runs/${GitHub.runId}${setAttempts ? `/attempts/${GitHub.runAttempt}` : ''}`; } static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined { @@ -211,6 +219,14 @@ export class GitHub { const refsSize = Object.keys(opts.exportRes.refs).length; + // we just need the last two parts of the URL as they are always relative + // to the workflow run URL otherwise URL could be broken if GitHub + // repository name is part of a secret value used in the workflow. e.g.: + // artifact: https://github.com/docker/actions-toolkit/actions/runs/9552208295/artifacts/1609622746 + // workflow: https://github.com/docker/actions-toolkit/actions/runs/9552208295 + // https://github.com/docker/actions-toolkit/issues/367 + const artifactRelativeURL = `./${GitHub.runId}/${opts.uploadRes.url.split('/').slice(-2).join('/')}`; + // prettier-ignore const sum = core.summary .addHeading('Docker Build summary', 1) @@ -221,7 +237,7 @@ export class GitHub { .addRaw(addLink('Learn more', 'https://docs.docker.com/go/build-summary/')) .addRaw('

') .addRaw(`

`) - .addRaw(`:arrow_down: ${addLink(`${opts.uploadRes.filename}`, opts.uploadRes.url)} (${Util.formatFileSize(opts.uploadRes.size)})`) + .addRaw(`:arrow_down: ${addLink(`${opts.uploadRes.filename}`, artifactRelativeURL)} (${Util.formatFileSize(opts.uploadRes.size)})`) .addBreak() .addRaw(`This file includes ${refsSize} build record${refsSize > 1 ? 's' : ''}.`) .addRaw(`

`) From e26a82d0aa3dc8ebc0f6854091ac41ea96b3f6fa Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:07:40 +0200 Subject: [PATCH 2/3] util: stringToUnicodeEntities func Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- __tests__/util.test.ts | 27 +++++++++++++++++++++++++++ src/util.ts | 6 ++++++ 2 files changed, 33 insertions(+) diff --git a/__tests__/util.test.ts b/__tests__/util.test.ts index 2e67ec2..9305a93 100644 --- a/__tests__/util.test.ts +++ b/__tests__/util.test.ts @@ -353,6 +353,33 @@ describe('generateRandomString', () => { }); }); +describe('stringToUnicodeEntities', () => { + it('should convert a string to Unicode entities', () => { + const input = 'Hello, World!'; + const expected = 'Hello, World!'; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); + it('should handle an empty string', () => { + const input = ''; + const expected = ''; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); + it('should handle special characters', () => { + const input = '@#^&*()'; + const expected = '@#^&*()'; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); + it('should handle non-English characters', () => { + const input = 'こんにちは'; + const expected = 'こんにちは'; + const result = Util.stringToUnicodeEntities(input); + expect(result).toEqual(expected); + }); +}); + // See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89 function getInputName(name: string): string { return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; diff --git a/src/util.ts b/src/util.ts index 4fe0bd2..c781ab5 100644 --- a/src/util.ts +++ b/src/util.ts @@ -179,4 +179,10 @@ export class Util { const bytes = crypto.randomBytes(Math.ceil(length / 2)); return bytes.toString('hex').slice(0, length); } + + public static stringToUnicodeEntities(str: string) { + return Array.from(str) + .map(char => `&#x${char.charCodeAt(0).toString(16)};`) + .join(''); + } } From edcf239f493a3d50b4d693e8e64c80e642de9c4f Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Tue, 18 Jun 2024 10:08:19 +0200 Subject: [PATCH 3/3] github(summary): convert filename and build name to unicode Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com> --- src/github.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/github.ts b/src/github.ts index 2075b8a..5fd1d5a 100644 --- a/src/github.ts +++ b/src/github.ts @@ -237,7 +237,7 @@ export class GitHub { .addRaw(addLink('Learn more', 'https://docs.docker.com/go/build-summary/')) .addRaw('

') .addRaw(`

`) - .addRaw(`:arrow_down: ${addLink(`${opts.uploadRes.filename}`, artifactRelativeURL)} (${Util.formatFileSize(opts.uploadRes.size)})`) + .addRaw(`:arrow_down: ${addLink(`${Util.stringToUnicodeEntities(opts.uploadRes.filename)}`, artifactRelativeURL)} (${Util.formatFileSize(opts.uploadRes.size)})`) .addBreak() .addRaw(`This file includes ${refsSize} build record${refsSize > 1 ? 's' : ''}.`) .addRaw(`

`) @@ -264,7 +264,7 @@ export class GitHub { // prettier-ignore summaryTableData.push([ {data: `${ref.substring(0, 6).toUpperCase()}`}, - {data: `${summary.name}`}, + {data: `${Util.stringToUnicodeEntities(summary.name)}`}, {data: `${summary.status === 'completed' ? ':white_check_mark:' : summary.status === 'canceled' ? ':no_entry_sign:' : ':x:'} ${summary.status}`}, {data: `${summary.numCachedSteps > 0 ? Math.round((summary.numCachedSteps / summary.numTotalSteps) * 100) : 0}%`}, {data: summary.duration}