Skip to content

Commit 3d5c87c

Browse files
authored
fix(publisher-ers): Update ERS API usage for v2.0 (#3149)
Recently, v2.0 of electron-release-server was released. This version updated many of the underlying packages and should be adopted by all users of the server. In the process, the API changed due to a change with SailsJS. This corrects the API usage within the ERS publisher plugin to allow users to continue using it. Note that this issue was reported here: ArekSredzki/electron-release-server#314
1 parent 8ed43ee commit 3d5c87c

File tree

2 files changed

+30
-21
lines changed

2 files changed

+30
-21
lines changed

packages/publisher/electron-release-server/src/PublisherERS.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@ import { PublisherERSConfig } from './Config';
1111

1212
const d = debug('electron-forge:publish:ers');
1313

14+
interface ERSAsset {
15+
name: string;
16+
platform: string;
17+
}
18+
19+
interface ERSFlavor {
20+
name: string;
21+
}
22+
1423
interface ERSVersion {
1524
name: string;
16-
assets: { name: string }[];
17-
flavor?: string;
25+
assets: ERSAsset[];
26+
flavor: ERSFlavor;
1827
}
1928

2029
const fetchAndCheckStatus = async (url: RequestInfo, init?: RequestInit): Promise<Response> => {
@@ -71,16 +80,15 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
7180
const authFetch = (apiPath: string, options?: RequestInit) =>
7281
fetchAndCheckStatus(api(apiPath), { ...(options || {}), headers: { ...(options || {}).headers, Authorization: `Bearer ${token}` } });
7382

74-
const versions: ERSVersion[] = await (await authFetch('api/version')).json();
7583
const flavor = config.flavor || 'default';
7684

7785
for (const makeResult of makeResults) {
7886
const { packageJSON } = makeResult;
7987
const artifacts = makeResult.artifacts.filter((artifactPath) => path.basename(artifactPath).toLowerCase() !== 'releases');
8088

81-
const existingVersion = versions.find((version) => {
82-
return version.name === packageJSON.version && (!version.flavor || version.flavor === flavor);
83-
});
89+
const versions: ERSVersion[] = await (await authFetch('api/version')).json();
90+
// Find the version with the same name and flavor
91+
const existingVersion = versions.find((version) => version.name === packageJSON.version && version.flavor.name === flavor);
8492

8593
let channel = 'stable';
8694
if (config.channel) {
@@ -97,12 +105,11 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
97105
await authFetch('api/version', {
98106
method: 'POST',
99107
body: JSON.stringify({
100-
channel: {
101-
name: channel,
102-
},
103-
flavor: config.flavor,
108+
channel: channel,
109+
flavor: flavor,
104110
name: packageJSON.version,
105111
notes: '',
112+
id: packageJSON.version + '_' + channel,
106113
}),
107114
headers: {
108115
'Content-Type': 'application/json',
@@ -115,10 +122,10 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
115122
updateStatusLine();
116123

117124
await Promise.all(
118-
artifacts.map(async (artifactPath) => {
125+
artifacts.map(async (artifactPath: string) => {
126+
const platform = ersPlatform(makeResult.platform, makeResult.arch);
119127
if (existingVersion) {
120-
const existingAsset = existingVersion.assets.find((asset) => asset.name === path.basename(artifactPath));
121-
128+
const existingAsset = existingVersion.assets.find((asset) => asset.name === path.basename(artifactPath) && asset.platform === platform);
122129
if (existingAsset) {
123130
d('asset at path:', artifactPath, 'already exists on server');
124131
uploaded += 1;
@@ -130,8 +137,7 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
130137
const artifactForm = new FormData();
131138
artifactForm.append('token', token);
132139
artifactForm.append('version', `${packageJSON.version}_${flavor}`);
133-
artifactForm.append('platform', ersPlatform(makeResult.platform, makeResult.arch));
134-
140+
artifactForm.append('platform', platform);
135141
// see https://github.com/form-data/form-data/issues/426
136142
const fileOptions = {
137143
knownLength: fs.statSync(artifactPath).size,

packages/publisher/electron-release-server/test/PublisherERS_spec.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('PublisherERS', () => {
3535
// mock login
3636
fetch.postOnce('path:/api/auth/login', { body: { token }, status: 200 });
3737
// mock fetch all existing versions
38-
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [], flavor: 'default' }], status: 200 });
38+
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [], flavor: { name: 'default' } }], status: 200 });
3939
// mock creating a new version
4040
fetch.postOnce('path:/api/version', { status: 200 });
4141
// mock asset upload
@@ -65,7 +65,7 @@ describe('PublisherERS', () => {
6565

6666
// creates a new version with the correct flavor, name, and channel
6767
expect(calls[2][0]).to.equal(`${baseUrl}/api/version`);
68-
expect(calls[2][1]?.body).to.equal(`{"channel":{"name":"stable"},"flavor":"${flavor}","name":"${version}","notes":""}`);
68+
expect(calls[2][1]?.body).to.equal(`{"channel":"stable","flavor":"${flavor}","name":"${version}","notes":"","id":"${version}_stable"}`);
6969

7070
// uploads asset successfully
7171
expect(calls[3][0]).to.equal(`${baseUrl}/api/asset`);
@@ -83,7 +83,7 @@ describe('PublisherERS', () => {
8383
// mock login
8484
fetch.postOnce('path:/api/auth/login', { body: { token }, status: 200 });
8585
// mock fetch all existing versions
86-
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [], flavor: 'lite' }], status: 200 });
86+
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [], flavor: { name: 'lite' } }], status: 200 });
8787
// mock asset upload
8888
fetch.post('path:/api/asset', { status: 200 });
8989

@@ -123,7 +123,10 @@ describe('PublisherERS', () => {
123123
// mock login
124124
fetch.postOnce('path:/api/auth/login', { body: { token }, status: 200 });
125125
// mock fetch all existing versions
126-
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [{ name: 'existing-artifact' }], flavor: 'default' }], status: 200 });
126+
fetch.getOnce('path:/api/version', {
127+
body: [{ name: '2.0.0', assets: [{ name: 'existing-artifact', platform: 'linux_64' }], flavor: { name: 'default' } }],
128+
status: 200,
129+
});
127130

128131
const publisher = new PublisherERS({
129132
baseUrl,
@@ -158,7 +161,7 @@ describe('PublisherERS', () => {
158161
// mock login
159162
fetch.postOnce('path:/api/auth/login', { body: { token }, status: 200 });
160163
// mock fetch all existing versions
161-
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [{ name: 'existing-artifact' }], flavor: 'default' }], status: 200 });
164+
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [{ name: 'existing-artifact' }], flavor: { name: 'default' } }], status: 200 });
162165
// mock creating a new version
163166
fetch.postOnce('path:/api/version', { status: 200 });
164167
// mock asset upload
@@ -188,7 +191,7 @@ describe('PublisherERS', () => {
188191

189192
// creates a new version with the correct flavor, name, and channel
190193
expect(calls[2][0]).to.equal(`${baseUrl}/api/version`);
191-
expect(calls[2][1]?.body).to.equal(`{"channel":{"name":"stable"},"flavor":"${flavor}","name":"${version}","notes":""}`);
194+
expect(calls[2][1]?.body).to.equal(`{"channel":"stable","flavor":"${flavor}","name":"${version}","notes":"","id":"${version}_stable"}`);
192195

193196
// uploads asset successfully
194197
expect(calls[3][0]).to.equal(`${baseUrl}/api/asset`);

0 commit comments

Comments
 (0)