Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add reason to deleted & re-created resources #2

Merged
merged 1 commit into from
Apr 29, 2024
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
27 changes: 21 additions & 6 deletions dist/index.js

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

2 changes: 1 addition & 1 deletion src/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { RenderedPlan } from './render'
function renderResources(resources: Record<string, string>): string {
let result = ''
for (const key of Object.keys(resources).sort()) {
const content = '```diff\n' + resources[key] + '\n```'
const content = resources[key]
result += `\n\n<details><summary><code>${key}</code></summary>\n\n${content}\n\n</details>`
}
return result
Expand Down
33 changes: 26 additions & 7 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ export type RenderedPlan = {
deletedResources?: Record<string, string>
}

function extractResourceContent(name: string, humanReadablePlan: string): string[] {
type ResourceContent = {
reason?: string
lines: string[]
}

function extractResourceContent(name: string, humanReadablePlan: string): ResourceContent {
const lines = humanReadablePlan.split('\n')

// In the plan, find the resource with the appropriate name
Expand All @@ -32,13 +37,21 @@ function extractResourceContent(name: string, humanReadablePlan: string): string
throw Error(`Resource '${name}' cannot be properly extracted from the human-readable plan.`)
}

// Eventually, we return the *contents* of the resource block
return lines.slice(resourceLineIndex + 1, resourceLineIndex + closingLineIndex)
// Eventually, we return the *contents* of the resource block along with a reason (if available)
let reason: string | undefined
if (resourceLineIndex - resourceHeaderIndex > 1) {
const match = lines[resourceLineIndex - 1].match(/\((.*)\)/)
if (match) {
reason = match[1]
}
}
const result = lines.slice(resourceLineIndex + 1, resourceLineIndex + closingLineIndex)
return { reason, lines: result }
}

function cleanResourceContent(content: string[]): string {
function formatResourceContent(content: ResourceContent): string {
// Indentation should be 6 spaces so we can get rid of this in all lines.
const aligned = content.map((line) => line.slice(6))
const aligned = content.lines.map((line) => line.slice(6))

// If there are now any lines where we find a `+`, `-`, or `~` only after spaces, we move it to
// the front.
Expand All @@ -52,7 +65,13 @@ function cleanResourceContent(content: string[]): string {

// Finally, we need to replace all `~` with `!` if they are the first character
const diffFinal = diffSuitable.map((line) => (line.startsWith('~') ? '!' + line.slice(1) : line))
return diffFinal.join('\n')
const formatted = '```diff\n' + diffFinal.join('\n') + '\n```'

let result = formatted
if (content.reason) {
result = formatted + `\n\n_→ ${content.reason}_`
}
return result
}

function extractResources(
Expand All @@ -65,7 +84,7 @@ function extractResources(
return names.reduce(
(acc, name) => {
const content = extractResourceContent(name, humanReadablePlan)
acc[name] = cleanResourceContent(content)
acc[name] = formatResourceContent(content)
return acc
},
{} as Record<string, string>
Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/basic/2-delete/rendered.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@
- id = "ec95e23aec3f635e195cb68d3b8b212ed0ff0da7" -> null
```

_→ because local_file.test is not in configuration_

</details>