-
Notifications
You must be signed in to change notification settings - Fork 22
Expose versioned JSON results from the Policy as Code Action #209
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
659b266
Initial plan
Copilot 306c5f9
Add structured action results output
Copilot 4434b1b
Harden structured output handling
Copilot 733fb9c
Address review feedback and fix action output CI failures
Copilot 83eb75b
Document write_results parameters
Copilot 4c5b9d4
Merge main to resolve conflicts
Copilot 14c644f
Merge branch 'main' into copilot/add-structured-output-option
felickz ef3ed04
Address remaining PR review suggestions
Copilot 502c622
Provide fallback action results output
Copilot 3354913
Emit clearly-errored fallback JSON when results file is missing
Copilot 0f14c45
Address second review: null total_errors in fallback, document fallba…
felickz 47c5e1e
Merge branch 'main' into copilot/add-structured-output-option
felickz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import json | ||
| import os | ||
| from typing import Any, Dict | ||
|
|
||
|
|
||
| def write_results( | ||
| path: str, | ||
| total_violations: int, | ||
| total_errors: int, | ||
| checks: Dict[str, Dict[str, Any]], | ||
| ) -> None: | ||
| """Write policy check results to a JSON file. | ||
|
|
||
| Args: | ||
| path: File path to write the JSON results to. Missing parent | ||
| directories are created. | ||
| total_violations: Total number of policy violations found by the | ||
| checks that ran successfully. | ||
| total_errors: Number of checks that failed with an error. | ||
| checks: Per-check results, keyed by check name, each containing the | ||
| check `status`, its `violations` count and, on failure, an `error`. | ||
| """ | ||
| output_directory = os.path.dirname(path) | ||
| if output_directory: | ||
| os.makedirs(output_directory, exist_ok=True) | ||
|
|
||
| with open(path, "w", encoding="utf-8") as handle: | ||
| json.dump( | ||
| { | ||
| "schema_version": 1, | ||
| "total_violations": total_violations, | ||
| "total_errors": total_errors, | ||
| "checks": checks, | ||
| }, | ||
| handle, | ||
| indent=2, | ||
| ) | ||
| handle.write("\n") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import json | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
|
|
||
| from ghascompliance.output import write_results | ||
|
|
||
|
|
||
| class TestOutput(unittest.TestCase): | ||
| def test_write_results_creates_structured_json(self): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| path = os.path.join(directory, "results", "output.json") | ||
|
|
||
| write_results( | ||
| path, | ||
| 2, | ||
| 1, | ||
| { | ||
| "code_scanning": {"status": "success", "violations": 1}, | ||
| "dependabot": {"status": "success", "violations": 1}, | ||
| "secret_scanning": { | ||
| "status": "error", | ||
| "violations": 0, | ||
| "error": "Authentication Error", | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| with open(path, encoding="utf-8") as handle: | ||
| self.assertEqual( | ||
| json.load(handle), | ||
| { | ||
| "schema_version": 1, | ||
| "total_violations": 2, | ||
| "total_errors": 1, | ||
| "checks": { | ||
| "code_scanning": {"status": "success", "violations": 1}, | ||
| "dependabot": {"status": "success", "violations": 1}, | ||
| "secret_scanning": { | ||
| "status": "error", | ||
| "violations": 0, | ||
| "error": "Authentication Error", | ||
| }, | ||
| }, | ||
| }, | ||
| ) | ||
|
|
||
| def test_write_results_ends_with_newline(self): | ||
| with tempfile.TemporaryDirectory() as directory: | ||
| path = os.path.join(directory, "output.json") | ||
|
|
||
| write_results(path, 0, 0, {}) | ||
|
|
||
| with open(path, encoding="utf-8") as handle: | ||
| self.assertTrue(handle.read().endswith("\n")) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.