Skip to content

Commit f6b913c

Browse files
authored
feat: improve release error reporting and test coverage (#813)
Signed-off-by: Rui Chen <[email protected]>
1 parent 15f193d commit f6b913c

11 files changed

Lines changed: 623 additions & 144 deletions

File tree

‎AGENTS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Optimize for stability, reproducibility, and clear user value over broad rewrite
1515

1616
## Current Architecture
1717

18-
- `src/main.ts` is the orchestration layer: parse config, validate inputs, create/update release, upload assets, finalize, set outputs.
18+
- `src/main.ts` is the action bootstrap; `src/run.ts` orchestrates config parsing, validation, release creation/update, asset uploads, finalization, and outputs.
1919
- `src/github.ts` owns release semantics: lookup, create/update/finalize, asset upload, race handling, and GitHub API interaction.
2020
- `src/util.ts` owns parsing and path normalization.
2121
- Keep behavior-specific logic in `src/github.ts` or `src/util.ts`; avoid growing `src/main.ts` with ad-hoc feature branches.

‎__tests__/github.test.ts‎

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
findTagFromReleases,
44
finalizeRelease,
55
GitHubReleaser,
6+
listReleaseAssets,
67
mimeOrDefault,
78
release,
89
Release,
@@ -13,9 +14,33 @@ import {
1314
import { mkdtempSync, rmSync, writeFileSync } from 'fs';
1415
import { tmpdir } from 'os';
1516
import { join } from 'path';
16-
import { assert, describe, expect, it, vi } from 'vitest';
17+
import { afterEach, assert, describe, expect, it, vi } from 'vitest';
18+
19+
const unexpected = (method: string) =>
20+
vi.fn().mockRejectedValue(new Error(`Unexpected ${method} call`));
21+
22+
const createReleaser = (overrides: Partial<Releaser> = {}): Releaser => ({
23+
getReleaseByTag: unexpected('getReleaseByTag'),
24+
createRelease: unexpected('createRelease'),
25+
updateRelease: unexpected('updateRelease'),
26+
finalizeRelease: unexpected('finalizeRelease'),
27+
allReleases: async function* () {
28+
throw new Error('Unexpected allReleases call');
29+
},
30+
listReleaseAssets: unexpected('listReleaseAssets'),
31+
deleteReleaseAsset: unexpected('deleteReleaseAsset'),
32+
deleteRelease: unexpected('deleteRelease'),
33+
updateReleaseAsset: unexpected('updateReleaseAsset'),
34+
uploadReleaseAsset: unexpected('uploadReleaseAsset'),
35+
...overrides,
36+
});
1737

1838
describe('github', () => {
39+
afterEach(() => {
40+
vi.useRealTimers();
41+
vi.restoreAllMocks();
42+
});
43+
1944
const config = {
2045
github_token: 'test-token',
2146
github_ref: 'refs/tags/v1.0.0',
@@ -148,6 +173,56 @@ describe('github', () => {
148173
});
149174
});
150175

176+
describe('listReleaseAssets', () => {
177+
const releaseToInspect: Release = {
178+
id: 42,
179+
upload_url: 'https://uploads.example.test/releases/42/assets',
180+
html_url: 'https://example.test/releases/42',
181+
tag_name: 'v1.0.0',
182+
name: 'v1.0.0',
183+
target_commitish: 'main',
184+
draft: false,
185+
prerelease: false,
186+
assets: [],
187+
};
188+
189+
it('returns assets with the repository and release parameters', async () => {
190+
const assets = [{ id: 7, name: 'action.zip' }];
191+
const listReleaseAssetsSpy = vi.fn().mockResolvedValue(assets);
192+
const releaser = createReleaser({ listReleaseAssets: listReleaseAssetsSpy });
193+
194+
await expect(listReleaseAssets(config, releaser, releaseToInspect)).resolves.toBe(assets);
195+
expect(listReleaseAssetsSpy).toHaveBeenCalledOnce();
196+
expect(listReleaseAssetsSpy).toHaveBeenCalledWith({
197+
owner: 'owner',
198+
repo: 'repo',
199+
release_id: 42,
200+
});
201+
});
202+
203+
it('retries a transient failure and then succeeds', async () => {
204+
const assets = [{ id: 7, name: 'action.zip' }];
205+
const listReleaseAssetsSpy = vi
206+
.fn()
207+
.mockRejectedValueOnce(new Error('temporary failure'))
208+
.mockResolvedValueOnce(assets);
209+
const releaser = createReleaser({ listReleaseAssets: listReleaseAssetsSpy });
210+
211+
await expect(listReleaseAssets(config, releaser, releaseToInspect, 2)).resolves.toBe(assets);
212+
expect(listReleaseAssetsSpy).toHaveBeenCalledTimes(2);
213+
});
214+
215+
it('stops after exhausting the configured attempts', async () => {
216+
const listReleaseAssetsSpy = vi.fn().mockRejectedValue(new Error('persistent failure'));
217+
const releaser = createReleaser({ listReleaseAssets: listReleaseAssetsSpy });
218+
219+
await expect(listReleaseAssets(config, releaser, releaseToInspect, 2)).rejects.toThrow(
220+
'Too many retries.',
221+
);
222+
expect(listReleaseAssetsSpy).toHaveBeenCalledTimes(2);
223+
});
224+
});
225+
151226
describe('GitHubReleaser', () => {
152227
it('passes previous_tag_name to generateReleaseNotes and strips it from createRelease', async () => {
153228
const generateReleaseNotes = vi.fn(async () => ({
@@ -519,6 +594,23 @@ describe('github', () => {
519594
});
520595

521596
describe('error handling', () => {
597+
it('reports a useful create error without assuming response data exists', async () => {
598+
const releaseError = {
599+
status: 403,
600+
message: 'Resource not accessible by integration',
601+
};
602+
const log = vi.spyOn(console, 'log').mockImplementation(() => undefined);
603+
const releaser = createReleaser({
604+
getReleaseByTag: vi.fn().mockRejectedValue({ status: 404 }),
605+
createRelease: vi.fn().mockRejectedValue(releaseError),
606+
});
607+
608+
await expect(release(config, releaser, 1)).rejects.toBe(releaseError);
609+
expect(log).toHaveBeenCalledWith('⚠️ GitHub release failed with status: 403');
610+
expect(log).toHaveBeenCalledWith('Resource not accessible by integration');
611+
expect(log).not.toHaveBeenCalledWith('undefined');
612+
});
613+
522614
it('passes previous_tag_name through when creating a release with generated notes', async () => {
523615
const createReleaseSpy = vi.fn(async () => ({
524616
data: {
@@ -1378,6 +1470,7 @@ describe('github', () => {
13781470
});
13791471

13801472
it('polls for a matching asset after update-a-release-asset 404 before failing', async () => {
1473+
vi.useFakeTimers();
13811474
const tempDir = mkdtempSync(join(tmpdir(), 'gh-release-dotfile-'));
13821475
const dotfilePath = join(tempDir, '.config');
13831476
writeFileSync(dotfilePath, 'config');
@@ -1421,7 +1514,8 @@ describe('github', () => {
14211514
[],
14221515
);
14231516

1424-
await new Promise((resolve) => setTimeout(resolve, 1100));
1517+
await vi.waitFor(() => expect(listReleaseAssetsSpy).toHaveBeenCalledTimes(1));
1518+
await vi.advanceTimersByTimeAsync(1000);
14251519

14261520
const result = await resultPromise;
14271521

‎__tests__/main.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { expect, it, vi } from 'vitest';
2+
3+
const run = vi.hoisted(() => vi.fn().mockResolvedValue(undefined));
4+
5+
vi.mock('../src/run', () => ({ run }));
6+
7+
it('starts the action orchestration', async () => {
8+
await import('../src/main');
9+
10+
expect(run).toHaveBeenCalledOnce();
11+
});

0 commit comments

Comments
 (0)