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
2 changes: 1 addition & 1 deletion plugins/slack/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ on the pod.
| Setting | What it is |
|---|---|
| Slack sign-in token | Filled in by Sign in with Slack. A secret; do not edit or paste by hand. |
| Project | Which bb project a Slack conversation starts its thread in. Empty uses the first project on this server. |
| Project | Which bb project a Slack conversation starts its thread in. Empty uses the personal project, or the first project when this server has none. |

**Sign out** forgets the token and reconnects the stream, so the service drops
the identity bound to the old connection.
Expand Down
42 changes: 38 additions & 4 deletions plugins/slack/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ describe("Slack plugin", () => {
expect(await s.harness.callRpc("status")).toEqual({
signedIn: false,
user: null,
project: { id: "prj-1", name: "Project One" },
project: { id: "prj-2", name: "Personal" },
projects: PROJECTS,
hooks: { configured: true, connected: true, state: "connected" },
});
Expand Down Expand Up @@ -254,7 +254,7 @@ describe("Slack plugin", () => {
// The thread runs where the developer's newest environment runs: that
// provider, with its inputs, on its machine; never the pod's own host.
expect(spawns[0]![0]).toMatchObject({
projectId: "prj-1",
projectId: "prj-2",
environment: { type: "provider", environmentProviderId: "multi-repo-workspace", inputs: { repos: ["a"] }, machine: { type: "existing", hostId: "host_on" } },
});
expect(String((spawns[0]![0] as { prompt: string }).prompt)).toContain("session D0123:1712345678.901234");
Expand Down Expand Up @@ -314,7 +314,7 @@ describe("Slack plugin", () => {
});

it("runs the thread in the project's checkout on its connected machine when it has no environment there", async () => {
const s = await live({ environments: [] });
const s = await live({ project: "prj-1", environments: [] });
s.hooks.last().send(s.hooks.session());
await tick();
expect(s.harness.sdk.callsTo("threads.spawn")[0]![0]).toMatchObject({
Expand Down Expand Up @@ -356,14 +356,48 @@ describe("Slack plugin", () => {

it("changes the project the next Slack conversation goes to", async () => {
const s = await live();
expect((await s.harness.callRpc("setProject", { projectId: "prj-2" })) as { ok: boolean }).toEqual({ ok: true });
// prj-1 is the standard project; the empty setting would pick the personal
// prj-2, so this proves the setting wins over the default.
expect((await s.harness.callRpc("setProject", { projectId: "prj-1" })) as { ok: boolean }).toEqual({ ok: true });
expect(((await s.harness.callRpc("status")) as { project: { id: string } }).project.id).toBe("prj-1");
s.hooks.last().send(s.hooks.session());
await tick();
expect((s.harness.sdk.callsTo("threads.spawn")[0]![0] as { projectId: string }).projectId).toBe("prj-1");
await s.stop();
});

it("starts a conversation in the personal project when the setting is empty, even when it is not first", async () => {
const s = await live();
expect(((await s.harness.callRpc("status")) as { project: { id: string } }).project.id).toBe("prj-2");
s.hooks.last().send(s.hooks.session());
await tick();
expect((s.harness.sdk.callsTo("threads.spawn")[0]![0] as { projectId: string }).projectId).toBe("prj-2");
await s.stop();
});

it("falls back to the personal project when the setting names no project", async () => {
const s = await live({ project: "not-a-project" });
expect(((await s.harness.callRpc("status")) as { project: { id: string } }).project.id).toBe("prj-2");
s.hooks.last().send(s.hooks.session());
await tick();
expect((s.harness.sdk.callsTo("threads.spawn")[0]![0] as { projectId: string }).projectId).toBe("prj-2");
await s.stop();
});

it("falls back to the first standard project when the server has no personal one", async () => {
const s = await live({
projects: [
{ id: "prj-1", name: "Project One", kind: "standard" },
{ id: "prj-3", name: "Project Two", kind: "standard" },
],
});
expect(((await s.harness.callRpc("status")) as { project: { id: string } }).project.id).toBe("prj-1");
s.hooks.last().send(s.hooks.session());
await tick();
expect((s.harness.sdk.callsTo("threads.spawn")[0]![0] as { projectId: string }).projectId).toBe("prj-1");
await s.stop();
});

it("keeps the conversation's thread on a transient send error, and starts a new one only when it is gone", async () => {
let sends = 0;
const transient = await live({
Expand Down
10 changes: 7 additions & 3 deletions plugins/slack/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function settingDescriptors(_env: Record<string, string | undefined>) {
project: {
type: "string",
label: "Project",
description: "Which bb project a Slack conversation starts its thread in. Leave empty to use the first project on this server.",
description: "Which bb project a Slack conversation starts its thread in. Leave empty to use the personal project, or the first project when this server has none.",
},
} as const;
}
Expand Down Expand Up @@ -114,14 +114,18 @@ type Project = { id: string; name: string; kind: string };
/** The environment a spawned thread runs in: `threads.spawn`'s `environment`. */
type SpawnEnvironment = Parameters<BbPluginApi["sdk"]["threads"]["spawn"]>[0]["environment"];

/** The project a Slack conversation goes to: the chosen one, else the first. */
/**
* The project a Slack conversation goes to: the chosen one, else the personal
* project (the way a thread with no project starts in bb), and only when there
* is none the first standard project.
*/
export function chooseProject(projects: Project[], chosen: string): Project | null {
const named = chosen.trim();
if (named !== "") {
const found = projects.find((p) => p.id === named || p.name.toLowerCase() === named.toLowerCase());
if (found !== undefined) return found;
}
return projects.find((p) => p.kind === "standard") ?? projects[0] ?? null;
return projects.find((p) => p.kind === "personal") ?? projects.find((p) => p.kind === "standard") ?? projects[0] ?? null;
}

export function createSlackPlugin(deps: SlackPluginDeps): (bb: BbPluginApi) => Promise<void> {
Expand Down
Loading