Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

github(summary): use relative path for artifact link and fix filename #368

Merged
merged 3 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This dummy secret is used for testing the behavior of a user having repo name as secret value.

-
name: Check coverage
run: |
Expand Down
27 changes: 27 additions & 0 deletions __tests__/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`;
Expand Down
24 changes: 20 additions & 4 deletions src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -221,7 +237,7 @@ export class GitHub {
.addRaw(addLink('Learn more', 'https://docs.docker.com/go/build-summary/'))
.addRaw('</p>')
.addRaw(`<p>`)
.addRaw(`:arrow_down: ${addLink(`<strong>${opts.uploadRes.filename}</strong>`, opts.uploadRes.url)} (${Util.formatFileSize(opts.uploadRes.size)})`)
.addRaw(`:arrow_down: ${addLink(`<strong>${Util.stringToUnicodeEntities(opts.uploadRes.filename)}</strong>`, artifactRelativeURL)} (${Util.formatFileSize(opts.uploadRes.size)})`)
.addBreak()
.addRaw(`This file includes <strong>${refsSize} build record${refsSize > 1 ? 's' : ''}</strong>.`)
.addRaw(`</p>`)
Expand All @@ -248,7 +264,7 @@ export class GitHub {
// prettier-ignore
summaryTableData.push([
{data: `<code>${ref.substring(0, 6).toUpperCase()}</code>`},
{data: `<strong>${summary.name}</strong>`},
{data: `<strong>${Util.stringToUnicodeEntities(summary.name)}</strong>`},
{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}
Expand Down
6 changes: 6 additions & 0 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
}
}
Loading