Skip to content

Commit 9eecaf7

Browse files
authored
refactor(ts)!: use @actions/exec instead of bash script for OPA commands, allow testing entire directory vs file by file (#27)
## Describe your changes * Input var of `test_mode` to indicate whether to test the Rego by an entire directory - `directory` - (including entire package, e.g. `opa test ./`) or by individual file by file basis - `file` - (e.g. `opa test a_test.rego a.rego`). Default will test by entire directory package. * Previously, it was testing OPA policies on a file by file basis (testing a.rego against a_test.rego). If this is the desired behavior, use `test_mode = file`. * Default OPA version is now the latest at the time of writing at [1.4.2](https://github.com/open-policy-agent/opa/releases). Previously, it was 0.67.1, latest at Summer of 2024. * Executions are ran with the `--v0-compatible` flag, see official OPA documentation on this: https://www.openpolicyagent.org/docs/latest/v0-compatibility/ * The action will now use [@actions/exec ](https://github.com/actions/toolkit/tree/main/packages/exec) to run the OPA test commands instead of a bash script. The output is consumed and processed as JSON. ## Reminder: - When the PR is ready, be sure to run `npm run build` to compile into the distribution `/dist` folder, which is the source code that the Action uses.
1 parent 6d75705 commit 9eecaf7

21 files changed

+3608
-1358
lines changed

.github/workflows/test.yml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,20 @@ jobs:
3333
id: checkout
3434
uses: actions/checkout@v4
3535

36-
- name: Test Local Action
37-
id: test-action
36+
- name: Test Local Action (Individual File Mode)
37+
id: test-action-opa-files
3838
uses: ./
3939
with:
4040
path: ./examples
41+
test_mode: file
4142
report_untested_files: true
42-
pr_comment_title: Below is the Action testing on itself with this PR's source code against the `/examples` directory. Confirm it is as expected.
43+
pr_comment_title: Below is the Action testing on itself with this PR's source code against policies in `/examples` file by file. Confirm it is as expected.
44+
45+
- name: Test Local Action (Directory Package Mode)
46+
id: test-action-opa-package
47+
uses: ./
48+
with:
49+
path: ./examples
50+
test_mode: directory
51+
report_untested_files: true
52+
pr_comment_title: Below is the Action testing on itself with this PR's source code against `/examples` entire package directory. Confirm it is as expected.

.vscode/launch.json

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"version": "1.0.0",
3+
"configurations": [
4+
{
5+
"name": "TS-Node",
6+
"type": "node",
7+
"request": "launch",
8+
"runtimeExecutable": "npx",
9+
"runtimeArgs": [
10+
"ts-node",
11+
"./src/index.ts"
12+
// "--transpile-only",
13+
// "--esm"
14+
],
15+
// "program": "${file}",
16+
// "program": "${workspaceRoot}/src/index.ts",
17+
// "cwd": "${workspaceRoot}",
18+
"internalConsoleOptions": "openOnSessionStart",
19+
"skipFiles": ["<node_internals>/**", "node_modules/**"],
20+
"env": {
21+
"path": "./examples",
22+
"test_file_postfix": "_test",
23+
"test_mode": "directory"
24+
}
25+
}
26+
]
27+
}

README.md

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ It's super easy to get started and use this GitHub Action to test your OPA Rego
3131
- name: Run OPA Rego Tests
3232
uses: masterpointio/github-action-opa-rego-test@main
3333
with:
34+
path: ./examples
3435
report_untested_files: true # Flag to check & report Rego files that does NOT have corresponding test files. Optional, defaults to false.
3536
```
3637
@@ -61,7 +62,7 @@ jobs:
6162
- name: Run OPA Rego Tests
6263
uses: masterpointio/github-action-opa-rego-test@main
6364
with:
64-
path: "./config/spacelift-policies" # Path of the directory where the OPA Rego policies are stored. Optional, defaults to `.` which is the root directory.
65+
path: "./config/spacelift-policies" # Path of the directory where the OPA Rego policies are stored.
6566
report_untested_files: true # Flag to check & report Rego files without corresponding test files. Optional, defaults to false.
6667
```
6768
@@ -77,17 +78,19 @@ In the example below, all `_test.rego` files' location are valid and will be exe
7778

7879
### Inputs
7980

80-
| Input | Description | Required | Default |
81-
| ------------------------- | ----------------------------------------------------------------------------------------------- | -------- | --------------------------------- |
82-
| `path` | Path to the directory containing OPA Rego files to test | No | `.` (root directory) |
83-
| `test_file_postfix` | Postfix of the test files to run (e.g. notification.rego <> notification_test.rego) | No | `_test` |
84-
| `write_pr_comment` | Flag to write a user-friendly PR comment with test results | No | `true` |
85-
| `pr_comment_title` | Title of the PR comment for test results | No | `🧪 OPA Rego Policy Test Results` |
86-
| `pr_comment_mode` | Mode that will be used to update comment. Options of upsert (update in place) or recreate. | No | `upsert` |
87-
| `run_coverage_report` | Flag to run OPA coverage tests and include in PR comment | No | `true` |
88-
| `report_untested_files` | Check & report Rego files without corresponding test files | No | `false` |
89-
| `opa_version` | Version of the OPA CLI to use. | No | `0.67.1` |
90-
| `indicate_source_message` | Flag to comment the origins watermark (this repository) of the GitHub Action in the PR comment. | No | `true` |
81+
| Input | Description | Required | Default |
82+
| ------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------- | --------------------------------- |
83+
| `path` | Path to the directory containing OPA Rego files to test | Yes | REQUIRED |
84+
| `test_mode` | Whether to test the Rego by an entire directory (including entire package, e.g. `opa test ./`) or by individual files (e.g. `opa test a_test.rego a.rego`). Options of `directory` or `file`. Default is `directory`. | No | `test` |
85+
| `test_file_postfix` | Postfix of the test files to run (e.g. notification.rego <> notification_test.rego) | No | `_test` |
86+
| `write_pr_comment` | Flag to write a user-friendly PR comment with test results | No | `true` |
87+
| `pr_comment_title` | Title of the PR comment for test results | No | `🧪 OPA Rego Policy Test Results` |
88+
| `pr_comment_mode` | Mode that will be used to update comment. Options of upsert (update in place) or recreate. | No | `upsert` |
89+
| `run_coverage_report` | Flag to run OPA coverage tests and include in PR comment | No | `true` |
90+
| `report_untested_files` | Check & report Rego files without corresponding test files | No | `false` |
91+
| `opa_version` | Version of the OPA CLI to use. | No | `1.4.2` |
92+
| `opa_static` | Whether to use the static binary for OPA installation. use. | No | `false` |
93+
| `indicate_source_message` | Flag to comment the origins watermark (this repository) of the GitHub Action in the PR comment. | No | `true` |
9194

9295
### Outputs
9396

@@ -121,11 +124,14 @@ On each pull request, there is a GitHub Actions workflow that runs the tests aut
121124

122125
## 🏗️ Setup & Run Locally
123126

124-
You can use [nektos/act](https://github.com/nektos/act) to simulate and run a GitHub Actions workflow locally. To directly test the custom TypeScript action locally, you can:
127+
You can use [nektos/act](https://github.com/nektos/act) to simulate and run a GitHub Actions workflow locally.
128+
129+
To directly test the custom TypeScript action locally, you can:
125130

126131
1. `npm run install`
127132
2. `node ./dist/index.js`
128133
This is assuming you have `npm` and `node` installed already. Note: You will have to manually provide the required inputs since this is directly executing the TypeScript code.
134+
Additionally, if you are using VS Code, you can use the `.vscode/launch.json` (which executes `npx ts-node ./src/index.ts`) to run and attach the debugger.
129135

130136
## 📦 Releases / Packaging for Distribution
131137

0 commit comments

Comments
 (0)