Skip to content

Commit

Permalink
fix: #30 empty plan would failed the action (#31)
Browse files Browse the repository at this point in the history
* docs: changed version from 1.2.0 to 1.3.1 in readme
* chore: added new test file
* fix: added error handling in main function
* fix: added error handling for output() function
* fix: formatting of comment body
* chore: added README.md to semantic-release assets to change
* fix: using ternary operator to output feedback in main function
* fix: changed where the comment creates the body for no changes to infra
  • Loading branch information
anthonygauthier authored Jun 9, 2022
1 parent e218cb9 commit c4ea052
Show file tree
Hide file tree
Showing 4 changed files with 891 additions and 41 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/action-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,21 @@ jobs:
- run: yarn install
- run: yarn run build

- name: Pull Request Comment
- name: Test PR Commenter
uses: ./
with:
json-file: |
test-data/tf_test.json
test-data/tf_test2.json
test-data/tf_test3.json
- name: Pull Request Comment
- name: Test PR Comment Expand feature
uses: ./
with:
json-file: test-data/tf_test.json
expand-comment: 'true'

- name: Test PR Commenter with no changes
uses: ./
with:
json-file: test-data/tf_nochanges.json
90 changes: 52 additions & 38 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,69 @@ const context = github.context;
const inputFilenames = core.getMultilineInput('json-file');

const output = () => {
let body = "";
let body = '';
// for each file
for(const file of inputFilenames) {
const resource_changes = JSON.parse(fs.readFileSync(file)).resource_changes;
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) {
const change = resource.change;
const address = resource.address;

switch(change.actions[0]) {
default:
break;
case "no-op":
resources_unchanged.push(address);
break;
case "create":
resources_to_create.push(address);
break;
case "delete":
if(change.actions.length > 1) {
resources_to_replace.push(address);
} else {
resources_to_delete.push(address);
try {
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) {
const change = resource.change;
const address = resource.address;

switch(change.actions[0]) {
default:
break;
case "no-op":
resources_unchanged.push(address);
break;
case "create":
resources_to_create.push(address);
break;
case "delete":
if(change.actions.length > 1) {
resources_to_replace.push(address);
} else {
resources_to_delete.push(address);
}
break;
case "update":
resources_to_update.push(address);
break;
}
break;
case "update":
resources_to_update.push(address);
break;
}
}

body += `
}
// the body must be indented at the start otherwise
// there will be formatting error when comment is
// showed on GitHub
body += `
\`${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>
`
} else {
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.`);
}
}
return body;
}
Expand Down Expand Up @@ -101,4 +115,4 @@ try {
});
} catch (error) {
core.setFailed(error.message);
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"package.json",
"yarn.lock",
"dist",
"CHANGELOG.md"
"CHANGELOG.md",
"README.md"
],
"message": "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
Expand Down
Loading

0 comments on commit c4ea052

Please sign in to comment.