feat: add issue edit, close, and reopen (task 0005) #6

Merged
alexion merged 1 commits from task-0005-issue-edit-close-reopen into main 2026-07-12 18:35:01 -04:00
5 changed files with 637 additions and 17 deletions

View File

@@ -15,10 +15,25 @@ All three use the action-block pattern on success (`edited:`/`closed:`/`reopened
## Acceptance criteria ## Acceptance criteria
- [ ] `issue edit` applies title, body, and milestone changes and outputs `edited: { number, status: "ok" }` - [x] `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 - [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
- [ ] `--add-assignee`/`--remove-assignee` use fetch-then-patch, sending the full resulting assignee list in a single PATCH - [x] `--add-assignee`/`--remove-assignee` use fetch-then-patch, sending the full resulting assignee list in a single PATCH
- [ ] `issue close <n>` 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 <n>` 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 - [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
- [ ] `issue reopen <n>` outputs `reopened: { number, status: "ok" }` - [x] `issue reopen <n>` 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] 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.

View File

@@ -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 { BODY_TRUNCATE_LIMIT, COMMENT_TRUNCATE_LIMIT, truncateBody } from "../body.js";
import { requireBodySource, resolveBodySource } from "../body-source.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 { COMMENT_FLAGS, commentItem } from "../comment.js";
import { resolveRepoContext, type RepoContext } from "../context.js"; import { resolveRepoContext, type RepoContext } from "../context.js";
import type { CliDeps } from "../deps.js"; import type { CliDeps } from "../deps.js";
import { axiError, classifyHttpError } from "../errors.js"; import { axiError, classifyHttpError, httpStatus } from "../errors.js";
import { import {
extractRow, extractRow,
joined, joined,
@@ -34,11 +34,59 @@ commands:
list List issues in the current repository list List issues in the current repository
view Show a single issue's details view Show a single issue's details
create Create an issue 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 comment Post a comment on an issue or pull request
Run \`gitea-axi issue <command> --help\` for the flags of a command. Run \`gitea-axi issue <command> --help\` for the flags of a command.
`; `;
export const ISSUE_EDIT_HELP = `usage: gitea-axi issue edit <number> [flags]
Edit an issue in the current repository. At least one change is required.
flags:
--title <text> New title
--body <text> New body
--body-file <path> Read the new body from a file (mutually exclusive with --body)
--add-label <name> Add a label by name (repeatable)
--remove-label <name> Remove a label by name (repeatable, case-insensitive)
--add-assignee <login> Add an assignee (repeatable)
--remove-assignee <login> Remove an assignee (repeatable)
--milestone <name> Assign a milestone by name (case-insensitive)
--help Show this help
global flags:
-R, --repo <OWNER/NAME> Override the repository detected from the git origin remote
--login <name> Select a tea login profile by name
`;
export const ISSUE_CLOSE_HELP = `usage: gitea-axi issue close <number> [flags]
Close an issue in the current repository.
flags:
--comment <text> Post a comment when closing
--help Show this help
global flags:
-R, --repo <OWNER/NAME> Override the repository detected from the git origin remote
--login <name> Select a tea login profile by name
`;
export const ISSUE_REOPEN_HELP = `usage: gitea-axi issue reopen <number>
Reopen a closed issue in the current repository.
flags:
--help Show this help
global flags:
-R, --repo <OWNER/NAME> Override the repository detected from the git origin remote
--login <name> Select a tea login profile by name
`;
export const ISSUE_CREATE_HELP = `usage: gitea-axi issue create --title <text> [flags] export const ISSUE_CREATE_HELP = `usage: gitea-axi issue create --title <text> [flags]
Create an issue in the current repository. 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<Issue> {
try {
const response = await api.repos.issueGetIssue(context.owner, context.name, number);
return response.data;
} catch (error) {
throw classifyHttpError(error);
}
}
function issueViewSuggestions( function issueViewSuggestions(
context: RepoContext, context: RepoContext,
number: number, number: number,
@@ -404,13 +462,7 @@ async function issueView(deps: CliDeps, args: string[]): Promise<string> {
const context = await resolveRepoContext(deps); const context = await resolveRepoContext(deps);
const api = createClient(context); const api = createClient(context);
let issue: Issue; const issue = await getIssue(api, context, number);
try {
const response = await api.repos.issueGetIssue(context.owner, context.name, number);
issue = response.data;
} catch (error) {
throw classifyHttpError(error);
}
if (issue.pull_request) { if (issue.pull_request) {
throw axiError(`issue #${number} is a pull request`, "VALIDATION_ERROR", [ throw axiError(`issue #${number} is a pull request`, "VALIDATION_ERROR", [
@@ -580,6 +632,231 @@ async function issueComment(deps: CliDeps, args: string[]): Promise<string> {
return renderDetail({ noun: "comment", item, help }); 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<string[]> {
const issue = await getIssue(api, context, number);
const removeSet = new Set(remove.map((login) => login.toLowerCase()));
const seen = new Set<string>();
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<string> {
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<string> {
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<string> {
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) { export function issueCommand(deps: CliDeps) {
return async (args: string[]): Promise<string> => { return async (args: string[]): Promise<string> => {
const [subcommand, ...rest] = args; const [subcommand, ...rest] = args;
@@ -595,6 +872,15 @@ export function issueCommand(deps: CliDeps) {
if (subcommand === "create") { if (subcommand === "create") {
return issueCreate(deps, rest); 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") { if (subcommand === "comment") {
return issueComment(deps, rest); return issueComment(deps, rest);
} }

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