feat: add issue edit, close, and reopen (task 0005)
All checks were successful
CI / test (pull_request) Successful in 27s
CI / test (push) Successful in 37s

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.
This commit was merged in pull request #6.
This commit is contained in:
2026-07-12 09:47:51 -04:00
parent 3c581b7c0e
commit 6296f47fab
5 changed files with 637 additions and 17 deletions

92
test/issue-close.test.ts Normal file
View File

@@ -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);
});
});

173
test/issue-edit.test.ts Normal file
View File

@@ -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<string, unknown> {
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<string, unknown>;
}
function issue(fields: Record<string, unknown> = {}): Record<string, unknown> {
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);
});
});

54
test/issue-reopen.test.ts Normal file
View File

@@ -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);
});
});