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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ For a headless machine, authenticate first with `npx wrangler login --device --b

- A Cloudflare account with Workers enabled.
- A Linear OAuth application with webhooks enabled.
- Optional: a ClickUp OAuth application for ClickUp task triggers.
- An exe.dev VM with Herdr and your coding agent (such as Codex or Claude) installed.
- An exe.dev HTTPS API token restricted to the exact SSH destination Factorize will use, for example `ssh exedev@my-vm`.

Expand Down Expand Up @@ -90,6 +91,10 @@ Use authorization-code OAuth and enable **Webhooks** on the OAuth application. C

After changing the webhook settings, re-authorize existing workspaces so Linear creates a fresh workspace subscription.

## Configure ClickUp

Create a ClickUp OAuth application with the callback URL `https://your-domain.example/auth/clickup/callback`, then store its client ID and secret in `CLICKUP_CLIENT_ID` and `CLICKUP_CLIENT_SECRET`. Connecting ClickUp from Settings registers the signed task webhook automatically. ClickUp triggers can filter a list by status, tag, assignee, or creator; all configured rules must match.

## Create a Job

1. Sign in with Linear.
Expand All @@ -107,6 +112,8 @@ Cloudflare Worker failures can also start Jobs through the separately deployable
| `LINEAR_CLIENT_ID` | Linear OAuth client ID |
| `LINEAR_CLIENT_SECRET` | Linear OAuth client secret |
| `LINEAR_WEBHOOK_SIGNING_SECRET` | Verifies Linear webhook signatures |
| `CLICKUP_CLIENT_ID` | ClickUp OAuth client ID |
| `CLICKUP_CLIENT_SECRET` | ClickUp OAuth client secret; ClickUp also uses a per-installation webhook secret stored encrypted by Factorize |
| `CREDENTIAL_ENCRYPTION_KEY` | Base64-encoded 32-byte key for encrypted credentials |
| `SESSION_SIGNING_SECRET` | Independent secret for signed browser sessions |
| `APP_ORIGIN` | Public Worker origin, configured in `packages/app/wrangler.jsonc` |
Expand Down
2 changes: 2 additions & 0 deletions packages/app/.dev.vars.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ LINEAR_CLIENT_ID=
LINEAR_CLIENT_SECRET=
# Copy this once from the Webhooks section of the Linear OAuth app settings.
LINEAR_WEBHOOK_SIGNING_SECRET=
CLICKUP_CLIENT_ID=
CLICKUP_CLIENT_SECRET=
# base64-encoded 32 random bytes: openssl rand -base64 32
CREDENTIAL_ENCRYPTION_KEY=
# a separate random value for signed browser sessions
Expand Down
22 changes: 22 additions & 0 deletions packages/app/src/clickup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { MatchRule } from "./types";

export type ClickUpTask = Record<string, any> & { id?: string; list?: { id?: string }; status?: { status?: string }; assignees?: Array<{ id?: number | string }>; tags?: Array<{ name?: string }>; creator?: { id?: number | string } };

export function matchingClickUpTask(task: ClickUpTask, listId: string, rules: MatchRule[]): boolean {
if (!task.id || String(task.list?.id ?? "") !== listId || !rules.length) return false;
return rules.every(rule => {
if (rule.type === "status") return task.status?.status === rule.targetId;
if (rule.type === "label") return task.tags?.some(tag => tag.name === rule.targetId) === true;
if (rule.type === "creator") return String(task.creator?.id ?? "") === rule.targetId;
return task.assignees?.some(person => String(person.id) === rule.targetId) === true;
});
}

export const clickUpHeaders = (token: string) => ({ Authorization: token, "Content-Type": "application/json" });

export async function clickUpJson(token: string, path: string): Promise<any> {
const response = await fetch(`https://api.clickup.com/api/v2${path}`, { headers: clickUpHeaders(token) });
const body = await response.json().catch(() => ({})) as any;
if (!response.ok) throw new Error(body.err ?? body.error ?? `ClickUp request failed (${response.status})`);
return body;
}
1 change: 1 addition & 0 deletions packages/app/src/flow-schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const runIdSchema = z.object({ runId: z.string().min(1) });
const webhookCommonSchema = z.object({ handlerCode: z.string().max(16_384).optional() });
export const webhookTriggerConfigSchema = z.discriminatedUnion("provider", [
webhookCommonSchema.extend({ provider: z.literal("linear"), projectId: z.string().min(1), matchRules: z.array(matchRuleSchema).min(1) }).strict(),
webhookCommonSchema.extend({ provider: z.literal("clickup"), listId: z.string().min(1), matchRules: z.array(matchRuleSchema).min(1) }).strict(),
webhookCommonSchema.extend({ provider: z.literal("github"), installationId: z.number().int().positive(), repositoryId: z.number().int().positive(), event: z.string().min(1).optional(), action: z.string().min(1).optional(), matchRules: z.array(matchRuleSchema).min(1).optional() }).strict().refine(value => Boolean(value.matchRules?.length || (value.event && value.action)), { message: "GitHub triggers require issue matching rules or an event and action" }),
webhookCommonSchema.extend({ provider: z.literal("cloudflareTail"), integrationId: z.string().min(1) }).strict(),
]);
Expand Down
44 changes: 44 additions & 0 deletions packages/app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,35 @@ app.post("/auth/logout", (c) => {
return c.redirect("/");
});

