Skip to content

fix(riff-raff-generator): Correctly validate stacks#2870

Open
akash1810 wants to merge 5 commits intomainfrom
aa/riff-raff-validation
Open

fix(riff-raff-generator): Correctly validate stacks#2870
akash1810 wants to merge 5 commits intomainfrom
aa/riff-raff-validation

Conversation

@akash1810
Copy link
Member

@akash1810 akash1810 commented Mar 25, 2026

Note

Recommended to review commit by commit as I've made some refactors along the way to improve readability.

What does this change?

This change validates more scenarios when instantiating a RiffRaffYamlFile to generate riff-raff.yaml. Given the below set of stacks, an Error will now be thrown as there is a missing instance of MyDatabaseStack for CODE.

new MyApplicationStack(app, "App-CODE-deploy", {
  env: {
    region: "eu-west-1",
  },
  stack: "deploy",
  stage: "CODE",
});
new MyApplicationStack(app, "App-PROD-deploy", {
  env: {
    region: "eu-west-1",
  },
  stack: "deploy",
  stage: "PROD",
});

new MyDatabaseStack(app, "Database-PROD-deploy", {
  env: {
    region: "eu-west-1",
  },
  stack: "deploy",
  stage: "PROD",
});

Previously, the validation would incorrectly pass, generating the below riff-raff.yaml.

Invalid `riff-raff.yaml`

allowedStages:
  - CODE
  - PROD
deployments:
  cfn-eu-west-1-deploy-my-application-stack:
    type: cloud-formation
    regions:
      - eu-west-1
    stacks:
      - deploy
    app: my-application-stack
    contentDirectory: /private/var/folders/0_/pvjwppsx5cl19t4n6_rmm_y80000gp/T/cdk.out9xIUJu
    parameters:
      templateStagePaths:
        CODE: App-CODE-deploy.template.json
        PROD: App-PROD-deploy.template.json
  cfn-eu-west-1-deploy-my-database-stack:
    type: cloud-formation
    regions:
      - eu-west-1
    stacks:
      - deploy
    app: my-database-stack
    contentDirectory: /private/var/folders/0_/pvjwppsx5cl19t4n6_rmm_y80000gp/T/cdk.out9xIUJu
    parameters:
      templateStagePaths:
        PROD: Database-PROD-deploy.template.json

How to test

See the added unit tests.

How can we measure success?

Correct validation when generating a riff-raff.yaml file.

Have we considered potential risks?

There are no changes to the public API, so I don't think there are any risks.

Checklist

  • I have listed any breaking changes, along with a migration path 1
  • I have updated the documentation as required for the described changes 2

Footnotes

  1. Consider whether this is something that will mean changes to projects that have already been migrated, or to the CDK CLI tool. If changes are required, consider adding a checklist here and/or linking to related PRs.

  2. If you are adding a new construct or pattern, has new documentation been added? If you are amending defaults or changing behaviour, are the existing docs still valid?

These tests are failing, demonstrating a fault in the current logic.
…adability

Removal of the `GroupedCdkStacks` type decreases complexity.
@akash1810 akash1810 added the fix Departmental tracking: fix label Mar 25, 2026
@changeset-bot
Copy link

changeset-bot bot commented Mar 25, 2026

🦋 Changeset detected

Latest commit: 1824bae

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@guardian/cdk Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@akash1810 akash1810 force-pushed the aa/riff-raff-validation branch from de660ea to a86b323 Compare March 25, 2026 21:54
@akash1810 akash1810 force-pushed the aa/riff-raff-validation branch from a86b323 to 1824bae Compare March 25, 2026 22:10

expect(actual).toMatchInlineSnapshot(`
"allowedStages:
- INFRA
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, this test (added in #2094) was incorrect!

@akash1810 akash1810 marked this pull request as ready for review March 25, 2026 22:19
@akash1810 akash1810 requested a review from a team as a code owner March 25, 2026 22:19
@akash1810 akash1810 requested a review from Copilot March 26, 2026 05:37
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Improves RiffRaffYamlFile validation so riff-raff.yaml generation fails when a stack “identity” (class name + stack tag + region) is missing required stage instances, preventing invalid templateStagePaths from being produced.

Changes:

  • Refactors stack grouping to validate per (className, stackTag, region) and records missing stage definitions.
  • Extracts grouping helpers and updates RiffRaffYamlFile to use them.
  • Adds/updates unit tests and includes a changeset entry documenting the behavior change.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/riff-raff-yaml-file/index.ts Refactors grouping/validation logic; adds missing-stack detection and region validation helper.
src/riff-raff-yaml-file/group-by.ts Exposes grouping helpers and removes the previous combined grouping function.
src/riff-raff-yaml-file/types.ts Removes the GroupedCdkStacks exported type alias.
src/riff-raff-yaml-file/index.test.ts Updates snapshots and adds targeted validation tests for missing definitions/unresolved regions.
.changeset/silent-planets-invent.md Documents the validation fix as a patch changeset.
Comments suppressed due to low confidence (1)

src/riff-raff-yaml-file/group-by.ts:20

  • groupByClassNameStackRegionStage was removed. Since this file is importable via deep paths under @guardian/cdk/lib/..., removing an exported function can be a breaking change for downstream consumers. Consider keeping a compatibility export that delegates to the new groupByClassName/groupByStackTag/groupByRegion/groupByStageTag helpers (or add a deprecation period).
export function groupByClassName(cdkStacks: GuStack[]): Record<ClassName, GuStack[]> {
  return groupBy(cdkStacks, (stack) => stack.constructor.name);
}

export function groupByStackTag(cdkStacks: GuStack[]): Record<StackTag, GuStack[]> {
  return groupBy(cdkStacks, ({ stack }) => stack);
}

export function groupByStageTag(cdkStacks: GuStack[]): Record<StageTag, GuStack[]> {
  return groupBy(cdkStacks, ({ stage }) => stage);
}

export function groupByRegion(cdkStacks: GuStack[]): Record<Region, GuStack[]> {
  return groupBy(cdkStacks, ({ region }) => region);
}


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Departmental tracking: fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants