Skip to content

Commit

Permalink
Revert " feat: multiple feature addition #58 (#60)"
Browse files Browse the repository at this point in the history
This reverts commit 01f0321.
  • Loading branch information
mitchell-liatrio authored May 8, 2024
1 parent 01f0321 commit 201f3c0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 156 deletions.
12 changes: 4 additions & 8 deletions .github/workflows/action-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"
node-version: '20'
- run: yarn install
- run: yarn run build

Expand All @@ -26,14 +26,10 @@ jobs:
uses: ./
with:
json-file: test-data/tf_test.json
expand-comment: "true"
include-plan-job-summary: "true"
comment-header: "BIG HEADER"
comment-footer: "BIG FOOTER"
include-workflow-link: "true"
quiet: "true"
expand-comment: 'true'
include-plan-job-summary: 'true'

- name: Test PR Commenter with no changes
uses: ./
with:
with:
json-file: test-data/tf_nochanges.json
10 changes: 4 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
name: Release
on:
push:
branches: [main]
release:
types: [published]
branches: [ main ]
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: "20"

node-version: '20'
- name: Install Dependencies
run: yarn install

- name: yarn build and semantic-release
run: yarn run build && npx semantic-release
env:
Expand Down
28 changes: 6 additions & 22 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
name: Terraform Change PR Commenter v2
name: Terraform Change PR Commenter
description: Parse changes from Terraform Plan JSON and post them for PR review
branding:
icon: "git-pull-request"
color: "green"
icon: 'git-pull-request'
color: 'green'
inputs:
json-file:
description: File location for the Terraform Plan JSON file
Expand All @@ -11,31 +11,15 @@ inputs:
github-token:
description: GitHub Token
required: false
default: "${{github.token}}"
default: '${{github.token}}'
expand-comment:
description: If true, expand the details comment by default
required: false
default: "false"
default: 'false'
include-plan-job-summary:
description: If true, add the results of the plan to the workflow job summary
required: false
default: "false"
comment-header:
description: Header to use for the comment
required: false
default: "Terraform Plan Changes"
comment-footer:
description: Footer to use for the comment
required: false
default: ""
include-workflow-link:
description: If true, include a link to the workflow in the comment
required: false
default: false
quiet:
description: Skips the comment if there are no changes
required: false
default: false
default: 'false'
runs:
using: node20
main: dist/index.js
87 changes: 27 additions & 60 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12824,42 +12824,26 @@ const myToken = core.getInput('github-token');
const octokit = github.getOctokit(myToken);
const context = github.context;
const inputFilenames = core.getMultilineInput('json-file');
const commentHeader = core.getMultilineInput('comment-header');
const commentFooter = core.getMultilineInput('comment-footer');
const quietMode = core.getBooleanInput('quiet');
const includeLinkToWorkflow = core.getBooleanInput('include-workflow-link');


const workflowLink = includeLinkToWorkflow ? `
[Workflow: ${context.workflow}](${ context.serverUrl }/${ context.repo.owner }/${ context.repo.repo }/actions/runs/${ context.runId })
` : "";

var hasNoChanges = false;

const output = () => {
let body = '';
// for each file
for (const file of inputFilenames) {
for(const file of inputFilenames) {
const resource_changes = JSON.parse(fs.readFileSync(file)).resource_changes;
try {
let changed_resources = resource_changes.filter((resource) => {
return resource.change.actions != ["no-op"];
})

console.log("changed_resources", changed_resources)
if (Array.isArray(resource_changes) && resource_changes.length > 0) {
const resources_to_create = [],
resources_to_update = [],
resources_to_delete = [],
resources_to_replace = [],
resources_unchanged = [];

if(Array.isArray(resource_changes) && resource_changes.length > 0) {
const resources_to_create = []
, resources_to_update = []
, resources_to_delete = []
, resources_to_replace = []
, resources_unchanged = [];

// for each resource changes
for (const resource of resource_changes) {
for(const resource of resource_changes) {
const change = resource.change;
const address = resource.address;

switch (change.actions[0]) {
switch(change.actions[0]) {
default:
break;
case "no-op":
Expand All @@ -12869,7 +12853,7 @@ const output = () => {
resources_to_create.push(address);
break;
case "delete":
if (change.actions.length > 1) {
if(change.actions.length > 1) {
resources_to_replace.push(address);
} else {
resources_to_delete.push(address);
Expand All @@ -12884,64 +12868,57 @@ const output = () => {
// there will be formatting error when comment is
// showed on GitHub
body += `
${commentHeader}
\`${file}\`
<details ${expandDetailsComment ? "open" : ""}>
<summary>
<b>Terraform Plan: ${resources_to_create.length} to be created, ${resources_to_delete.length} to be deleted, ${resources_to_update.length} to be updated, ${resources_to_replace.length} to be replaced, ${resources_unchanged.length} unchanged.</b>
</summary>
<summary>
<b>Terraform Plan: ${resources_to_create.length} to be created, ${resources_to_delete.length} to be deleted, ${resources_to_update.length} to be updated, ${resources_to_replace.length} to be replaced, ${resources_unchanged.length} unchanged.</b>
</summary>
${details("create", resources_to_create, "+")}
${details("delete", resources_to_delete, "-")}
${details("update", resources_to_update, "!")}
${details("replace", resources_to_replace, "+")}
</details>
${commentFooter.map(a => a == '' ? '\n' : a).join('\n')}
${workflowLink}
`
if (resources_to_create + resources_to_delete + resources_to_update + resources_to_replace == []) {
hasNoChanges = true;
}
} else {
hasNoChanges = true;
console.log("No changes found in the plan. setting hasNoChanges to true.")
body += `
\`${file}\`
<p>There were no changes done to the infrastructure.</p>
`
core.info(`"The content of ${file} did not result in a valid array or the array is empty... Skipping."`)
}
} catch (error) {
core.error(`${file} is not a valid JSON file. error: ${error}`);
core.error(`${file} is not a valid JSON file.`);
}
}
return body;
}

const details = (action, resources, operator) => {
let str = "";

if (resources.length !== 0) {
if(resources.length !== 0) {
str = `
#### Resources to ${action}\n
\`\`\`diff\n
`;
for (const el of resources) {
for(const el of resources) {
// In the replace block, we show delete (-) and then create (+)
if (action === "replace") {
if(action === "replace") {
str += `- ${el}\n`
}
str += `${operator} ${el}\n`
}

str += "```\n"
}

return str;
}

try {
let rawOutput = output();
if (includePlanSummary) {
core.info("Adding plan output to job summary")
core.summary.addHeading('Terraform Plan Results').addRaw(rawOutput).write()
core.summary.addHeading('Terraform Plan Results').addRaw(output()).write()
}

if (context.eventName === 'pull_request') {
Expand All @@ -12952,27 +12929,17 @@ try {
process.exit(0);
}

console.log("quietMode", quietMode)
console.log("hasNoChanges", hasNoChanges)
console.log("quietMode && hasNoChanges", quietMode && hasNoChanges)
if (quietMode && hasNoChanges) {
core.info("quiet mode is enabled and there are no changes to the infrastructure.")
core.info("Skipping comment creation.")
process.exit(0);
}

core.info("Adding comment to PR");
core.info(`Comment: ${rawOutput}`);

octokit.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: rawOutput
body: output()
});

} catch (error) {
core.setFailed(error.message);
}

})();

module.exports = __webpack_exports__;
Expand Down
Loading

0 comments on commit 201f3c0

Please sign in to comment.