Skip to content

Commit 6c6fdff

Browse files
authored
Merge pull request #368 from crazy-max/summary-artifact-relative-link
github(summary): use relative path for artifact link and fix filename
2 parents 0da8b90 + edcf239 commit 6c6fdff

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

.github/workflows/test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ jobs:
149149
env:
150150
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
151151
CTN_BUILDER_NAME: ${{ steps.builder.outputs.name }}
152+
TEST_FOR_SUMMARY: ${{ secrets.TEST_FOR_SUMMARY }}
152153
-
153154
name: Check coverage
154155
run: |

__tests__/util.test.ts

+27
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,33 @@ describe('generateRandomString', () => {
353353
});
354354
});
355355

356+
describe('stringToUnicodeEntities', () => {
357+
it('should convert a string to Unicode entities', () => {
358+
const input = 'Hello, World!';
359+
const expected = 'Hello, World!';
360+
const result = Util.stringToUnicodeEntities(input);
361+
expect(result).toEqual(expected);
362+
});
363+
it('should handle an empty string', () => {
364+
const input = '';
365+
const expected = '';
366+
const result = Util.stringToUnicodeEntities(input);
367+
expect(result).toEqual(expected);
368+
});
369+
it('should handle special characters', () => {
370+
const input = '@#^&*()';
371+
const expected = '@#^&*()';
372+
const result = Util.stringToUnicodeEntities(input);
373+
expect(result).toEqual(expected);
374+
});
375+
it('should handle non-English characters', () => {
376+
const input = 'こんにちは';
377+
const expected = 'こんにちは';
378+
const result = Util.stringToUnicodeEntities(input);
379+
expect(result).toEqual(expected);
380+
});
381+
});
382+
356383
// See: https://github.com/actions/toolkit/blob/a1b068ec31a042ff1e10a522d8fdf0b8869d53ca/packages/core/src/core.ts#L89
357384
function getInputName(name: string): string {
358385
return `INPUT_${name.replace(/ /g, '_').toUpperCase()}`;

src/github.ts

+20-4
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,18 @@ export class GitHub {
6868
return `${github.context.repo.owner}/${github.context.repo.repo}`;
6969
}
7070

71-
public static workflowRunURL(setAttempts?: boolean): string {
71+
static get runId(): number {
72+
return process.env.GITHUB_RUN_ID ? +process.env.GITHUB_RUN_ID : github.context.runId;
73+
}
74+
75+
static get runAttempt(): number {
7276
// TODO: runAttempt is not yet part of github.context but will be in a
7377
// future release of @actions/github package: https://github.com/actions/toolkit/commit/faa425440f86f9c16587a19dfb59491253a2c92a
74-
return `${GitHub.serverURL}/${GitHub.repository}/actions/runs/${github.context.runId}${setAttempts ? `/attempts/${process.env.GITHUB_RUN_ATTEMPT || 1}` : ''}`;
78+
return process.env.GITHUB_RUN_ATTEMPT ? +process.env.GITHUB_RUN_ATTEMPT : 1;
79+
}
80+
81+
public static workflowRunURL(setAttempts?: boolean): string {
82+
return `${GitHub.serverURL}/${GitHub.repository}/actions/runs/${GitHub.runId}${setAttempts ? `/attempts/${GitHub.runAttempt}` : ''}`;
7583
}
7684

7785
static get actionsRuntimeToken(): GitHubActionsRuntimeToken | undefined {
@@ -211,6 +219,14 @@ export class GitHub {
211219

212220
const refsSize = Object.keys(opts.exportRes.refs).length;
213221

222+
// we just need the last two parts of the URL as they are always relative
223+
// to the workflow run URL otherwise URL could be broken if GitHub
224+
// repository name is part of a secret value used in the workflow. e.g.:
225+
// artifact: https://github.com/docker/actions-toolkit/actions/runs/9552208295/artifacts/1609622746
226+
// workflow: https://github.com/docker/actions-toolkit/actions/runs/9552208295
227+
// https://github.com/docker/actions-toolkit/issues/367
228+
const artifactRelativeURL = `./${GitHub.runId}/${opts.uploadRes.url.split('/').slice(-2).join('/')}`;
229+
214230
// prettier-ignore
215231
const sum = core.summary
216232
.addHeading('Docker Build summary', 1)
@@ -221,7 +237,7 @@ export class GitHub {
221237
.addRaw(addLink('Learn more', 'https://docs.docker.com/go/build-summary/'))
222238
.addRaw('</p>')
223239
.addRaw(`<p>`)
224-
.addRaw(`:arrow_down: ${addLink(`<strong>${opts.uploadRes.filename}</strong>`, opts.uploadRes.url)} (${Util.formatFileSize(opts.uploadRes.size)})`)
240+
.addRaw(`:arrow_down: ${addLink(`<strong>${Util.stringToUnicodeEntities(opts.uploadRes.filename)}</strong>`, artifactRelativeURL)} (${Util.formatFileSize(opts.uploadRes.size)})`)
225241
.addBreak()
226242
.addRaw(`This file includes <strong>${refsSize} build record${refsSize > 1 ? 's' : ''}</strong>.`)
227243
.addRaw(`</p>`)
@@ -248,7 +264,7 @@ export class GitHub {
248264
// prettier-ignore
249265
summaryTableData.push([
250266
{data: `<code>${ref.substring(0, 6).toUpperCase()}</code>`},
251-
{data: `<strong>${summary.name}</strong>`},
267+
{data: `<strong>${Util.stringToUnicodeEntities(summary.name)}</strong>`},
252268
{data: `${summary.status === 'completed' ? ':white_check_mark:' : summary.status === 'canceled' ? ':no_entry_sign:' : ':x:'} ${summary.status}`},
253269
{data: `${summary.numCachedSteps > 0 ? Math.round((summary.numCachedSteps / summary.numTotalSteps) * 100) : 0}%`},
254270
{data: summary.duration}

src/util.ts

+6
Original file line numberDiff line numberDiff line change
@@ -179,4 +179,10 @@ export class Util {
179179
const bytes = crypto.randomBytes(Math.ceil(length / 2));
180180
return bytes.toString('hex').slice(0, length);
181181
}
182+
183+
public static stringToUnicodeEntities(str: string) {
184+
return Array.from(str)
185+
.map(char => `&#x${char.charCodeAt(0).toString(16)};`)
186+
.join('');
187+
}
182188
}

0 commit comments

Comments
 (0)