Skip to content

Commit 460d665

Browse files
authored
fix(build): update github release assets properly MONGOSH-606 (#672)
1 parent c4347ce commit 460d665

File tree

2 files changed

+49
-34
lines changed

2 files changed

+49
-34
lines changed

packages/build/src/github-repo.spec.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -197,49 +197,55 @@ describe('GithubRepo', () => {
197197
});
198198
});
199199

200-
it('fails if no release can be found', async() => {
201-
const release = {
202-
name: 'release',
203-
tag: 'v0.8.0',
204-
notes: ''
205-
};
206-
getReleaseByTag.resolves(undefined);
207-
try {
208-
await githubRepo.uploadReleaseAsset(release, {
209-
path: 'path',
210-
contentType: 'xyz'
211-
});
212-
} catch (e) {
213-
return expect(e.message).to.contain('Could not look up release for tag');
214-
}
215-
expect.fail('Expected error');
216-
});
217-
218-
it('ignores already exists error', async() => {
200+
it('updates an existing asset', async() => {
219201
const release = {
220202
name: 'release',
221203
tag: 'v0.8.0',
222204
notes: ''
223205
};
224206
getReleaseByTag.resolves({
225-
upload_url: 'url'
207+
upload_url: 'url',
208+
assets: [
209+
{
210+
id: 1,
211+
name: path.basename(__filename),
212+
url: 'assetUrl'
213+
}
214+
]
226215
});
227-
octoRequest.rejects(new ExistsError());
228216

229217
await githubRepo.uploadReleaseAsset(release, {
230218
path: __filename,
231219
contentType: 'xyz'
232220
});
233221
expect(octoRequest).to.have.been.calledWith({
234-
method: 'POST',
235-
url: 'url',
222+
method: 'PATCH',
223+
url: 'assetUrl',
236224
headers: {
237225
'content-type': 'xyz'
238226
},
239227
name: path.basename(__filename),
240228
data: await fs.readFile(__filename)
241229
});
242230
});
231+
232+
it('fails if no release can be found', async() => {
233+
const release = {
234+
name: 'release',
235+
tag: 'v0.8.0',
236+
notes: ''
237+
};
238+
getReleaseByTag.resolves(undefined);
239+
try {
240+
await githubRepo.uploadReleaseAsset(release, {
241+
path: 'path',
242+
contentType: 'xyz'
243+
});
244+
} catch (e) {
245+
return expect(e.message).to.contain('Could not look up release for tag');
246+
}
247+
expect.fail('Expected error');
248+
});
243249
});
244250

245251
describe('releaseToGithub', () => {

packages/build/src/github-repo.ts

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ type ReleaseDetails = {
3131
draft: boolean;
3232
upload_url: string;
3333
id: number;
34+
assets?: ReleaseAsset[]
35+
};
36+
37+
type ReleaseAsset = {
38+
id: number;
39+
name: string;
40+
url: string;
3441
};
3542

3643
export class GithubRepo {
@@ -96,12 +103,7 @@ export class GithubRepo {
96103

97104
/**
98105
* Uploads an asset for a Github release, if the assets already exists
99-
* the operation fails silently.
100-
*
101-
* @param {Release} release
102-
* @param {Asset} asset
103-
* @returns {Promise<void>}
104-
* @memberof GithubRepo
106+
* it will be updated.
105107
*/
106108
async uploadReleaseAsset(release: Release, asset: Asset): Promise<void> {
107109
const releaseDetails = await this.getReleaseByTag(release.tag);
@@ -110,18 +112,25 @@ export class GithubRepo {
110112
throw new Error(`Could not look up release for tag ${release.tag}`);
111113
}
112114

115+
const assetName = path.basename(asset.path);
116+
const existingAsset = releaseDetails.assets?.find(a => a.name === assetName);
117+
113118
const params = {
114-
method: 'POST',
115-
url: releaseDetails.upload_url,
119+
...(existingAsset ? {
120+
method: 'PATCH',
121+
url: existingAsset.url
122+
} : {
123+
method: 'POST',
124+
url: releaseDetails.upload_url
125+
}),
116126
headers: {
117127
'content-type': asset.contentType
118128
},
119-
name: path.basename(asset.path),
129+
name: assetName,
120130
data: await fs.readFile(asset.path)
121131
};
122132

123-
await this.octokit.request(params)
124-
.catch(this._ignoreAlreadyExistsError());
133+
await this.octokit.request(params);
125134
}
126135

127136
/**

0 commit comments

Comments
 (0)