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 GitHub action formatter #277

Open
kerrmarin-lvmh opened this issue Apr 18, 2022 · 1 comment
Open

Add GitHub action formatter #277

kerrmarin-lvmh opened this issue Apr 18, 2022 · 1 comment

Comments

@kerrmarin-lvmh
Copy link

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
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant