Skip to content

Commit

Permalink
fix: hashicorp/setup-terraform terraform_wrapper (#29)
Browse files Browse the repository at this point in the history
This adds detection logic for the Hashicorp action's `terraform_wrapper`
output and fails the action run if it exists.

This is because `terraform_wrapper` output makes it impossible to
JSON.parse the `terraform show` command output used by
Open Policy Agent.
  • Loading branch information
patheard authored Jul 1, 2021
1 parent 796cd16 commit aeb178d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 5 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ Use the following to control the action:
terraform-init: -backend-config="region=ca-central-1"
```
# Notes
If you use the [hashicorp/setup-terraform](https://github.com/hashicorp/setup-terraform) action, `terraform_wrapper` needs to be disabled:
```yaml
- uses: hashicorp/setup-terraform@v1
with:
terraform_wrapper: false
```

# Contributing
To setup your local dev environment:
```sh
Expand Down
10 changes: 9 additions & 1 deletion dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14882,9 +14882,17 @@ const action = async () => {
if (!command.depends || results[command.depends].isSuccess) {
results[command.key] = execCommand(command, directory);
} else {
results[command.key] = { isSuccess: false };
results[command.key] = { isSuccess: false, output: "" };
}
isError = isError || !results[command.key].isSuccess;

// Check for hashicorp/setup-terraform action's terraform_wrapper output
if (results[command.key].output.indexOf("::debug::exitcode:") > -1) {
core.setFailed(
"Error: `hashicorp/setup-terraform` must have `terraform_wrapper: false`"
);
return;
}
}

// Delete previous PR comments
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion src/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,17 @@ const action = async () => {
if (!command.depends || results[command.depends].isSuccess) {
results[command.key] = execCommand(command, directory);
} else {
results[command.key] = { isSuccess: false };
results[command.key] = { isSuccess: false, output: "" };
}
isError = isError || !results[command.key].isSuccess;

// Check for hashicorp/setup-terraform action's terraform_wrapper output
if (results[command.key].output.indexOf("::debug::exitcode:") > -1) {
core.setFailed(
"Error: `hashicorp/setup-terraform` must have `terraform_wrapper: false`"
);
return;
}
}

// Delete previous PR comments
Expand Down
18 changes: 16 additions & 2 deletions test/action.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ describe("action", () => {
});

test("failed command", async () => {
execCommand.mockReturnValue({ isSuccess: false });
execCommand.mockReturnValue({ isSuccess: false, output: "" });

await action();

Expand All @@ -192,7 +192,7 @@ describe("action", () => {
});

test("allowed to fail", async () => {
execCommand.mockReturnValue({ isSuccess: false });
execCommand.mockReturnValue({ isSuccess: false, output: "" });
when(core.getBooleanInput)
.calledWith("allow-failure")
.mockReturnValue(true);
Expand All @@ -206,6 +206,20 @@ describe("action", () => {
expect(core.setFailed.mock.calls.length).toBe(0);
});

test("detect terraform_wrapper", async () => {
execCommand.mockReturnValue({
isSuccess: false,
output: "Some command output ::debug::exitcode: 1",
});

await action();

expect(core.setFailed.mock.calls.length).toBe(1);
expect(core.setFailed.mock.calls[0]).toEqual([
"Error: `hashicorp/setup-terraform` must have `terraform_wrapper: false`",
]);
});

test("failed validation", async () => {
when(core.getBooleanInput).calledWith("comment").mockReturnValue(true);
when(core.getInput).calledWith("github-token").mockReturnValue("false");
Expand Down

0 comments on commit aeb178d

Please sign in to comment.