DrString icon indicating copy to clipboard operation
DrString copied to clipboard

Add GitHub action formatter

Open kerrmarin-lvmh opened this issue 3 years ago • 1 comments

Would be great to have a formatter that, when run in a GitHub action, formats the output in a way that can be understood by the runner and shows warnings in PRs. The format required is defined here

kerrmarin-lvmh avatar Apr 18 '22 15:04 kerrmarin-lvmh

I ended up doing a quick-and-dirty python script that does what I needed:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

'''
Runs drstring and formats the output for GitHub Actions
'''

import os
import argparse
from subprocess import PIPE, Popen
import re

def convertToGitHubActionsOutput(line: str, current_directory: str):
    regex = re.compile('^(.+?):(.+?):(.+?):(.+?):(.+)$')
    matches = regex.findall(line)[0]
    filename = matches[0].replace(current_directory, "")[1:]
    return f"::warning file={filename},line={matches[1]},col={matches[2]},endColumn={matches[2]}::{matches[4].strip()}"


# Parse argument for path
parser = argparse.ArgumentParser(description="Runs drstring and formats the output for GitHub Actions")
parser.add_argument('config_file', type=str, help='The path to the config file for drstring')
parser.add_argument('root_path', type=str, help='The path to the root of the project')
args = parser.parse_args()

# Run drstring
result = Popen(f"drstring check --config-file {args.config_file}", shell=True, stdout=PIPE, stderr=PIPE)
stdout, _ = result.communicate()

# Clean output into list of errors
cleaned_output = list(filter(None, stdout.decode('UTF-8').split("\n")))

# Map list of errors into something github actions understands
gh_output = [convertToGitHubActionsOutput(line, args.root_path) for line in cleaned_output]

for line in gh_output:
    print(line)

kerrmarin-lvmh avatar May 26 '22 11:05 kerrmarin-lvmh