Skip to content

Commit e6c70a5

Browse files
authored
fix: replace existing release assets on Gitea (#816)
Signed-off-by: Rui Chen <[email protected]>
1 parent f345337 commit e6c70a5

6 files changed

Lines changed: 315 additions & 43 deletions

File tree

‎__tests__/github.test.ts‎

Lines changed: 166 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,107 @@ describe('github', () => {
322322
});
323323

324324
describe('GitHubReleaser', () => {
325+
it('uses GitHub standard asset deletion without adding the release ID', async () => {
326+
const deleteReleaseAsset = vi.fn().mockResolvedValue(undefined);
327+
const request = vi.fn();
328+
const releaser = new GitHubReleaser({
329+
rest: { repos: { deleteReleaseAsset } },
330+
request,
331+
} as any);
332+
333+
await releaser.deleteReleaseAsset({
334+
owner: 'owner',
335+
repo: 'repo',
336+
release_id: 42,
337+
asset_id: 99,
338+
});
339+
340+
expect(deleteReleaseAsset).toHaveBeenCalledOnce();
341+
expect(deleteReleaseAsset).toHaveBeenCalledWith({
342+
owner: 'owner',
343+
repo: 'repo',
344+
asset_id: 99,
345+
});
346+
expect(request).not.toHaveBeenCalled();
347+
});
348+
349+
it('falls back to release-scoped asset deletion after a standard 404', async () => {
350+
const deleteReleaseAsset = vi.fn().mockRejectedValue({
351+
status: 404,
352+
message: 'standard route not found',
353+
});
354+
const request = vi.fn().mockResolvedValue({ status: 204 });
355+
const releaser = new GitHubReleaser({
356+
rest: { repos: { deleteReleaseAsset } },
357+
request,
358+
} as any);
359+
360+
await releaser.deleteReleaseAsset({
361+
owner: 'owner',
362+
repo: 'repo',
363+
release_id: 42,
364+
asset_id: 99,
365+
});
366+
367+
expect(request).toHaveBeenCalledOnce();
368+
expect(request).toHaveBeenCalledWith(
369+
'DELETE /repos/{owner}/{repo}/releases/{release_id}/assets/{asset_id}',
370+
{
371+
owner: 'owner',
372+
repo: 'repo',
373+
release_id: 42,
374+
asset_id: 99,
375+
},
376+
);
377+
});
378+
379+
it('does not mask non-404 asset deletion failures', async () => {
380+
const deletionError = { status: 403, message: 'forbidden' };
381+
const request = vi.fn();
382+
const releaser = new GitHubReleaser({
383+
rest: {
384+
repos: { deleteReleaseAsset: vi.fn().mockRejectedValue(deletionError) },
385+
},
386+
request,
387+
} as any);
388+
389+
await expect(
390+
releaser.deleteReleaseAsset({
391+
owner: 'owner',
392+
repo: 'repo',
393+
release_id: 42,
394+
asset_id: 99,
395+
}),
396+
).rejects.toBe(deletionError);
397+
expect(request).not.toHaveBeenCalled();
398+
});
399+
400+
it('reports both failures when release-scoped deletion also fails', async () => {
401+
const releaser = new GitHubReleaser({
402+
rest: {
403+
repos: {
404+
deleteReleaseAsset: vi
405+
.fn()
406+
.mockRejectedValue({ status: 404, message: 'standard route not found' }),
407+
},
408+
},
409+
request: vi
410+
.fn()
411+
.mockRejectedValue({ status: 404, message: 'release-scoped route not found' }),
412+
} as any);
413+
414+
await expect(
415+
releaser.deleteReleaseAsset({
416+
owner: 'owner',
417+
repo: 'repo',
418+
release_id: 42,
419+
asset_id: 99,
420+
}),
421+
).rejects.toThrow(
422+
'Failed to delete release asset 99. GitHub endpoint: standard route not found; release-scoped endpoint: release-scoped route not found',
423+
);
424+
});
425+
325426
it('passes previous_tag_name to generateReleaseNotes and strips it from createRelease', async () => {
326427
const generateReleaseNotes = vi.fn(async () => ({
327428
data: {
@@ -945,6 +1046,7 @@ describe('github', () => {
9451046
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
9461047
'__tests__/release.txt',
9471048
[],
1049+
1,
9481050
);
9491051

9501052
expect(result).toStrictEqual({ id: 123, name: 'release.txt' });
@@ -956,11 +1058,52 @@ describe('github', () => {
9561058
expect(deleteReleaseAsset).toHaveBeenCalledWith({
9571059
owner: 'owner',
9581060
repo: 'repo',
1061+
release_id: 1,
9591062
asset_id: 99,
9601063
});
9611064
expect(uploadReleaseAsset).toHaveBeenCalledTimes(2);
9621065
});
9631066

1067+
it('does not upload until deletion of an existing asset succeeds', async () => {
1068+
const deletionError = new Error('asset deletion failed');
1069+
const uploadReleaseAsset = vi.fn();
1070+
const releaser = createReleaser({
1071+
deleteReleaseAsset: vi.fn().mockRejectedValue(deletionError),
1072+
uploadReleaseAsset,
1073+
});
1074+
1075+
await expect(
1076+
upload(
1077+
config,
1078+
releaser,
1079+
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
1080+
'__tests__/release.txt',
1081+
[{ id: 99, name: 'release.txt' }],
1082+
1,
1083+
),
1084+
).rejects.toBe(deletionError);
1085+
expect(uploadReleaseAsset).not.toHaveBeenCalled();
1086+
});
1087+
1088+
it('skips deletion and upload when overwrite_files is false', async () => {
1089+
const deleteReleaseAsset = vi.fn();
1090+
const uploadReleaseAsset = vi.fn();
1091+
const releaser = createReleaser({ deleteReleaseAsset, uploadReleaseAsset });
1092+
1093+
await expect(
1094+
upload(
1095+
{ ...config, input_overwrite_files: false },
1096+
releaser,
1097+
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
1098+
'__tests__/release.txt',
1099+
[{ id: 99, name: 'release.txt' }],
1100+
1,
1101+
),
1102+
).resolves.toBeNull();
1103+
expect(deleteReleaseAsset).not.toHaveBeenCalled();
1104+
expect(uploadReleaseAsset).not.toHaveBeenCalled();
1105+
});
1106+
9641107
it('surfaces an actionable immutable-release error for prerelease uploads', async () => {
9651108
const tempDir = mkdtempSync(join(tmpdir(), 'gh-release-immutable-'));
9661109
const assetPath = join(tempDir, 'draft-false.txt');
@@ -1000,6 +1143,7 @@ describe('github', () => {
10001143
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
10011144
assetPath,
10021145
[],
1146+
1,
10031147
),
10041148
).rejects.toThrow(
10051149
'Cannot upload asset draft-false.txt to an immutable release. GitHub only allows asset uploads before a release is published, but draft prereleases publish with the release.published event instead of release.prereleased.',
@@ -1054,6 +1198,7 @@ describe('github', () => {
10541198
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
10551199
dotfilePath,
10561200
[],
1201+
1,
10571202
);
10581203

10591204
expect(result).toStrictEqual({ id: 123, name: 'default.config', label: '.config' });
@@ -1065,6 +1210,7 @@ describe('github', () => {
10651210
expect(deleteReleaseAsset).toHaveBeenCalledWith({
10661211
owner: 'owner',
10671212
repo: 'repo',
1213+
release_id: 1,
10681214
asset_id: 99,
10691215
});
10701216
expect(updateReleaseAsset).toHaveBeenCalledWith({
@@ -1512,6 +1658,7 @@ describe('github', () => {
15121658
'https://uploads.example.test/assets',
15131659
dotfilePath,
15141660
[],
1661+
1,
15151662
);
15161663

15171664
expect(updateReleaseAssetSpy).toHaveBeenCalledWith({
@@ -1583,6 +1730,7 @@ describe('github', () => {
15831730
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
15841731
dotfilePath,
15851732
[],
1733+
1,
15861734
);
15871735

15881736
expect(updateReleaseAssetSpy).toHaveBeenNthCalledWith(1, {
@@ -1653,6 +1801,7 @@ describe('github', () => {
16531801
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
16541802
dotfilePath,
16551803
[],
1804+
1,
16561805
);
16571806

16581807
expect(listReleaseAssetsSpy).toHaveBeenCalledWith({
@@ -1712,6 +1861,7 @@ describe('github', () => {
17121861
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
17131862
dotfilePath,
17141863
[],
1864+
1,
17151865
);
17161866

17171867
expect(listReleaseAssetsSpy).toHaveBeenCalledWith({
@@ -1772,6 +1922,7 @@ describe('github', () => {
17721922
'https://uploads.github.com/repos/owner/repo/releases/1/assets',
17731923
dotfilePath,
17741924
[],
1925+
1,
17751926
);
17761927

17771928
await vi.waitFor(() => expect(listReleaseAssetsSpy).toHaveBeenCalledTimes(1));
@@ -1827,18 +1978,26 @@ describe('github', () => {
18271978
};
18281979

18291980
try {
1830-
await upload(config, releaser, 'https://uploads.example.test/assets', dotfilePath, [
1831-
{
1832-
id: 1,
1833-
name: 'default.config',
1834-
label: '.config',
1835-
},
1836-
]);
1981+
await upload(
1982+
config,
1983+
releaser,
1984+
'https://uploads.example.test/assets',
1985+
dotfilePath,
1986+
[
1987+
{
1988+
id: 1,
1989+
name: 'default.config',
1990+
label: '.config',
1991+
},
1992+
],
1993+
1,
1994+
);
18371995

18381996
expect(deleteReleaseAssetSpy).toHaveBeenCalledWith({
18391997
asset_id: 1,
18401998
owner: 'owner',
18411999
repo: 'repo',
2000+
release_id: 1,
18422001
});
18432002
} finally {
18442003
rmSync(tempDir, { recursive: true, force: true });

‎__tests__/run.test.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ describe('run', () => {
211211
'https://uploads.example.test/releases/41/assets',
212212
'fixture/dist/action.zip',
213213
initialRelease.assets,
214+
initialRelease.id,
214215
);
215216
});
216217

0 commit comments

Comments
 (0)