diff --git a/.changeset/empty-tracker-flag.md b/.changeset/empty-tracker-flag.md new file mode 100644 index 00000000..c6b282c5 --- /dev/null +++ b/.changeset/empty-tracker-flag.md @@ -0,0 +1,5 @@ +--- +"@northguild/worktree": patch +--- + +`worktree branch` now refuses an empty `--github`, `--jira`, `--linear` or `--clickup` (exit `2`, `invalid_value`) instead of going on as if the flag were not given. diff --git a/docs/src/content/docs/commands/branch.mdx b/docs/src/content/docs/commands/branch.mdx index 54e8d4b0..01797d8f 100644 --- a/docs/src/content/docs/commands/branch.mdx +++ b/docs/src/content/docs/commands/branch.mdx @@ -702,7 +702,7 @@ describing it goes to stderr as usual: | `code` | Means | Exit | | --- | --- | --- | | `missing_value` | a value with no default was not given, such as `--linear` with no identifier in a run that cannot ask or with `--dry-run`; `details` names it and the flag | `2` | -| `invalid_value` | a value or flag combination was refused: a bad branch name, an empty brief, a malformed `--github`, `--jira`, `--linear` or `--clickup` issue id, a reference the `--issue` provider calls `invalid_reference`, an empty `--issue`, a `--github`, `--jira` or `--linear` URL for another repository or GitHub host, Jira site or Linear workspace or of a shape not accepted, a `--github` number that is a pull request, a branch name that is already taken | `2` | +| `invalid_value` | a value or flag combination was refused: a bad branch name, an empty brief, a malformed `--github`, `--jira`, `--linear` or `--clickup` issue id, a reference the `--issue` provider calls `invalid_reference`, an empty `--github`, `--jira`, `--linear`, `--clickup` or `--issue`, a `--github`, `--jira` or `--linear` URL for another repository or GitHub host, Jira site or Linear workspace or of a shape not accepted, a `--github` number that is a pull request, a branch name that is already taken | `2` | | `not_found` | something named does not exist, such as the `--source` branch | `2` | | `timeout` | a call that is bounded did not answer in time | `1` | | `failed` | anything else | `1` | diff --git a/src/commands/branch.test.ts b/src/commands/branch.test.ts index f411e1bd..e8956704 100644 --- a/src/commands/branch.test.ts +++ b/src/commands/branch.test.ts @@ -8381,6 +8381,52 @@ describe("branch command — --issue (a custom issue provider)", () => { expect(git.gitCreateWorktree).not.toHaveBeenCalled(); }); + // #363: an empty tracker flag was read as "not given" by every truthiness + // test, so the run went on as a plain branch or named the wrong problem. + // Each message names what that flag takes: --clickup takes no URL. + it.each([ + ["github", { github: "" }, "--github needs the issue's number or URL."], + ["jira", { jira: "" }, "--jira needs the issue's key or URL."], + [ + "linear", + { linear: "" }, + "--linear needs the issue's identifier or URL.", + ], + ["clickup", { clickup: "" }, "--clickup needs the task's id."], + [ + "github", + { github: " " }, + "--github needs the issue's number or URL.", + ], + ])("refuses an empty --%s with invalid_value and exit 2", async (_name, flags, message) => { + await runJson({ ...flags }); + + expect(output.document()).toMatchObject({ + error: { code: "invalid_value", message }, + }); + expect(process.exitCode).toBe(2); + expect(mockRunCapturing).not.toHaveBeenCalled(); + expect(git.gitCreateWorktree).not.toHaveBeenCalled(); + }); + + it.each([ + ["--assign", { assign: true }], + ["--start", { start: true }], + ["--dry-run", { "dry-run": true }], + ])("an empty --github with %s is refused as empty, not as a missing issue flag", async (_flag, other) => { + await expect(runPlain({ github: "", ...other })).rejects.toThrow( + "--github needs the issue's number or URL.", + ); + expect(git.gitCreateWorktree).not.toHaveBeenCalled(); + }); + + it("refuses an empty --issue before the fetch", async () => { + await expect(runPlain({ issue: "" })).rejects.toThrow( + "--issue needs the issue's key, number or URL.", + ); + expect(git.gitFetch).not.toHaveBeenCalled(); + }); + it("reads the provider pair from git config once per run", async () => { await runJson({ issue: "VERK-42" }); diff --git a/src/commands/branch.ts b/src/commands/branch.ts index ae204a90..d4652e43 100644 --- a/src/commands/branch.ts +++ b/src/commands/branch.ts @@ -1533,6 +1533,22 @@ missing issue exits 1; declining to open an existing worktree exits 1)`; ); } + // `--github ""`, `--github=` and the like parse to an empty string, which + // every truthiness test below reads as "not given" (#363). The flag was + // given, so it is refused here, before the brief is read and before the + // fetch. Each message names what that flag takes. + for (const [name, value, wanted] of [ + ["github", flags.github, "the issue's number or URL"], + ["jira", flags.jira, "the issue's key or URL"], + ["linear", flags.linear, "the issue's identifier or URL"], + ["clickup", flags.clickup, "the task's id"], + ["issue", flags.issue, "the issue's key, number or URL"], + ] as const) { + if (value !== undefined && value.trim() === "") { + throw new InvalidValueError(`--${name} needs ${wanted}.`); + } + } + // There is no issue to assign without one of them. Checked here rather than // beside the assignment itself so a malformed command line fails before the // issue fetch and the branch-name prompt, in the shape of the check above. @@ -1638,13 +1654,8 @@ missing issue exits 1; declining to open an existing worktree exits 1)`; // the file is where it came from, before anything can run it. Only a run // with --issue reads an issueProvider key. if (flags.issue !== undefined) { - const reference = flags.issue.trim(); - if (reference === "") { - throw new InvalidValueError( - "--issue needs the issue's key, number or URL.", - ); - } - flags.issue = reference; + // An empty one was refused above, with the other issue flags. + flags.issue = flags.issue.trim(); this.issueProvider = await resolveIssueProvider(); if (!this.issueProvider) { throw new MissingValueError(