app.get("/auth/clickup", async (c) => {
const session = await owner(c); if (!session) return c.redirect("/auth/linear");
if (!c.env.CLICKUP_CLIENT_ID) return c.text("ClickUp OAuth is not configured", 503);
const state = await signSession({ ...session, exp: Math.floor(Date.now() / 1000) + 600 }, c.env.SESSION_SIGNING_SECRET);
setCookie(c, "clickup_oauth_state", state, { httpOnly: true, secure: new URL(c.env.APP_ORIGIN).protocol === "https:", sameSite: "Lax", path: "/auth/clickup", maxAge: 600 });
const redirect = new URL("https://app.clickup.com/api");
redirect.searchParams.set("client_id", c.env.CLICKUP_CLIENT_ID);
redirect.searchParams.set("redirect_uri", `${c.env.APP_ORIGIN}/auth/clickup/callback`);
redirect.searchParams.set("state", state);
return c.redirect(redirect.toString());
});

app.get("/auth/clickup/callback", async (c) => {
const session = await owner(c), code = c.req.query("code"), state = c.req.query("state");
if (!session || !code || !state || state !== getCookie(c, "clickup_oauth_state") || !c.env.CLICKUP_CLIENT_ID || !c.env.CLICKUP_CLIENT_SECRET) return c.text("Invalid ClickUp OAuth state", 400);
const tokenResponse = await fetch("https://api.clickup.com/api/v2/oauth/token", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ client_id: c.env.CLICKUP_CLIENT_ID, client_secret: c.env.CLICKUP_CLIENT_SECRET, code }) });
const tokens = await tokenResponse.json() as { access_token?: string };
if (!tokenResponse.ok || !tokens.access_token) return c.text("ClickUp token exchange failed", 502);
const teamsResponse = await fetch("https://api.clickup.com/api/v2/team", { headers: { Authorization: tokens.access_token } });
const teams = await teamsResponse.json() as any, team = teams.teams?.[0];
if (!teamsResponse.ok || !team?.id) return c.text("Could not identify a ClickUp workspace", 502);
const hookResponse = await fetch(`https://api.clickup.com/api/v2/team/${encodeURIComponent(team.id)}/webhook`, { method: "POST", headers: { Authorization: tokens.access_token, "Content-Type": "application/json" }, body: JSON.stringify({ endpoint: `${c.env.APP_ORIGIN}/webhooks/clickup/${encodeURIComponent(session.tenantId)}`, events: ["taskCreated", "taskUpdated", "taskStatusUpdated", "taskAssigneeUpdated", "taskTagUpdated", "taskMoved"] }) });
const hook = await hookResponse.json() as any;
if (!hookResponse.ok || !hook.id) return c.text("Could not register the ClickUp webhook", 502);
await tenant(c, session.tenantId).fetch("https://tenant/connections/clickup", { method: "PUT", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ accessToken: tokens.access_token, teamId: String(team.id), teamName: team.name, webhookId: String(hook.id), webhookSecret: hook.secret }) });
deleteCookie(c, "clickup_oauth_state", { path: "/auth/clickup" });
return c.redirect("/settings/integrations");
});

app.get("/api/connections/status", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/connections/status");
Expand Down Expand Up @@ -173,6 +202,14 @@ app.get("/api/linear/options", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/linear/options");
});
app.get("/api/clickup/lists", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/clickup/lists");
});
app.get("/api/clickup/options", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch(`https://tenant/clickup/options?listId=${encodeURIComponent(c.req.query("listId") ?? "")}`);
});
app.get("/api/github/installations", async (c) => {
const session = await owner(c); if (!session) return c.json({ error: "Unauthorized" }, 401);
return tenant(c, session.tenantId).fetch("https://tenant/github/installations");
Expand Down Expand Up @@ -255,6 +292,13 @@ app.post("/webhooks/linear", async (c) => {
return tenant(c, organizationId).fetch("https://tenant/webhook/linear", { method: "POST", headers: { "Content-Type": "application/json", "Linear-Delivery": `linear:${deliveryId}` }, body: raw });
});

app.post("/webhooks/clickup/:tenantId", async (c) => {
const raw = await c.req.text();
if (new TextEncoder().encode(raw).byteLength > 262_144) return c.text("Payload too large", 413);
try { JSON.parse(raw); } catch { return c.text("Invalid JSON", 400); }
return tenant(c, c.req.param("tenantId")).fetch("https://tenant/webhook/clickup", { method: "POST", headers: { "Content-Type": "application/json", "X-Signature": c.req.header("X-Signature") ?? "" }, body: raw });
});

app.post("/webhooks/github", async (c) => {
if (!c.env.GITHUB_WEBHOOK_SECRET) return c.text("Webhook verification is not configured", 503);
const raw = await c.req.text();
Expand Down
Loading