Skip to content
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

Add JSON formatter for machine-readable output #68

Merged
merged 7 commits into from
Aug 8, 2024

Conversation

matthieucan
Copy link
Contributor

@matthieucan matthieucan commented Aug 7, 2024

The ability to save and use results from dbt-score in other tooling opens new possibilities.
To that end, add a new formatter, which uses stdout to print a JSON document displaying dbt-score's results.

Example:

$ dbt_score lint -f json
{
  "models": {
    "model1": {
      "score": 5.0,
      "badge": "🥈",
      "pass": true,
      "results": {
        "dbt_score.rules.generic.columns_have_description": {
          "result": "OK",
          "severity": "medium",
          "message": null
        },
        "dbt_score.rules.generic.has_description": {
          "result": "WARN",
          "severity": "medium",
          "message": "Model lacks a description."
        }
      }
    }
  },
  "project": {
    "score": 5.0,
    "badge": "🥈"
    "pass": true
  }
}

@matthieucan matthieucan self-assigned this Aug 7, 2024
Copy link
Contributor

@jochemvandooren jochemvandooren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good! 🚀

src/dbt_score/formatters/json_formatter.py Outdated Show resolved Hide resolved
src/dbt_score/formatters/json_formatter.py Outdated Show resolved Hide resolved
for rule, result in results.items():
if result is None:
self._model_results[model.name]["results"][rule.source()] = {
"result": "OK",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can make this an Enum or something, so it's clear what are the possible results codes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good idea, but given it's also used in the HumanReadableFormatter, this would require a bit of refactoring to have it proper. I suggest to keep it simple for now :)

src/dbt_score/formatters/json_formatter.py Outdated Show resolved Hide resolved
src/dbt_score/formatters/json_formatter.py Show resolved Hide resolved
Copy link
Contributor

@jochemvandooren jochemvandooren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! 🚀 I'll leave the Enum comment up to you 👍

Comment on lines +37 to +52
"pass": true,
"results": {
"tests.conftest.rule_severity_low": {
"result": "OK",
"severity": "low",
"message": null
},
"tests.conftest.rule_severity_medium": {
"result": "ERR",
"severity": "medium",
"message": "Oh noes"
},
"tests.conftest.rule_severity_critical": {
"result": "WARN",
"severity": "critical",
"message": "Error"
Copy link
Contributor

@jochemvandooren jochemvandooren Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting pass considering the rules.result 😁 Not a problem though!

@matthieucan matthieucan merged commit 7660e8a into master Aug 8, 2024
3 checks passed
@matthieucan matthieucan deleted the matthieucan/json-formatter branch August 8, 2024 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants