diff --git a/packages/build/src/github-repo.spec.ts b/packages/build/src/github-repo.spec.ts index 53bd2c056..f8846d2bd 100644 --- a/packages/build/src/github-repo.spec.ts +++ b/packages/build/src/github-repo.spec.ts @@ -161,13 +161,20 @@ describe('GithubRepo', () => { describe('uploadReleaseAsset', () => { let octoRequest: SinonStub; let getReleaseByTag: SinonStub; + let deleteReleaseAsset: SinonStub; + beforeEach(() => { octoRequest = sinon.stub(); octoRequest.resolves(); getReleaseByTag = sinon.stub(); getReleaseByTag.rejects(); + deleteReleaseAsset = sinon.stub(); + deleteReleaseAsset.rejects(); githubRepo = getTestGithubRepo({ - request: octoRequest + request: octoRequest, + repos: { + deleteReleaseAsset + } }); githubRepo.getReleaseByTag = getReleaseByTag; }); @@ -186,6 +193,7 @@ describe('GithubRepo', () => { path: __filename, contentType: 'xyz' }); + expect(deleteReleaseAsset).to.not.have.been.called; expect(octoRequest).to.have.been.calledWith({ method: 'POST', url: 'url', @@ -197,7 +205,7 @@ describe('GithubRepo', () => { }); }); - it('updates an existing asset', async() => { + it('updates an existing asset by removing the old one first', async() => { const release = { name: 'release', tag: 'v0.8.0', @@ -213,14 +221,20 @@ describe('GithubRepo', () => { } ] }); + deleteReleaseAsset.resolves(); await githubRepo.uploadReleaseAsset(release, { path: __filename, contentType: 'xyz' }); + expect(deleteReleaseAsset).to.have.been.calledWith( { + owner: 'mongodb-js', + repo: 'mongosh', + asset_id: 1 + }); expect(octoRequest).to.have.been.calledWith({ - method: 'PATCH', - url: 'assetUrl', + method: 'POST', + url: 'url', headers: { 'content-type': 'xyz' }, diff --git a/packages/build/src/github-repo.ts b/packages/build/src/github-repo.ts index 3512f887d..d07eb2d64 100644 --- a/packages/build/src/github-repo.ts +++ b/packages/build/src/github-repo.ts @@ -103,7 +103,7 @@ export class GithubRepo { /** * Uploads an asset for a Github release, if the assets already exists - * it will be updated. + * it will be removed and re-uploaded. */ async uploadReleaseAsset(release: Release, asset: Asset): Promise { const releaseDetails = await this.getReleaseByTag(release.tag); @@ -115,14 +115,16 @@ export class GithubRepo { const assetName = path.basename(asset.path); const existingAsset = releaseDetails.assets?.find(a => a.name === assetName); + if (existingAsset) { + await this.octokit.repos.deleteReleaseAsset({ + ...this.repo, + asset_id: existingAsset.id + }); + } + const params = { - ...(existingAsset ? { - method: 'PATCH', - url: existingAsset.url - } : { - method: 'POST', - url: releaseDetails.upload_url - }), + method: 'POST', + url: releaseDetails.upload_url, headers: { 'content-type': asset.contentType },