Skip to content

Commit 3557e73

Browse files
authored
chore(build): ensure to remove a GitHub asset before re-uploading (#678)
Updating an asset does not work with a PATCH call - only the name of an asset and other properties can be updated that way, not the binary itself.
1 parent f96b9e5 commit 3557e73

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

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

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,20 @@ describe('GithubRepo', () => {
161161
describe('uploadReleaseAsset', () => {
162162
let octoRequest: SinonStub;
163163
let getReleaseByTag: SinonStub;
164+
let deleteReleaseAsset: SinonStub;
165+
164166
beforeEach(() => {
165167
octoRequest = sinon.stub();
166168
octoRequest.resolves();
167169
getReleaseByTag = sinon.stub();
168170
getReleaseByTag.rejects();
171+
deleteReleaseAsset = sinon.stub();
172+
deleteReleaseAsset.rejects();
169173
githubRepo = getTestGithubRepo({
170-
request: octoRequest
174+
request: octoRequest,
175+
repos: {
176+
deleteReleaseAsset
177+
}
171178
});
172179
githubRepo.getReleaseByTag = getReleaseByTag;
173180
});
@@ -186,6 +193,7 @@ describe('GithubRepo', () => {
186193
path: __filename,
187194
contentType: 'xyz'
188195
});
196+
expect(deleteReleaseAsset).to.not.have.been.called;
189197
expect(octoRequest).to.have.been.calledWith({
190198
method: 'POST',
191199
url: 'url',
@@ -197,7 +205,7 @@ describe('GithubRepo', () => {
197205
});
198206
});
199207

200-
it('updates an existing asset', async() => {
208+
it('updates an existing asset by removing the old one first', async() => {
201209
const release = {
202210
name: 'release',
203211
tag: 'v0.8.0',
@@ -213,14 +221,20 @@ describe('GithubRepo', () => {
213221
}
214222
]
215223
});
224+
deleteReleaseAsset.resolves();
216225

217226
await githubRepo.uploadReleaseAsset(release, {
218227
path: __filename,
219228
contentType: 'xyz'
220229
});
230+
expect(deleteReleaseAsset).to.have.been.calledWith( {
231+
owner: 'mongodb-js',
232+
repo: 'mongosh',
233+
asset_id: 1
234+
});
221235
expect(octoRequest).to.have.been.calledWith({
222-
method: 'PATCH',
223-
url: 'assetUrl',
236+
method: 'POST',
237+
url: 'url',
224238
headers: {
225239
'content-type': 'xyz'
226240
},

packages/build/src/github-repo.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export class GithubRepo {
103103

104104
/**
105105
* Uploads an asset for a Github release, if the assets already exists
106-
* it will be updated.
106+
* it will be removed and re-uploaded.
107107
*/
108108
async uploadReleaseAsset(release: Release, asset: Asset): Promise<void> {
109109
const releaseDetails = await this.getReleaseByTag(release.tag);
@@ -115,14 +115,16 @@ export class GithubRepo {
115115
const assetName = path.basename(asset.path);
116116
const existingAsset = releaseDetails.assets?.find(a => a.name === assetName);
117117

118+
if (existingAsset) {
119+
await this.octokit.repos.deleteReleaseAsset({
120+
...this.repo,
121+
asset_id: existingAsset.id
122+
});
123+
}
124+
118125
const params = {
119-
...(existingAsset ? {
120-
method: 'PATCH',
121-
url: existingAsset.url
122-
} : {
123-
method: 'POST',
124-
url: releaseDetails.upload_url
125-
}),
126+
method: 'POST',
127+
url: releaseDetails.upload_url,
126128
headers: {
127129
'content-type': asset.contentType
128130
},

0 commit comments

Comments
 (0)