From 7ae554b479f06db535ec7dfafcfb5939a5efd670 Mon Sep 17 00:00:00 2001 From: David Sanders Date: Wed, 21 Feb 2024 16:39:47 -0800 Subject: [PATCH] fix(publisher-github): don't sanitize asset names before upload (#3485) Co-authored-by: Erick Zhao --- packages/publisher/github/package.json | 3 + .../publisher/github/src/PublisherGithub.ts | 55 +++++++++++++------ packages/publisher/github/src/util/github.ts | 24 +++++--- packages/publisher/github/test/github_spec.ts | 17 +++++- 4 files changed, 72 insertions(+), 27 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..803bf90605 100644 --- a/packages/publisher/github/src/PublisherGithub.ts +++ b/packages/publisher/github/src/PublisherGithub.ts @@ -1,7 +1,12 @@ +import path from 'node: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 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 1b6e77f8fb..3e246e8f4b 100644 --- a/packages/publisher/github/test/github_spec.ts +++ b/packages/publisher/github/test/github_spec.ts @@ -115,8 +115,21 @@ describe('GitHub', () => { 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); + }); + + it('should remove diacritics', () => { + expect(GitHub.sanitizeName('électron')).to.equal('electron'); }); }); });