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
2 changes: 2 additions & 0 deletions API.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 16 additions & 3 deletions src/custom-resource-provider/handler.lambda.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export const handler = async (event: OnEventRequest): Promise<OnEventResponse |

console.log("Payload: %j", event);

const call: GithubApiCall | undefined = parse(event.ResourceProperties[event.RequestType]);
const call: GithubApiCall | undefined = parse(
event.ResourceProperties[event.RequestType],
"PhysicalResourceId" in event ? event.PhysicalResourceId : undefined,
);

if (!call) {
return;
Expand Down Expand Up @@ -75,13 +78,23 @@ export const handler = async (event: OnEventRequest): Promise<OnEventResponse |
return;
};

/**
* @see https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/custom-resources/lib/aws-custom-resource/runtime/shared.ts
*/
const PHYSICAL_RESOURCE_ID_REFERENCE = "PHYSICAL:RESOURCEID:";

/**
* Parse the GitHub API call passed in ResourceProperties OnCreate, OnUpdate and OnDelete
*
* @internal
*/
const parse = (stringified?: string): GithubApiCall | undefined => {
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,
);
};

/**
Expand Down
2 changes: 2 additions & 0 deletions src/github-custom-resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion test/__snapshots__/github-custom-resource.test.ts.snap

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 27 additions & 1 deletion test/custom-resource-provider/handler.lambda.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down