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

feat(publisher-github): assets management #1761

Merged
merged 7 commits into from
Sep 25, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/publisher/github/src/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,8 @@ export interface PublisherGitHubConfig {
* Prepended to the package version to determine the release name (default "v")
*/
tagPrefix?: string;
/**
* Re-upload the new asset if you upload an asset with the same filename as existing asset
*/
force?: boolean;
erickzhao marked this conversation as resolved.
Show resolved Hide resolved
}
19 changes: 13 additions & 6 deletions packages/publisher/github/src/PublisherGithub.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import path from 'path';

import { PublisherBase, PublisherOptions } from '@electron-forge/publisher-base';
import { ForgeMakeResult } from '@electron-forge/shared-types';
import { GetResponseDataTypeFromEndpointMethod } from '@octokit/types';
Expand Down Expand Up @@ -98,10 +96,19 @@ export default class PublisherGithub extends PublisherBase<PublisherGitHubConfig
uploaded += 1;
updateUploadStatus();
};
const artifactName = path.basename(artifactPath);
const artifactName = GitHub.sanitizeName(artifactPath);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (release!.assets.find((asset: OctokitReleaseAsset) => asset.name === artifactName)) {
return done();
const asset = release!.assets.find((item: OctokitReleaseAsset) => item.name === artifactName);
if (asset !== undefined) {
if (config.force === true) {
await github.getGitHub().repos.deleteReleaseAsset({
owner: config.repository.owner,
repo: config.repository.name,
asset_id: asset.id,
});
} else {
return done();
}
}
await github.getGitHub().repos.uploadReleaseAsset({
owner: config.repository.owner,
Expand All @@ -116,7 +123,7 @@ export default class PublisherGithub extends PublisherBase<PublisherGitHubConfig
'content-type': mime.lookup(artifactPath) || 'application/octet-stream',
'content-length': (await fs.stat(artifactPath)).size,
},
name: path.basename(artifactPath),
name: artifactName,
});
return done();
})
Expand Down
13 changes: 13 additions & 0 deletions packages/publisher/github/src/util/github.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import path from 'path';

import { OctokitOptions } from '@octokit/core/dist-types/types.d';
import { retry } from '@octokit/plugin-retry';
import { Octokit } from '@octokit/rest';
Expand Down Expand Up @@ -45,4 +47,15 @@ export default class GitHub {
const github = new RetryableOctokit(options);
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
static sanitizeName(name: string): string {
return path
.basename(name)
.replace(/\.+/g, '.')
.replace(/^\./g, '')
.replace(/\.$/g, '')
.replace(/[^\w.-]+/g, '-');
}
}
14 changes: 14 additions & 0 deletions packages/publisher/github/test/github_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,18 @@ describe('GitHub', () => {
}).to.throw('Please set GITHUB_TOKEN in your environment to access these features');
});
});

describe('sanitizeName', () => {
it('should remove leading and trailing periods from the basename', () => {
expect(GitHub.sanitizeName('path/to/.foo.')).to.equal('foo');
});

it('should remove multiple periods in a row', () => {
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');
});
});
});