-
Notifications
You must be signed in to change notification settings - Fork 140
feat(runner): configure emails client before running flows #1382
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Chase J (chajac)
wants to merge
4
commits into
main
Choose a base branch
from
chajac/cli-emails-wiring
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+256
−36
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d02055e
refactor(cli): extract resolveApiBaseUrl helper
chajac cc0cc17
feat(runner): configure emails client via platform API
chajac 8d83426
feat(runner): add configureEmailsForRun helper
chajac 77824a9
feat(runner): configure emails before running flows
chajac File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import type { Fs } from "~/shell/fs.js"; | ||
| import { configureEmailsForRun } from "./configureEmailsForRun.js"; | ||
|
|
||
| const fakeFs = {} as Fs; | ||
|
|
||
| function baseParams() { | ||
| return { | ||
| apiBaseUrl: "https://app.qawolf.com", | ||
| configDir: "/cfg", | ||
| cwd: "/env", | ||
| fs: fakeFs, | ||
| log: undefined, | ||
| }; | ||
| } | ||
|
|
||
| const okResolveApiKey = async () => ({ key: "k", source: "env" as const }); | ||
| const okGetIdentity = async () => ({ | ||
| ok: true as const, | ||
| data: { team: { id: "team-1", name: "T", createdAt: "2026-01-01" } }, | ||
| }); | ||
|
|
||
| describe("configureEmailsForRun", () => { | ||
| it("configures emails on the happy path", async () => { | ||
| let captured: unknown; | ||
| const outcome = await configureEmailsForRun(baseParams(), { | ||
| resolveApiKey: okResolveApiKey, | ||
| getIdentity: okGetIdentity, | ||
| configureEmails: async (p: unknown) => { | ||
| captured = p; | ||
| }, | ||
| }); | ||
| expect(outcome).toBe("configured"); | ||
| expect(captured).toEqual({ | ||
| apiBaseUrl: "https://app.qawolf.com", | ||
| apiKey: "k", | ||
| teamId: "team-1", | ||
| cwd: "/env", | ||
| }); | ||
| }); | ||
|
|
||
| it("skips when not authenticated and does not call configureEmails", async () => { | ||
| let called = false; | ||
| const outcome = await configureEmailsForRun(baseParams(), { | ||
| resolveApiKey: async () => undefined, | ||
| getIdentity: okGetIdentity, | ||
| configureEmails: async () => { | ||
| called = true; | ||
| }, | ||
| }); | ||
| expect(outcome).toBe("skipped-not-authenticated"); | ||
| expect(called).toBe(false); | ||
| }); | ||
|
|
||
| it("skips when identity cannot be resolved", async () => { | ||
| const outcome = await configureEmailsForRun(baseParams(), { | ||
| resolveApiKey: okResolveApiKey, | ||
| getIdentity: async () => ({ | ||
| ok: false as const, | ||
| error: { kind: "network" as const, cause: new Error("offline") }, | ||
| }), | ||
| configureEmails: async () => {}, | ||
| }); | ||
| expect(outcome).toBe("skipped-identity-unavailable"); | ||
| }); | ||
|
|
||
| it("skips when the emails client cannot be built", async () => { | ||
| const outcome = await configureEmailsForRun(baseParams(), { | ||
| resolveApiKey: okResolveApiKey, | ||
| getIdentity: okGetIdentity, | ||
| configureEmails: async () => { | ||
| throw new Error("module missing"); | ||
| }, | ||
| }); | ||
| expect(outcome).toBe("skipped-emails-unavailable"); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| import { resolveApiKey } from "~/domains/auth/index.js"; | ||
| import { configureEmails as defaultConfigureEmails } from "~/domains/emails/configureEmails.js"; | ||
| import type { Fs } from "~/shell/fs.js"; | ||
| import { getIdentity } from "~/shell/platform/getIdentity.js"; | ||
| import type { CommandContext } from "~/shell/commandContext.js"; | ||
|
|
||
| export type ConfigureEmailsOutcome = | ||
| | "configured" | ||
| | "skipped-not-authenticated" | ||
| | "skipped-identity-unavailable" | ||
| | "skipped-emails-unavailable"; | ||
|
|
||
| type ConfigureEmailsForRunDeps = { | ||
| resolveApiKey: typeof resolveApiKey; | ||
| getIdentity: typeof getIdentity; | ||
| configureEmails: typeof defaultConfigureEmails; | ||
| }; | ||
|
|
||
| function makeDefaultDeps(): ConfigureEmailsForRunDeps { | ||
| return { | ||
| resolveApiKey, | ||
| getIdentity, | ||
| configureEmails: defaultConfigureEmails, | ||
| }; | ||
| } | ||
|
|
||
| // Resolve credentials and register the emails client for the current process. | ||
| // Total: any failure degrades gracefully to a "skipped-*" outcome so a run is | ||
| // never broken by email setup. Email-dependent flows that need a client and did | ||
| // not get one surface @qawolf/emails' own clear error at mail.inbox() time. | ||
| export async function configureEmailsForRun( | ||
| params: { | ||
| apiBaseUrl: string; | ||
| configDir: string; | ||
| cwd: string; | ||
| fs: Fs; | ||
| log: ((message: string) => void) | undefined; | ||
| }, | ||
| deps?: ConfigureEmailsForRunDeps, | ||
| ): Promise<ConfigureEmailsOutcome> { | ||
| const resolvedDeps = deps ?? makeDefaultDeps(); | ||
| const log = params.log ?? ((): void => undefined); | ||
|
|
||
| const apiKey = await resolvedDeps.resolveApiKey(params.configDir, params.fs); | ||
| if (apiKey === undefined) { | ||
| log("emails: skipped — not authenticated"); | ||
| return "skipped-not-authenticated"; | ||
| } | ||
|
|
||
| const identity = await resolvedDeps.getIdentity(apiKey.key, { | ||
| fetch: globalThis.fetch, | ||
| baseUrl: params.apiBaseUrl, | ||
| }); | ||
| if (!identity.ok) { | ||
| log(`emails: skipped — team identity unavailable (${identity.error.kind})`); | ||
| return "skipped-identity-unavailable"; | ||
| } | ||
|
|
||
| try { | ||
| await resolvedDeps.configureEmails({ | ||
| apiBaseUrl: params.apiBaseUrl, | ||
| apiKey: apiKey.key, | ||
| teamId: identity.data.team.id, | ||
| cwd: params.cwd, | ||
| }); | ||
| } catch (err) { | ||
| const detail = err instanceof Error ? err.message : String(err); | ||
| log(`emails: skipped — client unavailable (${detail})`); | ||
| return "skipped-emails-unavailable"; | ||
| } | ||
|
|
||
| log("emails: configured"); | ||
| return "configured"; | ||
| } | ||
|
|
||
| // In-process (`--workers 1`) convenience over configureEmailsForRun: pulls | ||
| // credentials from the command context. No-op for pooled runs — the worker | ||
| // entry configures those processes itself. | ||
| export async function configureEmailsForInProcessRun( | ||
| ctx: CommandContext, | ||
| cwd: string, | ||
| workers: number, | ||
| ): Promise<void> { | ||
| if (workers !== 1) return; | ||
| await configureEmailsForRun({ | ||
| apiBaseUrl: ctx.apiBaseUrl, | ||
| configDir: ctx.configDir, | ||
| cwd, | ||
| fs: ctx.fs, | ||
| log: (message) => ctx.log("emails").debug(message), | ||
| }); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { describe, expect, it } from "bun:test"; | ||
| import { resolveApiBaseUrl } from "./resolveApiBaseUrl.js"; | ||
|
|
||
| describe("resolveApiBaseUrl", () => { | ||
| it("returns the production default when QAWOLF_API_URL is unset", () => { | ||
| expect(resolveApiBaseUrl({})).toBe("https://app.qawolf.com"); | ||
| }); | ||
|
|
||
| it("uses QAWOLF_API_URL when set", () => { | ||
| expect( | ||
| resolveApiBaseUrl({ QAWOLF_API_URL: "https://staging.example.com" }), | ||
| ).toBe("https://staging.example.com"); | ||
| }); | ||
|
|
||
| it("trims trailing slashes", () => { | ||
| expect( | ||
| resolveApiBaseUrl({ QAWOLF_API_URL: "https://x.example.com///" }), | ||
| ).toBe("https://x.example.com"); | ||
| }); | ||
|
|
||
| it("falls back to the default for an empty string", () => { | ||
| expect(resolveApiBaseUrl({ QAWOLF_API_URL: "" })).toBe( | ||
| "https://app.qawolf.com", | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.