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
8 changes: 5 additions & 3 deletions src/commands/partial/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Args, Flags, ux } from "@oclif/core";

import * as ApiV1 from "@/lib/api-v1";
import BaseCommand from "@/lib/base-command";
import { formatCommandScope } from "@/lib/helpers/command";
import { formatDateTime } from "@/lib/helpers/date";
import { ApiError } from "@/lib/helpers/error";
import * as CustomFlags from "@/lib/helpers/flag";
import { formatErrorRespMessage, isSuccessResp } from "@/lib/helpers/request";
import { spinner } from "@/lib/helpers/ux";

Expand All @@ -15,6 +17,7 @@ export default class PartialGet extends BaseCommand<typeof PartialGet> {
default: "development",
summary: "The environment to use.",
}),
branch: CustomFlags.branch,
"hide-uncommitted-changes": Flags.boolean({
summary: "Hide any uncommitted changes.",
}),
Expand Down Expand Up @@ -64,9 +67,8 @@ export default class PartialGet extends BaseCommand<typeof PartialGet> {
const qualifier =
env === "development" && !commitedOnly ? "(including uncommitted)" : "";

this.log(
`‣ Showing partial \`${partialKey}\` in \`${env}\` environment ${qualifier}\n`,
);
const scope = formatCommandScope(this.props.flags);
this.log(`‣ Showing partial \`${partialKey}\` in ${scope} ${qualifier}\n`);

/*
* Partial table
Expand Down
8 changes: 5 additions & 3 deletions src/commands/partial/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { AxiosResponse } from "axios";

import * as ApiV1 from "@/lib/api-v1";
import BaseCommand from "@/lib/base-command";
import { formatCommandScope } from "@/lib/helpers/command";
import { formatDate } from "@/lib/helpers/date";
import * as CustomFlags from "@/lib/helpers/flag";
import { merge } from "@/lib/helpers/object.isomorphic";
import {
maybePromptPageAction,
Expand All @@ -20,6 +22,7 @@ export default class PartialList extends BaseCommand<typeof PartialList> {
default: "development",
summary: "The environment to use.",
}),
branch: CustomFlags.branch,
"hide-uncommitted-changes": Flags.boolean({
summary: "Hide any uncommitted changes.",
}),
Expand Down Expand Up @@ -55,9 +58,8 @@ export default class PartialList extends BaseCommand<typeof PartialList> {
const qualifier =
env === "development" && !committedOnly ? "(including uncommitted)" : "";

this.log(
`‣ Showing ${entries.length} partials in \`${env}\` environment ${qualifier}\n`,
);
const scope = formatCommandScope(this.props.flags);
this.log(`‣ Showing ${entries.length} partials in ${scope} ${qualifier}\n`);

/*
* Partials list table
Expand Down
8 changes: 6 additions & 2 deletions src/commands/partial/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Args, Flags } from "@oclif/core";

import * as ApiV1 from "@/lib/api-v1";
import BaseCommand from "@/lib/base-command";
import { formatCommandScope } from "@/lib/helpers/command";
import { ApiError } from "@/lib/helpers/error";
import * as CustomFlags from "@/lib/helpers/flag";
import { merge } from "@/lib/helpers/object.isomorphic";
Expand Down Expand Up @@ -31,6 +32,7 @@ export default class PartialPull extends BaseCommand<typeof PartialPull> {
default: "development",
summary: "The environment to use.",
}),
branch: CustomFlags.branch,
all: Flags.boolean({
summary: "Whether to pull all partials from the specified environment.",
}),
Expand Down Expand Up @@ -89,8 +91,9 @@ export default class PartialPull extends BaseCommand<typeof PartialPull> {
await Partial.writePartialDirFromData(dirContext, resp.data);

const action = dirContext.exists ? "updated" : "created";
const scope = formatCommandScope(flags);
this.log(
`‣ Successfully ${action} \`${dirContext.key}\` at ${dirContext.abspath}`,
`‣ Successfully ${action} \`${dirContext.key}\` at ${dirContext.abspath} using ${scope}`,
);
}

Expand All @@ -116,8 +119,9 @@ export default class PartialPull extends BaseCommand<typeof PartialPull> {
spinner.stop();

const action = targetDirCtx.exists ? "updated" : "created";
const scope = formatCommandScope(flags);
this.log(
`‣ Successfully ${action} the partials directory at ${targetDirCtx.abspath}`,
`‣ Successfully ${action} the partials directory at ${targetDirCtx.abspath} using ${scope}`,
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/commands/partial/push.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Args, Flags } from "@oclif/core";

import BaseCommand from "@/lib/base-command";
import { formatCommandScope } from "@/lib/helpers/command";
import { KnockEnv } from "@/lib/helpers/const";
import { formatError, formatErrors, SourceError } from "@/lib/helpers/error";
import * as CustomFlags from "@/lib/helpers/flag";
Expand All @@ -24,6 +25,7 @@ export default class PartialPush extends BaseCommand<typeof PartialPush> {
default: KnockEnv.Development,
options: [KnockEnv.Development],
}),
branch: CustomFlags.branch,
all: Flags.boolean({
summary: "Whether to push all partials from the target directory.",
}),
Expand Down Expand Up @@ -121,8 +123,9 @@ export default class PartialPush extends BaseCommand<typeof PartialPush> {
const partialKeys = partials.map((l) => l.key);
const actioned = flags.commit ? "pushed and committed" : "pushed";

const scope = formatCommandScope(flags);
this.log(
`‣ Successfully ${actioned} ${partials.length} partial(s):\n` +
`‣ Successfully ${actioned} ${partials.length} partial(s) to ${scope}:\n` +
indentString(partialKeys.join("\n"), 4),
);
}
Expand Down
5 changes: 4 additions & 1 deletion src/commands/partial/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Args, Flags } from "@oclif/core";

import * as ApiV1 from "@/lib/api-v1";
import BaseCommand, { Props } from "@/lib/base-command";
import { formatCommandScope } from "@/lib/helpers/command";
import { KnockEnv } from "@/lib/helpers/const";
import { formatErrors, SourceError } from "@/lib/helpers/error";
import * as CustomFlags from "@/lib/helpers/flag";
Expand All @@ -24,6 +25,7 @@ export default class PartialValidate extends BaseCommand<
default: KnockEnv.Development,
options: [KnockEnv.Development],
}),
branch: CustomFlags.branch,
all: Flags.boolean({
summary: "Whether to validate all partials from the target directory.",
}),
Expand Down Expand Up @@ -76,8 +78,9 @@ export default class PartialValidate extends BaseCommand<

// 3. Display a success message.
const partialKeys = partials.map((p) => p.key);
const scope = formatCommandScope({ ...this.props.flags });
this.log(
`‣ Successfully validated ${partials.length} partial(s):\n` +
`‣ Successfully validated ${partials.length} partial(s) using ${scope}:\n` +
indentString(partialKeys.join("\n"), 4),
);
}
Expand Down
4 changes: 4 additions & 0 deletions src/lib/api-v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,7 @@ export default class ApiV1 {
}: Props): Promise<AxiosResponse<ListPartialResp<A>>> {
const params = prune({
environment: flags.environment,
branch: flags.branch,
hide_uncommitted_changes: flags["hide-uncommitted-changes"],
annotate: flags.annotate,
...toPageParams(flags),
Expand All @@ -345,6 +346,7 @@ export default class ApiV1 {
}: Props): Promise<AxiosResponse<GetPartialResp<A>>> {
const params = prune({
environment: flags.environment,
branch: flags.branch,
annotate: flags.annotate,
hide_uncommitted_changes: flags["hide-uncommitted-changes"],
});
Expand All @@ -358,6 +360,7 @@ export default class ApiV1 {
): Promise<AxiosResponse<UpsertPartialResp<A>>> {
const params = prune({
environment: flags.environment,
branch: flags.branch,
annotate: flags.annotate,
commit: flags.commit,
commit_message: flags["commit-message"],
Expand All @@ -373,6 +376,7 @@ export default class ApiV1 {
): Promise<AxiosResponse<ValidatePartialResp>> {
const params = prune({
environment: flags.environment,
branch: flags.branch,
});
const data = { partial };

Expand Down
33 changes: 33 additions & 0 deletions test/commands/partial/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,39 @@ describe("commands/partial/get", () => {
});
});

describe("given a branch flag", () => {
test
.env({ KNOCK_SERVICE_TOKEN: "valid-token" })
.stub(KnockApiV1.prototype, "whoami", (stub) =>
stub.resolves(factory.resp({ data: whoami })),
)
.stub(KnockApiV1.prototype, "getPartial", (stub) =>
stub.resolves(
factory.resp({
data: factory.partial(),
}),
),
)
.stdout()
.command(["partial get", "foo", "--branch", "my-feature-branch-123"])
.it("calls apiV1 getPartial with expected params", () => {
sinon.assert.calledWith(
KnockApiV1.prototype.getPartial as any,
sinon.match(
({ args, flags }) =>
isEqual(args, {
partialKey: "foo",
}) &&
isEqual(flags, {
"service-token": "valid-token",
environment: "development",
branch: "my-feature-branch-123",
}),
),
);
});
});

describe("given a partial key that does not exist", () => {
test
.env({ KNOCK_SERVICE_TOKEN: "valid-token" })
Expand Down
24 changes: 24 additions & 0 deletions test/commands/partial/list.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,30 @@ describe("commands/partial/list", () => {
});
});

describe("given a branch flag", () => {
test
.env({ KNOCK_SERVICE_TOKEN: "valid-token" })
.stub(KnockApiV1.prototype, "listPartials", (stub) =>
stub.resolves(emptyPartialsListResp),
)
.stdout()
.command(["partial list", "--branch", "my-feature-branch-123"])
.it("calls apiV1 listPartials with expected params", () => {
sinon.assert.calledWith(
KnockApiV1.prototype.listPartials as any,
sinon.match(
({ args, flags }) =>
isEqual(args, {}) &&
isEqual(flags, {
"service-token": "valid-token",
environment: "development",
branch: "my-feature-branch-123",
}),
),
);
});
});

describe("given a list of partials in response", () => {
test
.env({ KNOCK_SERVICE_TOKEN: "valid-token" })
Expand Down
28 changes: 28 additions & 0 deletions test/commands/partial/pull.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,34 @@ describe("commands/partial/pull", () => {
);
});

describe("given a branch flag", () => {
setupWithGetPartialStub({ key: "partial-x" })
.stdout()
.command([
"partial pull",
"partial-x",
"--branch",
"my-feature-branch-123",
])
.it("calls apiV1 getPartial with expected params", () => {
sinon.assert.calledWith(
KnockApiV1.prototype.getPartial as any,
sinon.match(
({ args, flags }) =>
isEqual(args, {
partialKey: "partial-x",
}) &&
isEqual(flags, {
"service-token": "valid-token",
environment: "development",
branch: "my-feature-branch-123",
annotate: true,
}),
),
);
});
});

describe("given both a partial key arg and a --all flag", () => {
test
.env({ KNOCK_SERVICE_TOKEN: "valid-token" })
Expand Down
32 changes: 32 additions & 0 deletions test/commands/partial/push.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,38 @@ describe("commands/partial/push", () => {
);
});

describe("given a branch flag", () => {
setupWithStub({ data: { partial: mockPartialData } })
.stdout()
.command([
"partial push",
"default",
"--branch",
"my-feature-branch-123",
])
.it("calls apiV1 upsertPartial with expected params", () => {
sinon.assert.calledWith(
KnockApiV1.prototype.upsertPartial as any,
sinon.match(
({ args, flags }) =>
isEqual(args, { partialKey: "default" }) &&
isEqual(flags, {
"service-token": "valid-token",
environment: "development",
branch: "my-feature-branch-123",
annotate: true,
}),
),
sinon.match((partial) =>
isEqual(partial, {
key: "default",
name: "Default",
}),
),
);
});
});

setupWithStub({ data: { partial: mockPartialData } })
.stdout()
.command(["partial push", "default"])
Expand Down
31 changes: 31 additions & 0 deletions test/commands/partial/validate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,37 @@ describe("commands/partial/validate (a single partial)", () => {
),
);
});

describe("given a branch flag", () => {
setupWithStub()
.stdout()
.command([
"partial validate",
"default",
"--branch",
"my-feature-branch-123",
])
.it("calls apiV1 validatePartial with expected params", () => {
sinon.assert.calledWith(
KnockApiV1.prototype.validatePartial as any,
sinon.match(
({ args, flags }) =>
isEqual(args, { partialKey: "default" }) &&
isEqual(flags, {
"service-token": "valid-token",
environment: "development",
branch: "my-feature-branch-123",
}),
),
sinon.match((partial) =>
isEqual(partial, {
key: "default",
name: "Default",
}),
),
);
});
});
});

describe("given a partial.json file, with syntax errors", () => {
Expand Down