From ab68bb614b5eb42957331703bf7afa80e1188186 Mon Sep 17 00:00:00 2001 From: Mitchell Phillips Date: Wed, 8 May 2024 12:20:37 -0500 Subject: [PATCH] Revert " feat: multiple feature addition #58 (#60)" (#63) This reverts commit 01f0321246312425041b0ee8eaa5ba9404f6f1ae. --- .github/workflows/action-test.yml | 12 ++--- .github/workflows/release.yml | 10 ++-- action.yml | 28 +++------- dist/index.js | 87 ++++++++++--------------------- index.js | 86 +++++++++--------------------- 5 files changed, 67 insertions(+), 156 deletions(-) diff --git a/.github/workflows/action-test.yml b/.github/workflows/action-test.yml index a779e74..102ee08 100644 --- a/.github/workflows/action-test.yml +++ b/.github/workflows/action-test.yml @@ -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 @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 32f1256..5fd95d0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,9 +1,7 @@ name: Release on: push: - branches: [main] - release: - types: [published] + branches: [ main ] jobs: release: runs-on: ubuntu-latest @@ -11,11 +9,11 @@ jobs: - 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: diff --git a/action.yml b/action.yml index 0487a99..9ee8728 100644 --- a/action.yml +++ b/action.yml @@ -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 @@ -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 diff --git a/dist/index.js b/dist/index.js index 694f92a..7fcd019 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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": @@ -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); @@ -12884,32 +12868,26 @@ const output = () => { // there will be formatting error when comment is // showed on GitHub body += ` -${commentHeader} +\`${file}\`
- -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. - + + 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. + ${details("create", resources_to_create, "+")} ${details("delete", resources_to_delete, "-")} ${details("update", resources_to_update, "!")} ${details("replace", resources_to_replace, "+")}
-${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}\`

There were no changes done to the infrastructure.

` 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; @@ -12917,15 +12895,15 @@ ${workflowLink} 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` @@ -12933,15 +12911,14 @@ const details = (action, resources, operator) => { 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') { @@ -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__; diff --git a/index.js b/index.js index fbf3718..ecffa91 100644 --- a/index.js +++ b/index.js @@ -8,42 +8,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": @@ -53,7 +37,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); @@ -68,32 +52,26 @@ const output = () => { // there will be formatting error when comment is // showed on GitHub body += ` -${commentHeader} +\`${file}\`
- -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. - + + 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. + ${details("create", resources_to_create, "+")} ${details("delete", resources_to_delete, "-")} ${details("update", resources_to_update, "!")} ${details("replace", resources_to_replace, "+")}
-${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}\`

There were no changes done to the infrastructure.

` 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; @@ -101,15 +79,15 @@ ${workflowLink} 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` @@ -117,15 +95,14 @@ const details = (action, resources, operator) => { 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') { @@ -136,24 +113,13 @@ 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); }