Skip to content
Closed
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
7 changes: 7 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ for `STATE_IAC_CACHE_SECONDS` and states by object version, so an unchanged stat
`?refresh=1` skips the cache. States over `STATE_IAC_MAX_STATE_MB` fail with `too_large`. In e2e the
dev server reads the fixtures through a local S3 endpoint (`e2e/fixtures/fake-s3.ts`, seeded backend
`e2e-shared-state`: `acme-infra` → primary project, `acme-platform` → secondary project).
History and diff: `stateIacService.history()` pages `*.history.json` (`project:stateiac:history:read`);
`diff()` compares two snapshot sources (`current`, `history:<ns>`, `backup:<ns>`, `retained:<ns>`,
`previous`, `version:<object version>` from `ObjectStore.listVersions`) with `diffSnapshots()` in
`application/state-diff.service.ts` (`project:stateiac:states:read`), by default the last update
against the one before. Two secrets always compare equal (Pulumi re-encrypts them on every write);
provider-only changes are flagged `providerOnly`. Past sources are cached for good, the current one
by object version. Periodic scans and `state_iac.stack.*` events are not built (ADR 0003, deferred).

## Domain events

Expand Down
57 changes: 57 additions & 0 deletions e2e/specs/state-iac-stacks.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,60 @@ test.describe('state iac stacks — organization view', () => {
await expectDenied(page, `/org/${org}/state-iac`);
});
});

test.describe('state iac stacks — history and changes', () => {
test('the history lists the updates and opens what changed in one', async ({ page, loginAs }) => {
await loginAs('projectAdmin');
await expectAllowed(page, `${projectA}/${backend}/acme-infra/prod`);
await page.waitForLoadState('networkidle');
const tabs = page.getByRole('tablist', { name: 'Contenido del stack' });

await tabs.getByRole('tab', { name: 'Historial' }).click();
const history = page.getByTestId('stack-history');
await expect(history.locator('li[data-update-id]')).toHaveCount(4);
await expect(history.locator('li[data-update-id="1789061079158069310"]')).toContainText(
'fallido',
);
// allow-listed environment: commit and author, never the email
await expect(history).toContainText('Jane Doe');
await expect(history).not.toContainText('jane@example.com');

await history
.locator('li[data-update-id="1795604216162649000"]')
.getByRole('button', { name: 'Ver cambios' })
.click();
const diff = page.getByTestId('stack-diff');
await expect(diff.getByTestId('diff-summary')).toContainText('Cambios 2');
const sql = diff.locator('li[data-resource="cloud-sql-acme-prod"]');
await expect(sql).toHaveAttribute('data-change', 'update');
await sql.getByRole('button').first().click();
await expect(sql).toContainText('inputs.publicIp');
});

test('a replace between two chosen versions', async ({ page, loginAs }) => {
await loginAs('projectAdmin');
await expectAllowed(page, `${projectA}/${backend}/acme-infra/prod`);
await page.waitForLoadState('networkidle');
await page
.getByRole('tablist', { name: 'Contenido del stack' })
.getByRole('tab', { name: 'Cambios' })
.click();
const diff = page.getByTestId('stack-diff');
await expect(diff.getByTestId('diff-summary')).toBeVisible();
await diff.getByLabel('Hasta').selectOption('history:1795602803296701000');
await expect(diff.locator('li[data-resource="lb-prod-cert"]')).toHaveAttribute(
'data-change',
'replace',
);
});

test('without history:read nor states:read there is no history nor diff', async ({
page,
loginAs,
}) => {
await loginAs('stateiacStacksOnly');
await expectAllowed(page, `${projectA}/${backend}/acme-infra/prod`);
await expect(page.getByRole('tab', { name: 'Historial' })).toHaveCount(0);
await expect(page.getByRole('tab', { name: 'Cambios' })).toHaveCount(0);
});
});
64 changes: 52 additions & 12 deletions src/lib/components/state-iac/StackDetail.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,45 @@
import { Drawer, Tabs } from '#lib/components/ui';
import { _ } from '#lib/i18n';
import type { IacResourceView, StackDetailView } from '#lib/state-iac/types';
import { untrack } from 'svelte';
import IacValue from './IacValue.svelte';
import StackDiff from './StackDiff.svelte';
import StackHistory from './StackHistory.svelte';

