Repository navigation
Add organization issue types #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5f3fef4
2ca62e2
eb588e1
e1a44ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| import { beforeEach, describe, expect, it } from "vitest"; | ||
| import { | ||
| Hono, | ||
| Store, | ||
| WebhookDispatcher, | ||
| authMiddleware, | ||
| createApiErrorHandler, | ||
| createErrorHandler, | ||
| type TokenMap, | ||
| } from "@emulators/core"; | ||
| import { githubPlugin, seedFromConfig } from "../index.js"; | ||
|
|
||
| const base = "http://localhost:4000"; | ||
|
|
||
| type IssueType = { id: number; name: string; is_enabled: boolean; description: string | null }; | ||
| type Issue = { number: number; type: IssueType | null }; | ||
|
|
||
| function createTestApp() { | ||
| const store = new Store(); | ||
| const tokenMap: TokenMap = new Map([ | ||
| ["admin-token", { login: "admin", id: 1, scopes: ["repo", "user", "read:org"] }], | ||
| ["outsider-token", { login: "outsider", id: 2, scopes: ["repo", "user", "read:org"] }], | ||
| ]); | ||
| const app = new Hono(); | ||
| app.onError(createApiErrorHandler()); | ||
| app.use("*", createErrorHandler()); | ||
| app.use("*", authMiddleware(tokenMap)); | ||
| githubPlugin.register(app as any, store, new WebhookDispatcher(), base, tokenMap); | ||
| githubPlugin.seed?.(store, base); | ||
| seedFromConfig(store, base, { | ||
| users: [{ login: "admin" }, { login: "outsider" }], | ||
| orgs: [ | ||
| { login: "acme", members: [{ login: "admin", role: "admin" }] }, | ||
| { | ||
| login: "custom", | ||
| members: [{ login: "admin", role: "admin" }], | ||
| issue_types: [ | ||
| { name: "Epic", description: "A large body of work" }, | ||
| { name: "Chore", is_enabled: false }, | ||
| ], | ||
| }, | ||
| { login: "untyped", members: [{ login: "admin", role: "admin" }], issue_types: [] }, | ||
| ], | ||
| repos: [ | ||
| { owner: "acme", name: "widget" }, | ||
| { owner: "custom", name: "tool" }, | ||
| { owner: "admin", name: "personal" }, | ||
| ], | ||
| }); | ||
| return app; | ||
| } | ||
|
|
||
| describe("GitHub issue types", () => { | ||
| let app: Hono; | ||
| const call = (path: string, init: { method?: string; token?: string | null; body?: unknown } = {}) => { | ||
| const headers: Record<string, string> = {}; | ||
| const token = init.token === undefined ? "admin-token" : init.token; | ||
| if (token) headers.Authorization = `Bearer ${token}`; | ||
| if (init.body !== undefined) headers["Content-Type"] = "application/json"; | ||
| return app.request(`${base}${path}`, { | ||
| method: init.method ?? "GET", | ||
| headers, | ||
| body: init.body === undefined ? undefined : JSON.stringify(init.body), | ||
| }); | ||
| }; | ||
| const names = async (res: Response) => ((await res.json()) as IssueType[]).map((type) => type.name); | ||
|
|
||
| beforeEach(() => { | ||
| app = createTestApp(); | ||
| }); | ||
|
|
||
| it("seeds GitHub's default types, custom types or none per organization", async () => { | ||
| expect(await names(await call("/orgs/acme/issue-types"))).toEqual(["Task", "Bug", "Feature"]); | ||
| const custom = (await (await call("/orgs/custom/issue-types")).json()) as IssueType[]; | ||
| expect(custom.map((type) => [type.name, type.is_enabled, type.description])).toEqual([ | ||
| ["Epic", true, "A large body of work"], | ||
| ["Chore", false, null], | ||
| ]); | ||
| expect(await names(await call("/orgs/untyped/issue-types"))).toEqual([]); | ||
| }); | ||
|
|
||
| it("lists a repository's types from its organization, and none for a personal repository", async () => { | ||
| expect(await names(await call("/repos/acme/widget/issue-types"))).toEqual(["Task", "Bug", "Feature"]); | ||
| expect(await names(await call("/repos/admin/personal/issue-types"))).toEqual([]); | ||
| }); | ||
|
|
||
| it("hides an organization's types from non-members", async () => { | ||
| expect((await call("/orgs/acme/issue-types", { token: "outsider-token" })).status).toBe(404); | ||
| expect((await call("/orgs/acme/issue-types", { token: null })).status).toBe(404); | ||
| expect((await call("/orgs/missing/issue-types")).status).toBe(404); | ||
| }); | ||
|
|
||
| it("sets, changes and clears an issue's type by name", async () => { | ||
| const created = await call("/repos/acme/widget/issues", { method: "POST", body: { title: "Typed", type: "bug" } }); | ||
| expect(created.status).toBe(201); | ||
| const issue = (await created.json()) as Issue; | ||
| expect(issue.type?.name).toBe("Bug"); | ||
|
|
||
| const changed = await call(`/repos/acme/widget/issues/${issue.number}`, { | ||
| method: "PATCH", | ||
| body: { type: "Feature" }, | ||
| }); | ||
| expect(((await changed.json()) as Issue).type?.name).toBe("Feature"); | ||
| const untouched = await call(`/repos/acme/widget/issues/${issue.number}`, { | ||
| method: "PATCH", | ||
| body: { title: "Renamed" }, | ||
| }); | ||
| expect(((await untouched.json()) as Issue).type?.name).toBe("Feature"); | ||
| const cleared = await call(`/repos/acme/widget/issues/${issue.number}`, { method: "PATCH", body: { type: null } }); | ||
| expect(((await cleared.json()) as Issue).type).toBeNull(); | ||
|
|
||
| const plain = await call("/repos/acme/widget/issues", { method: "POST", body: { title: "Untyped" } }); | ||
| expect(((await plain.json()) as Issue).type).toBeNull(); | ||
| }); | ||
|
|
||
| it("rejects an unknown, disabled or personal-repository type with 422 and writes nothing", async () => { | ||
| const before = ((await (await call("/repos/custom/tool/issues?state=all")).json()) as unknown[]).length; | ||
| for (const type of ["Bug", "Chore", ""]) { | ||
| const res = await call("/repos/custom/tool/issues", { method: "POST", body: { title: "Rejected", type } }); | ||
| expect(res.status, type).toBe(422); | ||
| } | ||
| expect(((await (await call("/repos/custom/tool/issues?state=all")).json()) as unknown[]).length).toBe(before); | ||
| expect( | ||
| (await call("/repos/admin/personal/issues", { method: "POST", body: { title: "Personal", type: "Task" } })) | ||
| .status, | ||
| ).toBe(422); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { ApiError } from "@emulators/core"; | ||
| import type { GitHubIssueType, GitHubOrg, GitHubRepo } from "./entities.js"; | ||
| import type { GitHubStore } from "./store.js"; | ||
| import { generateNodeId } from "./helpers.js"; | ||
|
|
||
| export interface IssueTypeSeed { | ||
| name: string; | ||
| description?: string | null; | ||
| color?: string | null; | ||
| is_enabled?: boolean; | ||
| } | ||
|
|
||
| /** The issue types GitHub creates for a new organization. */ | ||
| export const DEFAULT_ISSUE_TYPES: IssueTypeSeed[] = [ | ||
| { name: "Task", description: "A specific piece of work", color: "yellow" }, | ||
| { name: "Bug", description: "An unexpected problem or behavior", color: "red" }, | ||
| { name: "Feature", description: "A request, idea, or new functionality", color: "blue" }, | ||
| ]; | ||
|
|
||
| export function seedIssueTypes(gh: GitHubStore, org: GitHubOrg, types?: IssueTypeSeed[] | null): void { | ||
| for (const type of types ?? DEFAULT_ISSUE_TYPES) { | ||
| if (!type.name?.trim()) throw new Error(`Issue type for ${org.login} needs a name`); | ||
| if (orgIssueTypes(gh, org.id).some((existing) => existing.name.toLowerCase() === type.name.toLowerCase())) continue; | ||
| const row = gh.issueTypes.insert({ | ||
| node_id: "", | ||
| org_id: org.id, | ||
| name: type.name.trim(), | ||
| description: type.description ?? null, | ||
| color: type.color ?? null, | ||
| is_enabled: type.is_enabled ?? true, | ||
| } as Omit<GitHubIssueType, "id" | "created_at" | "updated_at">); | ||
| gh.issueTypes.update(row.id, { node_id: generateNodeId("IssueType", row.id) }); | ||
| } | ||
| } | ||
|
|
||
| export function orgIssueTypes(gh: GitHubStore, orgId: number): GitHubIssueType[] { | ||
| return gh.issueTypes.findBy("org_id", orgId); | ||
| } | ||
|
|
||
| /** Issue types belong to organizations; a personal repository has none. */ | ||
| export function repoIssueTypes(gh: GitHubStore, repo: GitHubRepo): GitHubIssueType[] { | ||
| return repo.owner_type === "Organization" ? orgIssueTypes(gh, repo.owner_id) : []; | ||
| } | ||
|
|
||
| /** | ||
| * Resolves the `type` field of an issue write: `null` or an absent field clears it, and a name selects an | ||
| * enabled type of the repository's organization, matched without regard to case. | ||
| */ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ⚪ resolveIssueTypeId undefined branch is unreachable. 🤖 Fix with your agentWhy this mattersThe documented tri-state contract ("undefined leaves the type unchanged") never takes effect: the PATCH call site is guarded by /**
* Resolves the `type` field of an issue write. `undefined` leaves the type unchanged, `null` clears it,
* and a name selects an enabled type of the repository's organization, matched without regard to case.
*/
export function resolveIssueTypeId(gh: GitHubStore, repo: GitHubRepo, raw: unknown): number | null | undefined {
if (raw === undefined) return undefined;
if (raw === null) return null;
if (typeof raw !== "string" || !raw.trim()) throw new ApiError(422, "Validation failed"); |
||
| export function resolveIssueTypeId(gh: GitHubStore, repo: GitHubRepo, raw: unknown): number | null { | ||
| if (raw === undefined || raw === null) return null; | ||
| if (typeof raw !== "string" || !raw.trim()) throw new ApiError(422, "Validation failed"); | ||
| const type = repoIssueTypes(gh, repo).find( | ||
| (candidate) => candidate.is_enabled && candidate.name.toLowerCase() === raw.trim().toLowerCase(), | ||
| ); | ||
| if (!type) throw new ApiError(422, "Validation failed"); | ||
| return type.id; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 issueTypeJson duplicates formatIssueType serializer.
🤖 Fix with your agent
Why this matters
The private issueTypeJson helper in helpers.ts builds the exact same 8-field object ({id, node_id, name, description, color, created_at, updated_at, is_enabled}) as formatIssueType in issue-types.ts, and breaks the file's format* naming convention for serializers (formatLabel, formatMilestone, ...). Two copies of the issue-type API shape in one PR will drift when a field is added or renamed. helpers.ts cannot import issue-types.ts (issue-types.ts already imports generateNodeId from helpers.js), so the shared formatter belongs in helpers.ts.