Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@joshuataylor
Created October 22, 2020 10:57
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joshuataylor/55859e31982333ff1ab1d8cfccf36a62 to your computer and use it in GitHub Desktop.
Save joshuataylor/55859e31982333ff1ab1d8cfccf36a62 to your computer and use it in GitHub Desktop.
Parse dbt errors in github actions to display in github annotations
import json
import re
# Open the run_results.json, which contains results from the run or test run.
input_file = open('target/run_results.json')
json_array = json.load(input_file)
# If there is no errors, by default we just print a new line.
errors = "\n"
for item in json_array["results"]:
# Run and test have different syntaxes, this should cover them both.
if (item['fail'] is None) and item['status'] != 'ERROR':
continue
# The original file name is the one we want, as otherwise it'll be in the wrong path
file = item['node']['original_file_path']
# If there is no error, eg it's from a test run, we can just print "Error in test".
error = item['error'] or "Error in test"
line = "0"
position = "0"
# hack to show line numbers for syntax errors, if there is a better way I would love to know :)
if "syntax error line" in error:
line = re.search("line (.*) at", error).group(1)
position = re.search("position (.*) ", error).group(1)
# fixes multiline in github annotations, as they need to be escaped.
error = error.replace("\r", "%0D").replace("\n", "%0A").replace("]", "%5D").replace(";", "%3B")
errors += f"::error file={file},line={line},col={position}::{error}\n"
print(errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment