Skip to content

Commit

Permalink
feat: add skip-fmt config option (#275)
Browse files Browse the repository at this point in the history
Add a new `skip-fmt` option that allows a user to skip the
`terraform fmt` check.  This option will default to `false`.
  • Loading branch information
patheard committed Dec 7, 2023
1 parent 308a30c commit b84f6e8
Show file tree
Hide file tree
Showing 11 changed files with 252 additions and 6 deletions.
30 changes: 25 additions & 5 deletions .github/workflows/pr-test.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
name: Test action
on: pull_request

env:
AWS_REGION: ca-central-1

permissions:
id-token: write
pull-requests: write
contents: read

jobs:
terraform-plan:
runs-on: ubuntu-latest
Expand All @@ -20,7 +28,10 @@ jobs:
allow-failure: true
- test: skip-conftest
allow-failure: false
skip-conftest: true
skip-conftest: true
- test: skip-fmt
allow-failure: false
skip-fmt: true
- test: skip-plan
allow-failure: false
skip-plan: true
Expand All @@ -43,10 +54,14 @@ jobs:
&& chmod +x tf-summarize \
&& mv tf-summarize /usr/local/bin
- name: Configure AWS credentials using OIDC
uses: aws-actions/configure-aws-credentials@010d0da01d0b5a38af31e9c3470dbfdabdecca3a # v4.0.1
with:
role-to-assume: arn:aws:iam::124044056575:role/terraform-plan-plan
role-session-name: TFPlan
aws-region: ${{ env.AWS_REGION }}

- name: Test ${{ matrix.test }}
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
uses: ./
with:
directory: test/${{ matrix.test }}
Expand All @@ -55,6 +70,7 @@ jobs:
comment-delete: true
github-token: ${{ secrets.GITHUB_TOKEN }}
skip-conftest: ${{ matrix.skip-conftest || 'false' }}
skip-fmt: ${{ matrix.skip-fmt || 'false' }}
skip-plan: ${{ matrix.skip-plan || 'false' }}

test-action-comments:
Expand Down Expand Up @@ -91,7 +107,11 @@ jobs:
},{
exist: false,
title: /Test skip-conftest/g,
state: /Conftest:\*\*/gm
state: /Conftest:\*\*/gm
},{
exist: true,
title: /Test skip-fmt/g,
state: /Terraform Validate:\*\* `success`\n\*\*✅   Terraform Plan/gm
},{
exist: true,
title: /Test skip-plan/g,
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Use the following to control the action:
| `terraform-init` | Custom Terraform init args | |
| `terragrunt` | Use Terragrunt instead of Terraform | false |
| `skip-conftest` | Skip the Conftest step | false |
| `skip-fmt` | Skip the Terraform format check | false |
| `skip-plan` | Skip the Terraform plan for projects without a remote state | false |


Expand Down
4 changes: 4 additions & 0 deletions action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ inputs:
description: 'Skip the conftest step'
required: false
default: 'false'
skip-fmt:
description: 'Skip the terraform fmt check'
required: false
default: 'false'
skip-plan:
description: 'Skip the planning step, used for repos that do not specifically have a remote backend'
required: false
Expand Down
12 changes: 12 additions & 0 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38005,6 +38005,7 @@ const action = async () => {
const isComment = core.getBooleanInput("comment");
const isCommentDelete = core.getBooleanInput("comment-delete");
const isTerragrunt = core.getBooleanInput("terragrunt");
const skipFormat = core.getBooleanInput("skip-fmt");
const skipPlan = core.getBooleanInput("skip-plan");
const skipConftest = core.getBooleanInput("skip-conftest");

Expand Down Expand Up @@ -38093,6 +38094,11 @@ const action = async () => {
}
}

if (skipFormat && command.key === "fmt") {
results[command.key] = { isSuccess: true, output: "" };
continue;
}

if (skipConftest && command.key === "conftest") {
results[command.key] = { isSuccess: true, output: "" };
continue;
Expand Down Expand Up @@ -38139,6 +38145,7 @@ const action = async () => {
changes,
planLimit,
conftestLimit,
skipFormat,
skipPlan,
skipConftest,
);
Expand Down Expand Up @@ -38223,7 +38230,9 @@ const nunjucks = __nccwpck_require__(7006);
const commentTemplate = `## {{ title }}
**{{ "✅" if results.init.isSuccess else "❌" }}   Terraform Init:** \`{{ "success" if results.init.isSuccess else "failed" }}\`
**{{ "✅" if results.validate.isSuccess else "❌" }}   Terraform Validate:** \`{{ "success" if results.validate.isSuccess else "failed" }}\`
{% if not skipFormat -%}
**{{ "✅" if results.fmt.isSuccess else "❌" }}   Terraform Format:** \`{{ "success" if results.fmt.isSuccess else "failed" }}\`
{% endif -%}
{% if not skipPlan -%}
**{{ "✅" if results.plan.isSuccess else "❌" }}   Terraform Plan:** \`{{ "success" if results.plan.isSuccess else "failed" }}\`
{% if not skipConftest -%}
Expand Down Expand Up @@ -38315,6 +38324,7 @@ Plan: {{ changes.resources.create }} to add, {{ changes.resources.update }} to c
* @param {Object} changes Resource and output changes for the plan
* @param {number} planLimit the number of characters to render
* @param {number} conftestPlanLimit the nubmer of characters to render
* @param {boolean} skipFormat Skip runnting terraform fmt check
* @param {boolean} skipPlan Skip the rendering of the plan output
*/
const addComment = async (
Expand All @@ -38325,6 +38335,7 @@ const addComment = async (
changes,
planLimit,
conftestLimit,
skipFormat,
skipPlan,
skipConftest,
) => {
Expand All @@ -38338,6 +38349,7 @@ const addComment = async (
title: title,
planLimit: planLimit,
conftestLimit: conftestLimit,
skipFormat: skipFormat,
skipPlan: skipPlan,
skipConftest: skipConftest,
runLink: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const action = async () => {
const isComment = core.getBooleanInput("comment");
const isCommentDelete = core.getBooleanInput("comment-delete");
const isTerragrunt = core.getBooleanInput("terragrunt");
const skipFormat = core.getBooleanInput("skip-fmt");
const skipPlan = core.getBooleanInput("skip-plan");
const skipConftest = core.getBooleanInput("skip-conftest");

Expand Down Expand Up @@ -111,6 +112,11 @@ const action = async () => {
}
}

if (skipFormat && command.key === "fmt") {
results[command.key] = { isSuccess: true, output: "" };
continue;
}

if (skipConftest && command.key === "conftest") {
results[command.key] = { isSuccess: true, output: "" };
continue;
Expand Down Expand Up @@ -157,6 +163,7 @@ const action = async () => {
changes,
planLimit,
conftestLimit,
skipFormat,
skipPlan,
skipConftest,
);
Expand Down
5 changes: 5 additions & 0 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ const nunjucks = require("nunjucks");
const commentTemplate = `## {{ title }}
**{{ "✅" if results.init.isSuccess else "❌" }}   Terraform Init:** \`{{ "success" if results.init.isSuccess else "failed" }}\`
**{{ "✅" if results.validate.isSuccess else "❌" }}   Terraform Validate:** \`{{ "success" if results.validate.isSuccess else "failed" }}\`
{% if not skipFormat -%}
**{{ "✅" if results.fmt.isSuccess else "❌" }}   Terraform Format:** \`{{ "success" if results.fmt.isSuccess else "failed" }}\`
{% endif -%}
{% if not skipPlan -%}
**{{ "✅" if results.plan.isSuccess else "❌" }}   Terraform Plan:** \`{{ "success" if results.plan.isSuccess else "failed" }}\`
{% if not skipConftest -%}
Expand Down Expand Up @@ -96,6 +98,7 @@ Plan: {{ changes.resources.create }} to add, {{ changes.resources.update }} to c
* @param {Object} changes Resource and output changes for the plan
* @param {number} planLimit the number of characters to render
* @param {number} conftestPlanLimit the nubmer of characters to render
* @param {boolean} skipFormat Skip runnting terraform fmt check
* @param {boolean} skipPlan Skip the rendering of the plan output
*/
const addComment = async (
Expand All @@ -106,6 +109,7 @@ const addComment = async (
changes,
planLimit,
conftestLimit,
skipFormat,
skipPlan,
skipConftest,
) => {
Expand All @@ -119,6 +123,7 @@ const addComment = async (
title: title,
planLimit: planLimit,
conftestLimit: conftestLimit,
skipFormat: skipFormat,
skipPlan: skipPlan,
skipConftest: skipConftest,
runLink: `${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${context.runId}`,
Expand Down
4 changes: 4 additions & 0 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ describe("action", () => {
.calledWith("comment-title")
.mockReturnValue("raspberries");
when(core.getInput).calledWith("github-token").mockReturnValue("mellow");
when(core.getBooleanInput).calledWith("skip-fmt").mockReturnValue(false);
when(core.getBooleanInput).calledWith("skip-plan").mockReturnValue(false);
github.getOctokit.mockReturnValue("octokit");
github.context = "context";
Expand Down Expand Up @@ -257,6 +258,7 @@ describe("action", () => {
30000,
2000,
false,
false,
]);
});

Expand Down Expand Up @@ -348,6 +350,7 @@ conftest test plan.json --no-color --update git::https://github.com/cds-snc/opa_
.calledWith("comment-title")
.mockReturnValue("raspberries");
when(core.getInput).calledWith("github-token").mockReturnValue("mellow");
when(core.getBooleanInput).calledWith("skip-fmt").mockReturnValue(false);
when(core.getBooleanInput).calledWith("skip-plan").mockReturnValue(true);
github.getOctokit.mockReturnValue("octokit");
github.context = "context";
Expand All @@ -373,6 +376,7 @@ conftest test plan.json --no-color --update git::https://github.com/cds-snc/opa_
{},
30000,
2000,
false,
true,
]);
});
Expand Down
80 changes: 80 additions & 0 deletions test/github.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ Hello there
{},
1000,
1000,
false,
true,
);
expect(octomock.rest.issues.createComment.mock.calls.length).toBe(1);
Expand All @@ -315,6 +316,84 @@ Hello there
});
});

test("don't render format results if skip-fmt is true", async () => {
const results = {
init: { isSuccess: true, output: "" },
validate: { isSuccess: true, output: "" },
fmt: { isSuccess: true, output: "" },
plan: { isSuccess: true, output: "< Hello there >" },
summary: { isSuccess: true, output: "" },
conftest: { isSuccess: true, output: "< General Kenobi >" },
};
const changes = {
isChanges: true,
isDeletes: true,
resources: {
update: 0,
delete: 0,
create: 1,
},
};
const comment = `## Foobar
**✅ &nbsp; Terraform Init:** \`success\`
**✅ &nbsp; Terraform Validate:** \`success\`
**✅ &nbsp; Terraform Plan:** \`success\`
**✅ &nbsp; Conftest:** \`success\`
**⚠️ &nbsp; Warning:** resources will be destroyed by this change!
\`\`\`terraform
Plan: 1 to add, 0 to change, 0 to destroy
\`\`\`
<details>
<summary>Show summary</summary>
</details>
<details>
<summary>Show plan</summary>
\`\`\`terraform
< Hello there >
\`\`\`
</details>
<details>
<summary>Show Conftest results</summary>
\`\`\`sh
< General Kenobi >
\`\`\`
</details>
`;

await addComment(
octomock,
context,
"Foobar",
results,
changes,
10000,
10000,
true,
false,
);
expect(octomock.rest.issues.createComment.mock.calls.length).toBe(1);
expect(octomock.rest.issues.createComment.mock.calls[0]).toEqual([
{
owner: "foo",
repo: "bar",
issue_number: 42,
body: comment,
},
]);
});

test("add a truncated plan comment", async () => {
const results = {
init: { isSuccess: true, output: "" },
Expand Down Expand Up @@ -382,6 +461,7 @@ Plan: 1 to add, 0 to change, 0 to destroy
10,
10000,
false,
false,
);
expect(octomock.rest.issues.createComment.mock.calls.length).toBe(1);
expect(octomock.rest.issues.createComment.mock.calls[0]).toEqual([
Expand Down
17 changes: 17 additions & 0 deletions test/skip-fmt/skip-fmt.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
terraform {
required_providers {
test = {
source = "hashicorp/random"
version = "~> 3.5.0"
}
}
required_version = ">= 1.0.0"
}

resource "random_id" "id" {
byte_length = 8
}

output "id" {
value = random_id.id.hex
}

0 comments on commit b84f6e8

Please sign in to comment.