From b6fa8f4146979324f21a06cfec7080e4abbda18f Mon Sep 17 00:00:00 2001 From: Patrick Florek Date: Thu, 26 Nov 2026 17:40:13 +0100 Subject: [PATCH] feat(custom-resource): replace physical resource id reference in api call parameters closes #6 --- API.md | 2 ++ .../handler.lambda.ts | 19 +++++++++++-- src/github-custom-resource.ts | 2 ++ .../github-custom-resource.test.ts.snap | 2 +- .../handler.lambda.test.ts | 28 ++++++++++++++++++- 5 files changed, 48 insertions(+), 5 deletions(-) diff --git a/API.md b/API.md index 38ba025b..05bb2fb8 100644 --- a/API.md +++ b/API.md @@ -521,6 +521,8 @@ public readonly parameters: any; The parameters for the service action. +Use `new custom_resources.PhysicalResourceIdReference()` to pass the current physical resource id in `onUpdate` or `onDelete`. + > [https://github.com/octokit/rest.js](https://github.com/octokit/rest.js) --- diff --git a/src/custom-resource-provider/handler.lambda.ts b/src/custom-resource-provider/handler.lambda.ts index 3cbfed51..271abed8 100644 --- a/src/custom-resource-provider/handler.lambda.ts +++ b/src/custom-resource-provider/handler.lambda.ts @@ -17,7 +17,10 @@ export const handler = async (event: OnEventRequest): Promise { - return stringified ? JSON.parse(stringified) : undefined; +export const parse = (stringified?: string, physicalResourceId?: string): GithubApiCall | undefined => { + if (!stringified) { + return undefined; + } + return JSON.parse(stringified, (_key, value) => + PHYSICAL_RESOURCE_ID_REFERENCE === value && physicalResourceId ? physicalResourceId : value, + ); }; /** diff --git a/src/github-custom-resource.ts b/src/github-custom-resource.ts index b35565f1..5ffe5b4a 100644 --- a/src/github-custom-resource.ts +++ b/src/github-custom-resource.ts @@ -20,6 +20,8 @@ export interface GithubApiCall { /** * The parameters for the service action. * + * Use `new custom_resources.PhysicalResourceIdReference()` to pass the current physical resource id in `onUpdate` or `onDelete`. + * * @see https://github.com/octokit/rest.js */ readonly parameters?: any; diff --git a/test/__snapshots__/github-custom-resource.test.ts.snap b/test/__snapshots__/github-custom-resource.test.ts.snap index 8ad79990..4e42dda3 100644 --- a/test/__snapshots__/github-custom-resource.test.ts.snap +++ b/test/__snapshots__/github-custom-resource.test.ts.snap @@ -39,7 +39,7 @@ exports[`GithubCustomResource Should match snapshot 1`] = ` "S3Bucket": { "Fn::Sub": "cdk-hnb659fds-assets-\${AWS::AccountId}-\${AWS::Region}", }, - "S3Key": "4c630c0046183c5fdb3559db8d437ca2451a7ebb67e241bd6f160b95dda51bbe.zip", + "S3Key": "7378a0c80cd9353b3fe160f044fffa621deb706c9781a07a2b15951e899ef5dd.zip", }, "Description": "src/custom-resource-provider/handler.lambda.ts", "Environment": { diff --git a/test/custom-resource-provider/handler.lambda.test.ts b/test/custom-resource-provider/handler.lambda.test.ts index ac3bcbf6..09c67031 100644 --- a/test/custom-resource-provider/handler.lambda.test.ts +++ b/test/custom-resource-provider/handler.lambda.test.ts @@ -1,6 +1,32 @@ -import { filter, flatten } from "../../src/custom-resource-provider/handler.lambda"; +import { filter, flatten, parse } from "../../src/custom-resource-provider/handler.lambda"; describe("handler", () => { + describe("parse", () => { + it("Should replace the physical resource id reference", () => { + // Given + const call = JSON.stringify({ + endpoint: "repos", + method: "update", + parameters: { owner: "pepperize", repo: "PHYSICAL:RESOURCEID:" }, + }); + + // When + const result = parse(call, "cdk-github"); + + // Then + expect(result?.parameters).toEqual({ owner: "pepperize", repo: "cdk-github" }); + }); + it("Should keep the physical resource id reference without a physical resource id", () => { + // Given + const call = JSON.stringify({ endpoint: "repos", method: "get", parameters: { repo: "PHYSICAL:RESOURCEID:" } }); + + // When + const result = parse(call, undefined); + + // Then + expect(result?.parameters).toEqual({ repo: "PHYSICAL:RESOURCEID:" }); + }); + }); describe("filter", () => { it("Should filter single object key", () => { // When