Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion intake/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ export function github(repo: string, token: string): Issues {
},
body: init.body ? JSON.stringify(init.body) : undefined,
});
if (res.status === 404 || res.status === 410) throw new Gone();
if (!res.ok) throw new Error(`GitHub ${init.method ?? 'GET'} ${path}: ${res.status} ${await res.text()}`);
return res.json() as Promise<T>;
}
// an issue deleted by hand answers 410 (404 if the repo can't see it any more)
class Gone extends Error {}
type Issue = { number: number; body: string | null };
return {
async find(text) {
Expand All @@ -23,7 +26,9 @@ export function github(repo: string, token: string): Issues {
async create(title, body, labels) {
return (await call<Issue>('/issues', { method: 'POST', body: { title, body, labels } })).number;
},
async read(n) { return (await call<Issue>(`/issues/${n}`)).body ?? ''; },
async read(n) {
try { return (await call<Issue>(`/issues/${n}`)).body ?? ''; } catch (e) { if (e instanceof Gone) return null; throw e; }
},
async edit(n, body) { await call(`/issues/${n}`, { method: 'PATCH', body: { body } }); },
};
}
6 changes: 4 additions & 2 deletions intake/src/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export interface Issues {
/** An issue whose text holds `text`, among the newest ones. */
find(text: string): Promise<number | null>;
create(title: string, body: string, labels: string[]): Promise<number>;
read(n: number): Promise<string>;
/** The issue's text, or null if it was deleted. */
read(n: number): Promise<string | null>;
edit(n: number, body: string): Promise<void>;
}
export type FileName = 'shot.jpg' | 'ui.jpg' | 'save.json';
Expand Down Expand Up @@ -63,7 +64,8 @@ export async function run(store: Store, issues: Issues, files: Files, now = Date

// a week on: the issue keeps the note, and says the picture and the save are gone
for (const old of await store.expired(now - KEEP_MS)) {
if (old.issue) await issues.edit(old.issue, expired(await issues.read(old.issue)));
const text = old.issue ? await issues.read(old.issue) : null;
if (old.issue && text !== null) await issues.edit(old.issue, expired(text));
await store.remove(old.id);
done.expired++;
}
Expand Down
9 changes: 8 additions & 1 deletion test/intake.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function services(reports: StoredReport[], old: { id: string; issue?: number }[]
const issues: Issues = {
async find(text) { return [...existing].find(([, b]) => b.includes(text))?.[0] ?? null; },
async create(t, b, labels) { made.push({ title: t, body: b, labels }); existing.set(100 + made.length, b); return 100 + made.length; },
async read(n) { return existing.get(n) ?? ''; },
async read(n) { return existing.get(n) ?? null; },
async edit(n, b) { edits.set(n, b); },
};
const put: string[] = [], log: string[] = [];
Expand Down Expand Up @@ -100,6 +100,13 @@ describe('the intake', () => {
expect(s.edits.get(3)).toBe('_Picture deleted after 7 days._');
expect(s.removed).toEqual(['old', 'lost']);
});

it('a week on, still deletes the report when its issue was deleted by hand', async () => {
const s = services([], [{ id: 'gone', issue: 2 }, { id: 'old', issue: 3 }]);
expect((await run(s.store, s.issues, s.files, NOW)).expired).toBe(2);
expect([...s.edits.keys()]).toEqual([3]);
expect(s.removed).toEqual(['gone', 'old']);
});
});

describe("a player's words in an issue", () => {
Expand Down
Loading