type Props = {
detail: StackDetailView;
/** Sin `project:stateiac:states:read` solo se ve el resumen. */
canReadStates: boolean;
/** Historial de updates (`project:stateiac:history:read`). */
canReadHistory?: boolean;
backHref: string;
projectName?: string | null;
/** `/api/state-iac/stacks/<backend>/<project>/<stack>` y `projectId=…|organizationId=…` */
apiBase: string;
scopeQuery: string;
};
let { detail, canReadStates, backHref, projectName = null }: Props = $props();
let {
detail,
canReadStates,
canReadHistory = false,
backHref,
projectName = null,
apiBase,
scopeQuery,
}: Props = $props();

const STACK_TYPE = 'pulumi:pulumi:Stack';
const resources = $derived(detail.resources ?? []);
const byAddress = $derived(new Map(resources.map((r) => [r.address, r])));

let tab = $state('resources');
let tab = $state(untrack(() => (canReadStates ? 'resources' : 'history')));
/** Version picked in the history for the changes tab. */
let changesTo = $state<string | null>(null);

function showChanges(updateId: string) {
changesTo = `history:${updateId}`;
tab = 'changes';
}
let query = $state('');
let typeFilter = $state('');
let providerFilter = $state('');
Expand Down Expand Up @@ -81,6 +104,18 @@
});
const outputs = $derived(Object.entries(detail.outputs ?? {}));

// what this viewer may open: resources/outputs/changes need states:read, the history its own grant
const tabs = $derived([
...(canReadStates
? [
{ value: 'resources', label: $_('stateIac.stacks.resources'), count: resources.length },
{ value: 'outputs', label: $_('stateIac.detail.outputs'), count: outputs.length },
]
: []),
...(canReadHistory ? [{ value: 'history', label: $_('stateIac.history.title') }] : []),
...(canReadStates ? [{ value: 'changes', label: $_('stateIac.diff.title') }] : []),
]);

function select(address: string) {
const resource = byAddress.get(address);
if (!resource) return;
Expand Down Expand Up @@ -170,23 +205,15 @@
<p class="k-card border-danger/30 px-4 py-3 text-sm text-red-300" role="alert">
{$_(`stateIac.errors.${summary.error}`)}
</p>
{:else if !canReadStates}
{:else if tabs.length === 0}
<p
class="k-card border-dashed px-4 py-6 text-center text-sm text-fg-muted"
data-testid="states-forbidden"
>
{$_('stateIac.detail.noStatesPermission')}
</p>
{:else}
<Tabs
label={$_('stateIac.detail.tabs')}
bind:value={tab}
idPrefix="stack"
items={[
{ value: 'resources', label: $_('stateIac.stacks.resources'), count: resources.length },
{ value: 'outputs', label: $_('stateIac.detail.outputs'), count: outputs.length },
]}
/>
<Tabs label={$_('stateIac.detail.tabs')} bind:value={tab} idPrefix="stack" items={tabs} />

{#if tab === 'resources'}
<div
Expand Down Expand Up @@ -280,6 +307,19 @@
</li>
{/each}
</ul>
{:else if tab === 'history'}
<div role="tabpanel" id="stack-panel-history" aria-labelledby="stack-tab-history">
<StackHistory {apiBase} {scopeQuery} canDiff={canReadStates} onshowchanges={showChanges} />
</div>
{:else if tab === 'changes'}
<div role="tabpanel" id="stack-panel-changes" aria-labelledby="stack-tab-changes">
<StackDiff
{apiBase}
{scopeQuery}
to={changesTo}
lastChanges={summary.lastUpdate?.changes ?? null}
/>
</div>
{:else}
<div
class="k-card p-4 font-mono text-xs"
Expand Down
Loading
Loading