name: 'CDK Diff Report'
description: 'Run cdk diff and post a summary of added, modified, removed, and replaced AWS resources to your GitHub PR. Free and open source.'
author: 'dzon337'

branding:
  icon: 'git-pull-request'
  color: 'orange'

inputs:
  cdk-args:
    description: 'Arguments forwarded to cdk diff (comma-separated). Example: "--all" or "MyStack,OtherStack"'
    required: false
    default: '--all'
  html-output:
    description: 'Path to write a standalone HTML report (relative to working-directory). Leave empty to skip.'
    required: false
    default: ''
  dry-run:
    description: 'If "true", preview the markdown in the log without posting to the PR.'
    required: false
    default: 'false'
  github-token:
    description: 'Token used to post the PR comment. Defaults to the workflow token; needs the pull-requests: write permission.'
    required: false
    default: ${{ github.token }}
  version:
    description: 'cdk-diff-report CLI version to install. Defaults to the version pinned by this action release; set "latest" to always track npm.'
    required: false
    default: ''
  api-key:
    description: 'IaCReview API key. When set, enables cost, security, policy, and multi-environment analysis. Get one at https://iacreview.com'
    required: false
    default: ''
  api-url:
    description: 'IaCReview API base URL. Only set this for a self-hosted IaCReview deployment.'
    required: false
    default: ''
  environment:
    description: 'IaCReview: run only this configured environment from .cdkdiffreportrc (staged pipelines). Leave empty to run all.'
    required: false
    default: ''
  fail-on-block:
    description: 'IaCReview: if "true", exit non-zero when the merge verdict is BLOCKED.'
    required: false
    default: 'false'
  working-directory:
    description: 'Directory containing cdk.json. Defaults to the repository root.'
    required: false
    default: '.'
  node-version:
    description: 'Node.js version to use. Must be >= 18.'
    required: false
    default: '20'

runs:
  using: 'composite'
  steps:
    - name: Set up Node.js
      uses: actions/setup-node@v4
      with:
        node-version: ${{ inputs.node-version }}

    - name: Install cdk-diff-report
      shell: bash
      run: |
        # Pin the CLI to this action release's version so `uses: ...@v1.2.0`
        # actually means CLI 1.2.0 — unless the caller overrides `version`.
        VERSION="${{ inputs.version }}"
        if [ -z "$VERSION" ]; then
          VERSION=$(node -p "require('${{ github.action_path }}/package.json').version")
        fi
        echo "Installing cdk-diff-report@$VERSION"
        npm install -g "cdk-diff-report@$VERSION"

    - name: Run CDK Diff Report
      shell: bash
      working-directory: ${{ inputs.working-directory }}
      env:
        GITHUB_TOKEN: ${{ inputs.github-token }}
        CDK_DIFF_PLATFORM: github
        CDK_DIFF_CDK_ARGS: ${{ inputs.cdk-args }}
        CDK_DIFF_HTML_OUTPUT: ${{ inputs.html-output }}
        CDK_DIFF_DRY_RUN: ${{ inputs.dry-run }}
        CDK_DIFF_ENVIRONMENT: ${{ inputs.environment }}
        CDK_DIFF_FAIL_ON_BLOCK: ${{ inputs.fail-on-block }}
        IACREVIEW_API_KEY: ${{ inputs.api-key }}
        IACREVIEW_API_URL: ${{ inputs.api-url }}
      run: cdk-diff-report
