Skip to content

Commit

Permalink
fix(publisher-ers): Update ERS API usage for v2.0
Browse files Browse the repository at this point in the history
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 an 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
  • Loading branch information
ArekSredzki committed Jan 27, 2023
1 parent 0720e07 commit 2fd43d0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
27 changes: 14 additions & 13 deletions packages/publisher/electron-release-server/src/PublisherERS.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ const d = debug('electron-forge:publish:ers');

interface ERSVersion {
name: string;
assets: { name: string }[];
flavor?: string;
assets: {
name: string;
platform: string;
}[];
flavor: { name: string };
}

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

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

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

const versions: ERSVersion[] = await (await authFetch('api/version')).json();
const existingVersion = versions.find((version) => {
return version.name === packageJSON.version && (!version.flavor || version.flavor === flavor);
return version.name === packageJSON.version && version.flavor.name === flavor;
});

let channel = 'stable';
Expand All @@ -97,12 +100,11 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
await authFetch('api/version', {
method: 'POST',
body: JSON.stringify({
channel: {
name: channel,
},
flavor: config.flavor,
channel: channel,
flavor: flavor,
name: packageJSON.version,
notes: '',
id: packageJSON.version + '_' + channel,
}),
headers: {
'Content-Type': 'application/json',
Expand All @@ -115,10 +117,10 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
updateStatusLine();

await Promise.all(
artifacts.map(async (artifactPath) => {
artifacts.map(async (artifactPath: string) => {
const platform = ersPlatform(makeResult.platform, makeResult.arch);
if (existingVersion) {
const existingAsset = existingVersion.assets.find((asset) => asset.name === path.basename(artifactPath));

const existingAsset = existingVersion.assets.find((asset) => asset.name === path.basename(artifactPath) && asset.platform === platform);
if (existingAsset) {
d('asset at path:', artifactPath, 'already exists on server');
uploaded += 1;
Expand All @@ -130,8 +132,7 @@ export default class PublisherERS extends PublisherBase<PublisherERSConfig> {
const artifactForm = new FormData();
artifactForm.append('token', token);
artifactForm.append('version', `${packageJSON.version}_${flavor}`);
artifactForm.append('platform', ersPlatform(makeResult.platform, makeResult.arch));

artifactForm.append('platform', platform);
// see https://github.com/form-data/form-data/issues/426
const fileOptions = {
knownLength: fs.statSync(artifactPath).size,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('PublisherERS', () => {
// mock login
fetch.postOnce('path:/api/auth/login', { body: { token }, status: 200 });
// mock fetch all existing versions
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [], flavor: 'default' }], status: 200 });
fetch.getOnce('path:/api/version', { body: [{ name: '2.0.0', assets: [], flavor: { name: 'default' } }], status: 200 });
// mock creating a new version
fetch.postOnce('path:/api/version', { status: 200 });
// mock asset upload
Expand Down Expand Up @@ -65,7 +65,7 @@ describe('PublisherERS', () => {

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

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

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

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

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

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

0 comments on commit 2fd43d0

Please sign in to comment.