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
10 changes: 3 additions & 7 deletions packages/app/src/matcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { FilterType, MatchRule } from "./types";
import type { MatchRule } from "./types";

type IssueReference = { id?: string; project?: { id?: string }; state?: { id?: string }; assignee?: { id?: string }; creator?: { id?: string }; owner?: { id?: string }; labels?: Array<{ id?: string }> };
type EventData = {
Expand All @@ -16,11 +16,7 @@ type EventData = {
};
type LinearEvent = { type?: string; action?: string; data?: EventData; updatedFrom?: { stateId?: string; assigneeId?: string; creatorId?: string; ownerId?: string; labelIds?: string[]; [key: string]: unknown } };

export type FlowMatcher = { projectId: string; matchRules?: MatchRule[]; filterType?: FilterType; filterTargetId?: string };

const rulesFor = (flow: FlowMatcher): MatchRule[] => flow.matchRules?.length
? flow.matchRules
: flow.filterType && flow.filterTargetId ? [{ type: flow.filterType, targetId: flow.filterTargetId }] : [];
export type FlowMatcher = { projectId: string; matchRules: MatchRule[] };

/** Returns the issue to enqueue only when a webhook is relevant to a flow. */
export function matchingIssue(event: LinearEvent, flow: FlowMatcher): string | null {
Expand All @@ -29,7 +25,7 @@ export function matchingIssue(event: LinearEvent, flow: FlowMatcher): string | n
const data = { ...(event.data?.issue ?? {}), ...(event.data ?? {}) } as EventData;
const issueId = data.issueId ?? data.issue?.id ?? (event.type === "Issue" ? data.id : undefined);
const projectId = data.project?.id ?? data.issue?.project?.id;
const rules = rulesFor(flow);
const rules = flow.matchRules;
if (!issueId || projectId !== flow.projectId || !rules.length) return null;

// A flow starts on issue creation, or when a rule changes and the issue now
Expand Down
8 changes: 2 additions & 6 deletions packages/app/src/tenant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1579,19 +1579,15 @@ export class Tenant extends DurableObject<Env> {
}

private matchRules(input: PipeInput): MatchRule[] {
const rules = Array.isArray(input.matchRules) && input.matchRules.length ? input.matchRules : input.filterType && input.filterTargetId ? [{ type: input.filterType, targetId: input.filterTargetId }] : [];
const rules = input.matchRules;
const validTypes: FilterType[] = ["owner", "creator", "status", "label", "assignee"];
if (!rules.length) throw new Error("Configure at least one issue matching rule.");
if (!rules.every((rule) => rule && validTypes.includes(rule.type) && typeof rule.targetId === "string" && rule.targetId.trim())) throw new Error("Each matching rule needs a supported type and target.");
return rules.map((rule) => ({ type: rule.type, targetId: rule.targetId.trim() }));
}

private savedMatchRules(pipe: Row): MatchRule[] {
try {
const parsed = JSON.parse(String(pipe.match_rules || "[]"));
if (Array.isArray(parsed) && parsed.length) return this.matchRules({ name: "", projectId: "", matchRules: parsed, maxConcurrency: 1 });
} catch { /* Pre-migration flows fall back to their legacy single rule. */ }
return this.matchRules({ name: "", projectId: "", filterType: pipe.filter_type as FilterType, filterTargetId: String(pipe.filter_target_id), maxConcurrency: 1 });
return this.matchRules({ name: "", projectId: "", matchRules: JSON.parse(String(pipe.match_rules)), maxConcurrency: 1 });
}

private nextRunSlot(pipe: Row, runId: string): number {
Expand Down
5 changes: 1 addition & 4 deletions packages/app/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ export interface PipeInput {
flowId?: string;
projectId?: string;
/** At least one rule is required. All rules must match the issue. */
matchRules?: MatchRule[];
/** Legacy single-rule input, retained so existing API clients keep working. */
filterType?: FilterType;
filterTargetId?: string;
matchRules: MatchRule[];
maxConcurrency: number;
workspaceName?: string;
/** Mustache template rendered with the Linear webhook payload for each run. */
Expand Down
Loading