Skip to content
Open
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
80 changes: 54 additions & 26 deletions dist/index.js

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

40 changes: 22 additions & 18 deletions src/planfile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import * as semver from 'semver'
import { z } from 'zod'

const planChangeSchema = z.object({
address: z.string(),
change: z.object({
actions: z.union([
z.tuple([z.literal('no-op')]),
z.tuple([z.literal('create')]),
z.tuple([z.literal('read')]),
z.tuple([z.literal('delete')]),
z.tuple([z.literal('update')]),
z.tuple([z.literal('delete'), z.literal('create')]),
z.tuple([z.literal('create'), z.literal('delete')]),
z.tuple([z.literal('forget')]),
z.tuple([z.literal('create'), z.literal('forget')])
])
})
});

const planfileSchema = z.object({
format_version: z.string().refine(
(v) => {
Expand All @@ -11,29 +28,16 @@ const planfileSchema = z.object({
message: `Version ${v} of Terraform planfile is currently unsupported (must be version 1.x).`
})
),
resource_drift: z
.array(planChangeSchema)
.optional(),
resource_changes: z
.array(
z.object({
address: z.string(),
change: z.object({
actions: z.union([
z.tuple([z.literal('no-op')]),
z.tuple([z.literal('create')]),
z.tuple([z.literal('read')]),
z.tuple([z.literal('delete')]),
z.tuple([z.literal('update')]),
z.tuple([z.literal('delete'), z.literal('create')]),
z.tuple([z.literal('create'), z.literal('delete')]),
z.tuple([z.literal('forget')]),
z.tuple([z.literal('create'), z.literal('forget')])
])
})
})
)
.array(planChangeSchema)
.optional()
})

export type StructuredPlanfile = z.infer<typeof planfileSchema>
export type StructuredPlanChange = z.infer<typeof planChangeSchema>

export function parsePlanfileJSON(json: string): StructuredPlanfile {
return planfileSchema.parse(json)
Expand Down
67 changes: 48 additions & 19 deletions src/render.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as exec from '@actions/exec'
import type { StructuredPlanfile } from './planfile'
import type { StructuredPlanfile, StructuredPlanChange } from './planfile'
import { parsePlanfileJSON } from './planfile'

export type RenderedPlan = {
Expand Down Expand Up @@ -100,35 +100,64 @@ function extractResources(
)
}

function queryChanges(changes: StructuredPlanChange[], changeKind: string): string[] {
return changes
.filter((r) => r.change.actions.toString() === [changeKind].toString())
.map((r) => r.address)
}
Comment on lines +103 to +107
Copy link
Owner

Choose a reason for hiding this comment

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

Let's generalize this and use changeKind: string[] s.t. we can also use this for recreated changes


function getRecreatedChanges(changes: StructuredPlanChange[]): string[] {
return changes
.filter(
(r) =>
r.change.actions.toString() === ['delete', 'create'].toString() ||
r.change.actions.toString() === ['create', 'delete'].toString()
)
.map((r) => r.address)
}

export function internalRenderPlan(
structuredPlan: StructuredPlanfile,
humanReadablePlan: string
): RenderedPlan {
// If there are no changes, we do not need to build any sections
if (
structuredPlan.resource_changes === undefined ||
structuredPlan.resource_changes.length === 0
(
structuredPlan.resource_changes === undefined ||
structuredPlan.resource_changes.length === 0
) && (
structuredPlan.resource_drift === undefined ||
structuredPlan.resource_drift.length === 0
)
) {
return {}
}

// Partition changes for output formatting and extract resources
const createdResources = structuredPlan.resource_changes
.filter((r) => r.change.actions.toString() === ['create'].toString())
.map((r) => r.address)
const updatedResources = structuredPlan.resource_changes
.filter((r) => r.change.actions.toString() === ['update'].toString())
.map((r) => r.address)
const recreatedResources = structuredPlan.resource_changes
.filter(
(r) =>
r.change.actions.toString() === ['delete', 'create'].toString() ||
r.change.actions.toString() === ['create', 'delete'].toString()
)
.map((r) => r.address)
const deletedResources = structuredPlan.resource_changes
.filter((r) => r.change.actions.toString() === ['delete'].toString())
.map((r) => r.address)
const createdResources = queryChanges(
structuredPlan.resource_changes || [],
'create'
).concat(queryChanges(
structuredPlan.resource_drift || [],
'create',
))
Comment on lines +137 to +143
Copy link
Owner

Choose a reason for hiding this comment

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

Can we create a new function which takes care of the concatenation s.t. we don't have to do this multiple times?

const updatedResources = queryChanges(
structuredPlan.resource_changes || [],
'update'
).concat(queryChanges(
structuredPlan.resource_drift || [],
'update',
))
const deletedResources = queryChanges(
structuredPlan.resource_changes || [],
'delete'
).concat(queryChanges(
structuredPlan.resource_drift || [],
'delete')
)
const recreatedResources = getRecreatedChanges(
structuredPlan.resource_changes || []
).concat(getRecreatedChanges(structuredPlan.resource_drift || []))

return {
createdResources: extractResources(createdResources, humanReadablePlan),
Expand Down
Loading