Skip to content

Commit 5cb34be

Browse files
committed
feat: merge resolved value into context
1 parent db040a8 commit 5cb34be

File tree

3 files changed

+34
-16
lines changed

3 files changed

+34
-16
lines changed

core/actions/_lib/interpolationContext.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,23 @@ export type InterpolationContextBase = {
2525

2626
export type InterpolationContextWithPub = InterpolationContextBase & {
2727
pub: ReturnType<typeof createPubProxy>
28+
json?: Json
2829
}
2930

3031
export type InterpolationContextWithJson = InterpolationContextBase & {
3132
json: Json
33+
pub?: ReturnType<typeof createPubProxy>
3234
}
3335

34-
export type InterpolationContext = InterpolationContextWithPub | InterpolationContextWithJson
36+
export type InterpolationContextWithBoth = InterpolationContextBase & {
37+
pub: ReturnType<typeof createPubProxy>
38+
json: Json
39+
}
40+
41+
export type InterpolationContext =
42+
| InterpolationContextWithPub
43+
| InterpolationContextWithJson
44+
| InterpolationContextWithBoth
3545

3646
type InterpolationCommunity = Pick<Communities, "id" | "name" | "slug" | "avatar">
3747

@@ -67,7 +77,7 @@ type BuildInterpolationContextArgs =
6777
} & (
6878
| {
6979
pub: ProcessedPub
70-
json?: never
80+
json?: Json
7181
}
7282
| {
7383
pub?: never
@@ -82,7 +92,7 @@ type BuildInterpolationContextArgs =
8292
} & (
8393
| {
8494
pub: ProcessedPub
85-
json?: never
95+
json?: Json
8696
}
8797
| {
8898
pub?: never
@@ -144,6 +154,15 @@ export function buildInterpolationContext(
144154
: null,
145155
}
146156

157+
if (args.pub && args.json !== undefined) {
158+
// Both pub and json provided
159+
return {
160+
...baseContext,
161+
pub: createPubProxy(args.pub, args.community.slug),
162+
json: args.json,
163+
}
164+
}
165+
147166
if (args.pub) {
148167
return {
149168
...baseContext,

core/actions/_lib/resolveAutomationInput.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import type { ProcessedPub } from "contracts"
22
import type { CommunitiesId, PubsId } from "db/public"
33
import type { FullAutomation, Json } from "db/types"
4+
import type { InterpolationContext } from "./interpolationContext"
45

56
import { interpolate } from "@pubpub/json-interpolate"
67
import { logger } from "logger"
78
import { tryCatch } from "utils/try-catch"
89

910
import { db } from "~/kysely/database"
1011
import { getPubsWithRelatedValues } from "~/lib/server"
11-
import type { InterpolationContext } from "./interpolationContext"
1212

1313
type ResolvedPub = ProcessedPub<{
1414
withPubType: true
@@ -158,13 +158,7 @@ async function findPubByFieldValue(
158158
return pub as ResolvedPub
159159
}
160160

161-
/**
162-
* Finds a pub by its ID.
163-
*/
164-
async function findPubById(
165-
communityId: CommunitiesId,
166-
pubId: PubsId
167-
): Promise<ResolvedPub | null> {
161+
async function findPubById(communityId: CommunitiesId, pubId: PubsId): Promise<ResolvedPub | null> {
168162
const [error, pub] = await tryCatch(
169163
getPubsWithRelatedValues(
170164
{ pubId, communityId },

core/actions/_lib/runAutomation.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ const runActionInstance = async (args: RunActionInstanceArgs): Promise<ActionIns
422422
},
423423
automationRun: args.automationRun,
424424
user: args.user ?? null,
425-
...(pub ? { pub } : { json: args.json ?? ({} as Json) }),
425+
...(pub ? { pub, json: args.json } : { json: args.json ?? ({} as Json) }),
426426
})
427427

428428
const interpolated = await actionConfigBuilder.interpolate(interpolationData)
@@ -579,6 +579,9 @@ export async function runAutomation(
579579

580580
// resolve automation input if a resolver is configured
581581
// this allows the automation to operate on a different pub or transformed json
582+
// resolved values are additive - they add to or overwrite existing context:
583+
// - JSON resolved: adds json to context (or overwrites existing json)
584+
// - Pub resolved: adds pub to context (or overwrites existing pub)
582585
let resolvedPub = pub
583586
let resolvedJson = args.json
584587

@@ -593,7 +596,7 @@ export async function runAutomation(
593596
id: args.scheduledAutomationRunId ?? ("pending-resolver" as AutomationRunsId),
594597
},
595598
user: args.user ?? null,
596-
...(pub ? { pub } : { json: args.json ?? ({} as Json) }),
599+
...(pub ? { pub, json: args.json } : { json: args.json ?? ({} as Json) }),
597600
})
598601

599602
const resolved = await resolveAutomationInput(
@@ -604,18 +607,20 @@ export async function runAutomation(
604607
)
605608

606609
if (resolved.type === "pub") {
610+
// Pub resolved: overwrites existing pub, keeps existing json
607611
resolvedPub = resolved.pub
608-
resolvedJson = undefined
609-
logger.debug("Resolver resolved to a different pub", {
612+
logger.debug("Resolver resolved to a pub", {
610613
automationId: automation.id,
611614
originalPubId: pub?.id,
612615
resolvedPubId: resolved.pub.id,
616+
hasExistingJson: resolvedJson !== undefined,
613617
})
614618
} else if (resolved.type === "json") {
619+
// JSON resolved: overwrites existing json, keeps existing pub
615620
resolvedJson = resolved.json
616-
resolvedPub = null
617621
logger.debug("Resolver resolved to JSON", {
618622
automationId: automation.id,
623+
hasExistingPub: resolvedPub !== null,
619624
})
620625
}
621626
// if type is "unchanged", keep the original pub/json

0 commit comments

Comments
 (0)