From 861f98f0e313392e79a12bb360a6ff347d69a23f Mon Sep 17 00:00:00 2001 From: David Sanders Date: Wed, 31 Jan 2024 19:16:26 -0800 Subject: [PATCH 1/4] fix(publisher-github): don't sanitize asset names before upload --- packages/publisher/github/package.json | 3 + .../publisher/github/src/PublisherGithub.ts | 55 +++++++++++++------ packages/publisher/github/src/util/github.ts | 4 +- packages/publisher/github/test/github_spec.ts | 13 ++++- 4 files changed, 54 insertions(+), 21 deletions(-) diff --git a/packages/publisher/github/package.json b/packages/publisher/github/package.json index 20900886be..dbd4b8bfd5 100644 --- a/packages/publisher/github/package.json +++ b/packages/publisher/github/package.json @@ -25,10 +25,13 @@ "@electron-forge/shared-types": "7.3.0", "@octokit/core": "^3.2.4", "@octokit/plugin-retry": "^3.0.9", + "@octokit/request-error": "^2.0.5", "@octokit/rest": "^18.0.11", "@octokit/types": "^6.1.2", + "chalk": "^4.0.0", "debug": "^4.3.1", "fs-extra": "^10.0.0", + "log-symbols": "^4.0.0", "mime-types": "^2.1.25" }, "publishConfig": { diff --git a/packages/publisher/github/src/PublisherGithub.ts b/packages/publisher/github/src/PublisherGithub.ts index 71f8d576fd..aacd6bf7d1 100644 --- a/packages/publisher/github/src/PublisherGithub.ts +++ b/packages/publisher/github/src/PublisherGithub.ts @@ -1,7 +1,12 @@ +import path from 'path'; + import { PublisherBase, PublisherOptions } from '@electron-forge/publisher-base'; import { ForgeMakeResult } from '@electron-forge/shared-types'; +import { RequestError } from '@octokit/request-error'; import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types'; +import chalk from 'chalk'; import fs from 'fs-extra'; +import logSymbols from 'log-symbols'; import mime from 'mime-types'; import { PublisherGitHubConfig } from './Config'; @@ -97,9 +102,10 @@ export default class PublisherGithub extends PublisherBase item.name === artifactName); + const asset = release!.assets.find((item: OctokitReleaseAsset) => item.name === sanitizedArtifactName); if (asset !== undefined) { if (config.force === true) { await github.getGitHub().repos.deleteReleaseAsset({ @@ -111,21 +117,36 @@ export default class PublisherGithub extends PublisherBase { expect(GitHub.sanitizeName('path/to/foo..bar')).to.equal('foo.bar'); }); - it('should replace non-alphanumeric, non-hyphen characters with hyphens', () => { - expect(GitHub.sanitizeName('path/to/foo%$bar baz.')).to.equal('foo-bar-baz'); + it('should replace non-alphanumeric, non-hyphen characters with periods', () => { + expect(GitHub.sanitizeName('path/to/foo%$bar baz.')).to.equal('foo.bar.baz'); + }); + + it('should preserve special symbols', () => { + expect(GitHub.sanitizeName('path/to/@foo+bar')).to.equal('@foo+bar'); + }); + + it('should preserve hyphens', () => { + const name = 'electron-fiddle-0.99.0-full.nupkg'; + expect(GitHub.sanitizeName(`path/to/${name}`)).to.equal(name); }); }); }); From 69b7554542e3bc7097f9f319df199ba1d17d5b3c Mon Sep 17 00:00:00 2001 From: David Sanders Date: Mon, 5 Feb 2024 17:40:31 -0800 Subject: [PATCH 2/4] chore: update import Co-authored-by: Erick Zhao --- packages/publisher/github/src/PublisherGithub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/publisher/github/src/PublisherGithub.ts b/packages/publisher/github/src/PublisherGithub.ts index aacd6bf7d1..32d6a83161 100644 --- a/packages/publisher/github/src/PublisherGithub.ts +++ b/packages/publisher/github/src/PublisherGithub.ts @@ -1,4 +1,4 @@ -import path from 'path'; +import path from 'node:path'; import { PublisherBase, PublisherOptions } from '@electron-forge/publisher-base'; import { ForgeMakeResult } from '@electron-forge/shared-types'; From 9aa4b821500ef76ebe6efa442e4cf12c7e7f785a Mon Sep 17 00:00:00 2001 From: David Sanders Date: Mon, 5 Feb 2024 17:41:39 -0800 Subject: [PATCH 3/4] chore: update error message --- packages/publisher/github/src/PublisherGithub.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/publisher/github/src/PublisherGithub.ts b/packages/publisher/github/src/PublisherGithub.ts index 32d6a83161..803bf90605 100644 --- a/packages/publisher/github/src/PublisherGithub.ts +++ b/packages/publisher/github/src/PublisherGithub.ts @@ -143,7 +143,7 @@ export default class PublisherGithub extends PublisherBase Date: Wed, 21 Feb 2024 09:38:19 -0800 Subject: [PATCH 4/4] chore: better match GitHub sanitize algorithm --- packages/publisher/github/src/util/github.ts | 24 ++++++++++++------- packages/publisher/github/test/github_spec.ts | 6 ++++- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/packages/publisher/github/src/util/github.ts b/packages/publisher/github/src/util/github.ts index 29a3c6c939..f632dc08d8 100644 --- a/packages/publisher/github/src/util/github.ts +++ b/packages/publisher/github/src/util/github.ts @@ -48,14 +48,22 @@ export default class GitHub { return github; } - // Based on https://docs.github.com/en/rest/releases/assets?apiVersion=2022-11-28#upload-a-release-asset and - // https://stackoverflow.com/questions/59081778/rules-for-special-characters-in-github-repository-name + // Based on https://github.com/cli/cli/blob/b07f955c23fb54c400b169d39255569e240b324e/pkg/cmd/release/upload/upload.go#L131-L153 static sanitizeName(name: string): string { - return path - .basename(name) - .replace(/[^\w.@+-]+/g, '.') - .replace(/\.+/g, '.') - .replace(/^\./g, '') - .replace(/\.$/g, ''); + return ( + path + .basename(name) + // Remove diacritics (e.g. é -> e) + .normalize('NFD') + .replace(/\p{Diacritic}/gu, '') + // Replace special characters with dot + .replace(/[^\w_.@+-]+/g, '.') + // Replace multiple dots with a single dot + .replace(/\.+/g, '.') + // Remove leading dot if present + .replace(/^\./g, '') + // Remove trailing dot if present + .replace(/\.$/g, '') + ); } } diff --git a/packages/publisher/github/test/github_spec.ts b/packages/publisher/github/test/github_spec.ts index b23f073baf..3e246e8f4b 100644 --- a/packages/publisher/github/test/github_spec.ts +++ b/packages/publisher/github/test/github_spec.ts @@ -120,12 +120,16 @@ describe('GitHub', () => { }); it('should preserve special symbols', () => { - expect(GitHub.sanitizeName('path/to/@foo+bar')).to.equal('@foo+bar'); + expect(GitHub.sanitizeName('path/to/@foo+bar_')).to.equal('@foo+bar_'); }); it('should preserve hyphens', () => { const name = 'electron-fiddle-0.99.0-full.nupkg'; expect(GitHub.sanitizeName(`path/to/${name}`)).to.equal(name); }); + + it('should remove diacritics', () => { + expect(GitHub.sanitizeName('électron')).to.equal('electron'); + }); }); });