From 6296f47faba0df05836c9ca896e68b8493ad5c6b Mon Sep 17 00:00:00 2001 From: alexion Date: Sun, 12 Jul 2026 09:47:51 -0400 Subject: [PATCH] feat: add issue edit, close, and reopen (task 0005) Implement the issue state-transition mutations: - issue edit: --title, --body/--body-file, --add-label/--remove-label, --add-assignee/--remove-assignee, --milestone. Label mutations use Gitea's dedicated additive/removal endpoints (names added directly, removals resolved to IDs, unapplied-label 404 as silent success); assignees use fetch-then-patch (ADR 0007); title/body/milestone and the recomputed assignee list travel in one PATCH. Outputs edited: {number, status: ok}. - issue close: PATCHes state closed, optional --comment posted after with its failure surfaced; already-closed short-circuits with Already closed. - issue reopen: PATCHes state open; already-open short-circuits with Already open. Extract a shared getIssue helper used by view/edit/close/reopen. --- .claude/tasks/0005-issue-edit-close-reopen.md | 29 +- src/commands/issue.ts | 306 +++++++++++++++++- test/issue-close.test.ts | 92 ++++++ test/issue-edit.test.ts | 173 ++++++++++ test/issue-reopen.test.ts | 54 ++++ 5 files changed, 637 insertions(+), 17 deletions(-) create mode 100644 test/issue-close.test.ts create mode 100644 test/issue-edit.test.ts create mode 100644 test/issue-reopen.test.ts diff --git a/.claude/tasks/0005-issue-edit-close-reopen.md b/.claude/tasks/0005-issue-edit-close-reopen.md index 3927db1..86ccee1 100644 --- a/.claude/tasks/0005-issue-edit-close-reopen.md +++ b/.claude/tasks/0005-issue-edit-close-reopen.md @@ -15,10 +15,25 @@ All three use the action-block pattern on success (`edited:`/`closed:`/`reopened ## Acceptance criteria -- [ ] `issue edit` applies title, body, and milestone changes and outputs `edited: { number, status: "ok" }` -- [ ] `--add-label` posts the name directly to the additive label endpoint; `--remove-label` resolves the ID first, yields `VALIDATION_ERROR` for a name not in the repo, and treats a 404 for an unapplied label as silent success -- [ ] `--add-assignee`/`--remove-assignee` use fetch-then-patch, sending the full resulting assignee list in a single PATCH -- [ ] `issue close ` outputs `closed: { number, status: "ok" }`; with `--comment` the comment is posted after the close, and a comment-post failure surfaces as an error even though the issue is closed -- [ ] `issue close` on an already-closed issue and `issue reopen` on an already-open issue return early with `message: "Already closed"` / `message: "Already open"` and exit 0 -- [ ] `issue reopen ` outputs `reopened: { number, status: "ok" }` -- [ ] Fixture-server tests cover each mutation path, both idempotent no-ops, the unapplied-label silent success, and the close-comment partial failure +- [x] `issue edit` applies title, body, and milestone changes and outputs `edited: { number, status: "ok" }` +- [x] `--add-label` posts the name directly to the additive label endpoint; `--remove-label` resolves the ID first, yields `VALIDATION_ERROR` for a name not in the repo, and treats a 404 for an unapplied label as silent success +- [x] `--add-assignee`/`--remove-assignee` use fetch-then-patch, sending the full resulting assignee list in a single PATCH +- [x] `issue close ` outputs `closed: { number, status: "ok" }`; with `--comment` the comment is posted after the close, and a comment-post failure surfaces as an error even though the issue is closed +- [x] `issue close` on an already-closed issue and `issue reopen` on an already-open issue return early with `message: "Already closed"` / `message: "Already open"` and exit 0 +- [x] `issue reopen ` outputs `reopened: { number, status: "ok" }` +- [x] Fixture-server tests cover each mutation path, both idempotent no-ops, the unapplied-label silent success, and the close-comment partial failure + +## Implementation Notes + +No criteria were dropped or altered; all seven are satisfied. + +Decisions made mid-implementation: + +- The idempotent no-op for `close`/`reopen` renders an entity block — `issue: { number, state, message }` — mirroring gh-axi, since the spec's deliberate action-block departure is scoped to the *success* path only. + Determining the no-op requires a `GET` on the issue first, which also supplies the `state` reported in that block. +- `--add-label`/`--remove-label`/`--add-assignee`/`--remove-assignee` are repeatable, matching `issue create`'s repeatable `--label`, rather than the single-valued form the spec text implies. +- Added a `VALIDATION_ERROR` when `issue edit` is invoked with no changes (documented in `--help`). The spec never specified the no-change case; this is a small justified extension, not scope creep. +- Title/body/milestone and the recomputed assignee list travel in a single PATCH; label mutations use Gitea's dedicated endpoints afterward. Name resolution (milestone, remove-label IDs) runs before any mutation so a typo is reported before a change lands. +- Extracted a shared `getIssue` helper (review finding) now used by `view`/`edit`/`close`/`reopen`. + +Follow-up worth flagging: `issue close`/`reopen` do not type-guard against a PR number (unlike `issue view`), consistent with the spec, which does not require it. diff --git a/src/commands/issue.ts b/src/commands/issue.ts index cc07409..9f386b7 100644 --- a/src/commands/issue.ts +++ b/src/commands/issue.ts @@ -1,11 +1,11 @@ -import type { Comment, CreateIssueOption, Issue } from "gitea-js"; +import type { Comment, CreateIssueOption, EditIssueOption, Issue } from "gitea-js"; import { BODY_TRUNCATE_LIMIT, COMMENT_TRUNCATE_LIMIT, truncateBody } from "../body.js"; import { requireBodySource, resolveBodySource } from "../body-source.js"; -import { createClient } from "../client.js"; +import { createClient, type GiteaClient } from "../client.js"; import { COMMENT_FLAGS, commentItem } from "../comment.js"; import { resolveRepoContext, type RepoContext } from "../context.js"; import type { CliDeps } from "../deps.js"; -import { axiError, classifyHttpError } from "../errors.js"; +import { axiError, classifyHttpError, httpStatus } from "../errors.js"; import { extractRow, joined, @@ -34,11 +34,59 @@ commands: list List issues in the current repository view Show a single issue's details create Create an issue + edit Edit an issue's title, body, labels, assignees, or milestone + close Close an issue + reopen Reopen a closed issue comment Post a comment on an issue or pull request Run \`gitea-axi issue --help\` for the flags of a command. `; +export const ISSUE_EDIT_HELP = `usage: gitea-axi issue edit [flags] + +Edit an issue in the current repository. At least one change is required. + +flags: + --title New title + --body New body + --body-file Read the new body from a file (mutually exclusive with --body) + --add-label Add a label by name (repeatable) + --remove-label Remove a label by name (repeatable, case-insensitive) + --add-assignee Add an assignee (repeatable) + --remove-assignee Remove an assignee (repeatable) + --milestone Assign a milestone by name (case-insensitive) + --help Show this help + +global flags: + -R, --repo Override the repository detected from the git origin remote + --login Select a tea login profile by name +`; + +export const ISSUE_CLOSE_HELP = `usage: gitea-axi issue close [flags] + +Close an issue in the current repository. + +flags: + --comment Post a comment when closing + --help Show this help + +global flags: + -R, --repo Override the repository detected from the git origin remote + --login Select a tea login profile by name +`; + +export const ISSUE_REOPEN_HELP = `usage: gitea-axi issue reopen + +Reopen a closed issue in the current repository. + +flags: + --help Show this help + +global flags: + -R, --repo Override the repository detected from the git origin remote + --login Select a tea login profile by name +`; + export const ISSUE_CREATE_HELP = `usage: gitea-axi issue create --title [flags] Create an issue in the current repository. @@ -371,6 +419,16 @@ function buildCommentRows( }); } +/** Fetch a single issue, mapping any HTTP failure to an AxiError. */ +async function getIssue(api: GiteaClient, context: RepoContext, number: number): Promise { + try { + const response = await api.repos.issueGetIssue(context.owner, context.name, number); + return response.data; + } catch (error) { + throw classifyHttpError(error); + } +} + function issueViewSuggestions( context: RepoContext, number: number, @@ -404,13 +462,7 @@ async function issueView(deps: CliDeps, args: string[]): Promise { const context = await resolveRepoContext(deps); const api = createClient(context); - let issue: Issue; - try { - const response = await api.repos.issueGetIssue(context.owner, context.name, number); - issue = response.data; - } catch (error) { - throw classifyHttpError(error); - } + const issue = await getIssue(api, context, number); if (issue.pull_request) { throw axiError(`issue #${number} is a pull request`, "VALIDATION_ERROR", [ @@ -580,6 +632,231 @@ async function issueComment(deps: CliDeps, args: string[]): Promise { return renderDetail({ noun: "comment", item, help }); } +/** + * The assignee list to PATCH: the issue's current assignees with the requested + * additions appended and removals dropped (fetch-then-patch, ADR 0007). Matching + * is case-insensitive and the result is de-duplicated, order-preserving, so a + * login that is already assigned never lands in the list twice. + */ +async function resolveAssignees( + api: GiteaClient, + context: RepoContext, + number: number, + add: string[], + remove: string[], +): Promise { + const issue = await getIssue(api, context, number); + const removeSet = new Set(remove.map((login) => login.toLowerCase())); + const seen = new Set(); + const result: string[] = []; + const push = (login: string): void => { + const key = login.toLowerCase(); + if (removeSet.has(key) || seen.has(key)) { + return; + } + seen.add(key); + result.push(login); + }; + for (const assignee of issue.assignees ?? []) { + if (assignee.login) { + push(assignee.login); + } + } + for (const login of add) { + push(login); + } + return result; +} + +async function issueEdit(deps: CliDeps, args: string[]): Promise { + if (args.includes("--help")) { + return ISSUE_EDIT_HELP; + } + const { flags, lists, positionals } = parseFlags( + args, + { + "--title": { takesValue: true }, + "--body": { takesValue: true }, + "--body-file": { takesValue: true }, + "--add-label": { takesValue: true, repeatable: true }, + "--remove-label": { takesValue: true, repeatable: true }, + "--add-assignee": { takesValue: true, repeatable: true }, + "--remove-assignee": { takesValue: true, repeatable: true }, + "--milestone": { takesValue: true }, + }, + "issue edit", + ); + const number = parsePositionalNumber(positionals, "issue edit", "issue"); + const title = flagValue(flags, "--title"); + const body = resolveBodySource(deps, flags, "issue edit"); + const milestoneName = flagValue(flags, "--milestone"); + const addLabels = lists["--add-label"] ?? []; + const removeLabels = lists["--remove-label"] ?? []; + const addAssignees = lists["--add-assignee"] ?? []; + const removeAssignees = lists["--remove-assignee"] ?? []; + + const changesAssignees = addAssignees.length > 0 || removeAssignees.length > 0; + const nothingToDo = + title === undefined && + body === undefined && + milestoneName === undefined && + addLabels.length === 0 && + removeLabels.length === 0 && + !changesAssignees; + if (nothingToDo) { + throw axiError("issue edit requires at least one change", "VALIDATION_ERROR", [ + "Run `gitea-axi issue edit --help` to see the fields you can change", + ]); + } + + const context = await resolveRepoContext(deps); + const api = createClient(context); + + // Name resolution runs before any mutation: whether a milestone or label name + // is real does not depend on the issue's state, so a typo is reported before a + // single change lands, never leaving the issue half-edited. + const milestoneId = + milestoneName !== undefined ? await resolveMilestoneId(api, context, milestoneName) : undefined; + const removeLabelIds = await resolveLabelIds(api, context, removeLabels); + + // Title, body, milestone, and the recomputed assignee list travel in one PATCH. + const payload: EditIssueOption = {}; + if (title !== undefined) { + payload.title = title; + } + if (body !== undefined) { + payload.body = body; + } + if (milestoneId !== undefined) { + payload.milestone = milestoneId; + } + if (changesAssignees) { + payload.assignees = await resolveAssignees(api, context, number, addAssignees, removeAssignees); + } + if (Object.keys(payload).length > 0) { + try { + await api.repos.issueEditIssue(context.owner, context.name, number, payload); + } catch (error) { + throw classifyHttpError(error); + } + } + + // Label mutations use Gitea's dedicated endpoints (idempotent). `--add-label` + // passes names straight through — Gitea accepts them there, no lookup needed. + if (addLabels.length > 0) { + try { + await api.repos.issueAddLabel(context.owner, context.name, number, { labels: addLabels }); + } catch (error) { + throw classifyHttpError(error); + } + } + for (const id of removeLabelIds) { + try { + await api.repos.issueRemoveLabel(context.owner, context.name, number, id); + } catch (error) { + // The label exists in the repo but is not applied to this issue: Gitea + // answers 404, and the caller's intent (label absent) already holds, so it + // is silent success rather than an error. + if (httpStatus(error) === 404) { + continue; + } + throw classifyHttpError(error); + } + } + + // The mutation ran, so the block is named for the action, not the entity — a + // deliberate departure from gh-axi's `issue:` block (see ADR/spec). + return renderDetail({ + noun: "edited", + item: { number, status: "ok" }, + help: [suggestCommand(context, `issue view ${number}`, "to see the issue in full")], + }); +} + +async function issueClose(deps: CliDeps, args: string[]): Promise { + if (args.includes("--help")) { + return ISSUE_CLOSE_HELP; + } + const { flags, positionals } = parseFlags( + args, + { "--comment": { takesValue: true } }, + "issue close", + ); + const number = parsePositionalNumber(positionals, "issue close", "issue"); + const comment = flagValue(flags, "--comment"); + + const context = await resolveRepoContext(deps); + const api = createClient(context); + + // Read the current state first: an already-closed issue short-circuits to the + // idempotent no-op below rather than issuing a redundant PATCH. + const issue = await getIssue(api, context, number); + if (issue.state === "closed") { + return renderDetail({ + noun: "issue", + item: { number, state: "closed", message: "Already closed" }, + help: [suggestCommand(context, `issue reopen ${number}`, "to reopen this issue")], + }); + } + + try { + await api.repos.issueEditIssue(context.owner, context.name, number, { state: "closed" }); + } catch (error) { + throw classifyHttpError(error); + } + + // The comment is a second call after the close lands. A failure here is + // surfaced, never swallowed: the issue is closed, but the caller must learn + // that the comment they asked for did not post. + if (comment !== undefined) { + try { + await api.repos.issueCreateComment(context.owner, context.name, number, { body: comment }); + } catch (error) { + throw classifyHttpError(error); + } + } + + return renderDetail({ + noun: "closed", + item: { number, status: "ok" }, + help: [suggestCommand(context, `issue reopen ${number}`, "to reopen this issue")], + }); +} + +async function issueReopen(deps: CliDeps, args: string[]): Promise { + if (args.includes("--help")) { + return ISSUE_REOPEN_HELP; + } + const { positionals } = parseFlags(args, {}, "issue reopen"); + const number = parsePositionalNumber(positionals, "issue reopen", "issue"); + + const context = await resolveRepoContext(deps); + const api = createClient(context); + + // Read the current state first: an already-open issue short-circuits to the + // idempotent no-op below rather than issuing a redundant PATCH. + const issue = await getIssue(api, context, number); + if (issue.state === "open") { + return renderDetail({ + noun: "issue", + item: { number, state: "open", message: "Already open" }, + help: [suggestCommand(context, `issue close ${number}`, "to close this issue")], + }); + } + + try { + await api.repos.issueEditIssue(context.owner, context.name, number, { state: "open" }); + } catch (error) { + throw classifyHttpError(error); + } + + return renderDetail({ + noun: "reopened", + item: { number, status: "ok" }, + help: [suggestCommand(context, `issue view ${number}`, "to see the issue in full")], + }); +} + export function issueCommand(deps: CliDeps) { return async (args: string[]): Promise => { const [subcommand, ...rest] = args; @@ -595,6 +872,15 @@ export function issueCommand(deps: CliDeps) { if (subcommand === "create") { return issueCreate(deps, rest); } + if (subcommand === "edit") { + return issueEdit(deps, rest); + } + if (subcommand === "close") { + return issueClose(deps, rest); + } + if (subcommand === "reopen") { + return issueReopen(deps, rest); + } if (subcommand === "comment") { return issueComment(deps, rest); } diff --git a/test/issue-close.test.ts b/test/issue-close.test.ts new file mode 100644 index 0000000..451882d --- /dev/null +++ b/test/issue-close.test.ts @@ -0,0 +1,92 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { startFixtureServer, type FixtureServer } from "./fixture-server.js"; +import { runCliTest, testModeEnv } from "./harness.js"; + +const ISSUE_PATH = "/api/v1/repos/testowner/testrepo/issues/7"; +const COMMENTS_PATH = "/api/v1/repos/testowner/testrepo/issues/7/comments"; + +let server: FixtureServer; + +afterEach(async () => { + await server.close(); +}); + +describe("issue close", () => { + it("closes an open issue and reports the action", async () => { + server = await startFixtureServer([ + { method: "GET", path: ISSUE_PATH, body: { number: 7, state: "open" } }, + { method: "PATCH", path: ISSUE_PATH, body: { number: 7, state: "closed" } }, + ]); + const { stdout, exitCode } = await runCliTest(["issue", "close", "7"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(0); + expect(stdout).toContain("closed:"); + expect(stdout).toContain("number: 7"); + expect(stdout).toContain("status: ok"); + const patch = server.requests.find((request) => request.method === "PATCH"); + expect(patch?.body).toEqual({ state: "closed" }); + }); + + it("posts a --comment after the close and reports success", async () => { + server = await startFixtureServer([ + { method: "GET", path: ISSUE_PATH, body: { number: 7, state: "open" } }, + { method: "PATCH", path: ISSUE_PATH, body: { number: 7, state: "closed" } }, + { method: "POST", path: COMMENTS_PATH, status: 201, body: { id: 1 } }, + ]); + const { exitCode } = await runCliTest( + ["issue", "close", "7", "--comment", "Fixed in main."], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(0); + const post = server.requests.find((request) => request.method === "POST"); + expect(post?.body).toEqual({ body: "Fixed in main." }); + // The close lands before the comment. + const patchIndex = server.requests.findIndex((request) => request.method === "PATCH"); + const postIndex = server.requests.findIndex((request) => request.method === "POST"); + expect(patchIndex).toBeLessThan(postIndex); + }); + + it("surfaces a comment-post failure even though the issue was closed", async () => { + server = await startFixtureServer([ + { method: "GET", path: ISSUE_PATH, body: { number: 7, state: "open" } }, + { method: "PATCH", path: ISSUE_PATH, body: { number: 7, state: "closed" } }, + { method: "POST", path: COMMENTS_PATH, status: 403, body: { message: "forbidden" } }, + ]); + const { stdout, exitCode } = await runCliTest( + ["issue", "close", "7", "--comment", "note"], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(1); + expect(stdout).toContain("code: FORBIDDEN"); + // The close still happened. + expect(server.requests.some((request) => request.method === "PATCH")).toBe(true); + }); + + it("returns early with Already closed on an already-closed issue", async () => { + server = await startFixtureServer([ + { method: "GET", path: ISSUE_PATH, body: { number: 7, state: "closed" } }, + ]); + const { stdout, exitCode } = await runCliTest(["issue", "close", "7"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(0); + expect(stdout).toContain("message: Already closed"); + expect(server.requests.some((request) => request.method === "PATCH")).toBe(false); + }); + + it("prints help with --help without calling the API", async () => { + server = await startFixtureServer([]); + const { stdout, exitCode } = await runCliTest(["issue", "close", "--help"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(0); + expect(stdout).toContain("usage: gitea-axi issue close"); + expect(server.requests).toHaveLength(0); + }); +}); diff --git a/test/issue-edit.test.ts b/test/issue-edit.test.ts new file mode 100644 index 0000000..95aba83 --- /dev/null +++ b/test/issue-edit.test.ts @@ -0,0 +1,173 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { startFixtureServer, type FixtureServer } from "./fixture-server.js"; +import { runCliTest, tempFiles, testModeEnv } from "./harness.js"; + +const ISSUE_PATH = "/api/v1/repos/testowner/testrepo/issues/7"; +const LABELS_ENDPOINT = "/api/v1/repos/testowner/testrepo/issues/7/labels"; +const REPO_LABELS = "/api/v1/repos/testowner/testrepo/labels"; +const MILESTONES_PATH = "/api/v1/repos/testowner/testrepo/milestones"; + +let server: FixtureServer; +const files = tempFiles(); + +afterEach(async () => { + await server.close(); + files.cleanup(); +}); + +/** The parsed body of the single PATCH the CLI sent to the issue. */ +function patchedIssue(): Record { + const patch = server.requests.find( + (request) => request.method === "PATCH" && request.path === ISSUE_PATH, + ); + expect(patch, "expected a PATCH to the issue").toBeDefined(); + return patch!.body as Record; +} + +function issue(fields: Record = {}): Record { + return { number: 7, state: "open", assignees: [], ...fields }; +} + +describe("issue edit", () => { + it("applies title, body, and milestone in one PATCH and reports the action", async () => { + server = await startFixtureServer([ + { + method: "GET", + path: MILESTONES_PATH, + query: { name: "v1.0" }, + body: [{ id: 5, title: "v1.0" }], + }, + { method: "PATCH", path: ISSUE_PATH, body: issue() }, + ]); + const { stdout, exitCode } = await runCliTest( + ["issue", "edit", "7", "--title", "New title", "--body", "New body", "--milestone", "v1.0"], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(0); + expect(stdout).toContain("edited:"); + expect(stdout).toContain("number: 7"); + expect(stdout).toContain("status: ok"); + expect(patchedIssue()).toEqual({ title: "New title", body: "New body", milestone: 5 }); + }); + + it("reads the new body from --body-file", async () => { + const path = files.write("body.md", "Body from a file.\n"); + server = await startFixtureServer([{ method: "PATCH", path: ISSUE_PATH, body: issue() }]); + const { exitCode } = await runCliTest( + ["issue", "edit", "7", "--body-file", path], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(0); + expect(patchedIssue()).toEqual({ body: "Body from a file.\n" }); + }); + + it("posts --add-label names directly to the additive label endpoint", async () => { + server = await startFixtureServer([ + { method: "POST", path: LABELS_ENDPOINT, body: [] }, + ]); + const { exitCode } = await runCliTest( + ["issue", "edit", "7", "--add-label", "bug", "--add-label", "chore"], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(0); + const post = server.requests.find( + (request) => request.method === "POST" && request.path === LABELS_ENDPOINT, + ); + expect(post?.body).toEqual({ labels: ["bug", "chore"] }); + // No label lookup happens for additions — names go straight through. + expect(server.requests.some((request) => request.path === REPO_LABELS)).toBe(false); + }); + + it("resolves --remove-label to an id before deleting it", async () => { + server = await startFixtureServer([ + { method: "GET", path: REPO_LABELS, body: [{ id: 22, name: "Priority: High" }] }, + { method: "DELETE", path: `${LABELS_ENDPOINT}/22`, body: {} }, + ]); + const { exitCode } = await runCliTest( + ["issue", "edit", "7", "--remove-label", "priority: high"], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(0); + expect( + server.requests.some( + (request) => request.method === "DELETE" && request.path === `${LABELS_ENDPOINT}/22`, + ), + ).toBe(true); + }); + + it("rejects a --remove-label name not in the repo with VALIDATION_ERROR and mutates nothing", async () => { + server = await startFixtureServer([ + { method: "GET", path: REPO_LABELS, body: [{ id: 11, name: "bug" }] }, + ]); + const { stdout, exitCode } = await runCliTest( + ["issue", "edit", "7", "--remove-label", "ghost"], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(2); + expect(stdout).toContain("code: VALIDATION_ERROR"); + expect(stdout).toContain("ghost"); + expect(server.requests.some((request) => request.method === "DELETE")).toBe(false); + }); + + it("treats a 404 removing an unapplied label as silent success", async () => { + server = await startFixtureServer([ + { method: "GET", path: REPO_LABELS, body: [{ id: 22, name: "wontfix" }] }, + { method: "DELETE", path: `${LABELS_ENDPOINT}/22`, status: 404, body: { message: "not found" } }, + ]); + const { stdout, exitCode } = await runCliTest( + ["issue", "edit", "7", "--remove-label", "wontfix"], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(0); + expect(stdout).toContain("edited:"); + expect(stdout).toContain("status: ok"); + }); + + it("adds and removes assignees via fetch-then-patch, sending the full resulting list", async () => { + server = await startFixtureServer([ + { + method: "GET", + path: ISSUE_PATH, + body: issue({ assignees: [{ login: "alice" }, { login: "bob" }] }), + }, + { method: "PATCH", path: ISSUE_PATH, body: issue() }, + ]); + const { exitCode } = await runCliTest( + ["issue", "edit", "7", "--add-assignee", "carol", "--remove-assignee", "alice"], + { env: testModeEnv(server.url) }, + ); + + expect(exitCode).toBe(0); + expect(patchedIssue()).toEqual({ assignees: ["bob", "carol"] }); + // Exactly one PATCH carries the whole recomputed list. + expect(server.requests.filter((request) => request.method === "PATCH")).toHaveLength(1); + }); + + it("rejects an edit with no changes before calling the API", async () => { + server = await startFixtureServer([]); + const { stdout, exitCode } = await runCliTest(["issue", "edit", "7"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(2); + expect(stdout).toContain("code: VALIDATION_ERROR"); + expect(server.requests).toHaveLength(0); + }); + + it("prints help with --help without calling the API", async () => { + server = await startFixtureServer([]); + const { stdout, exitCode } = await runCliTest(["issue", "edit", "--help"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(0); + expect(stdout).toContain("usage: gitea-axi issue edit"); + expect(server.requests).toHaveLength(0); + }); +}); diff --git a/test/issue-reopen.test.ts b/test/issue-reopen.test.ts new file mode 100644 index 0000000..b3ed778 --- /dev/null +++ b/test/issue-reopen.test.ts @@ -0,0 +1,54 @@ +import { afterEach, describe, expect, it } from "vitest"; +import { startFixtureServer, type FixtureServer } from "./fixture-server.js"; +import { runCliTest, testModeEnv } from "./harness.js"; + +const ISSUE_PATH = "/api/v1/repos/testowner/testrepo/issues/7"; + +let server: FixtureServer; + +afterEach(async () => { + await server.close(); +}); + +describe("issue reopen", () => { + it("reopens a closed issue and reports the action", async () => { + server = await startFixtureServer([ + { method: "GET", path: ISSUE_PATH, body: { number: 7, state: "closed" } }, + { method: "PATCH", path: ISSUE_PATH, body: { number: 7, state: "open" } }, + ]); + const { stdout, exitCode } = await runCliTest(["issue", "reopen", "7"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(0); + expect(stdout).toContain("reopened:"); + expect(stdout).toContain("number: 7"); + expect(stdout).toContain("status: ok"); + const patch = server.requests.find((request) => request.method === "PATCH"); + expect(patch?.body).toEqual({ state: "open" }); + }); + + it("returns early with Already open on an already-open issue", async () => { + server = await startFixtureServer([ + { method: "GET", path: ISSUE_PATH, body: { number: 7, state: "open" } }, + ]); + const { stdout, exitCode } = await runCliTest(["issue", "reopen", "7"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(0); + expect(stdout).toContain("message: Already open"); + expect(server.requests.some((request) => request.method === "PATCH")).toBe(false); + }); + + it("prints help with --help without calling the API", async () => { + server = await startFixtureServer([]); + const { stdout, exitCode } = await runCliTest(["issue", "reopen", "--help"], { + env: testModeEnv(server.url), + }); + + expect(exitCode).toBe(0); + expect(stdout).toContain("usage: gitea-axi issue reopen"); + expect(server.requests).toHaveLength(0); + }); +}); -- 2.47.3