Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-tracker-flag.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion docs/src/content/docs/commands/branch.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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` |
Expand Down
46 changes: 46 additions & 0 deletions src/commands/branch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });

Expand Down
25 changes: 18 additions & 7 deletions src/commands/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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(
Expand Down