File size: 1,667 Bytes
d8d14f1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import os
import subprocess

import requests
from dotenv import load_dotenv

load_dotenv

# Constants
GITHUB_USERNAME = os.getenv("GITHUB_USERNAME")
REPO_NAME = os.getenv("GITHUB_REPO_NAME")
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
ISSUES_URL = f"https://api.github.com/repos/{GITHUB_USERNAME}/{REPO_NAME}/issues"

# Headers for authentication
headers = {
    "Authorization": f"token {GITHUB_TOKEN}",
    "Accept": "application/vnd.github.v3+json",
}


def run_pytest():
    result = subprocess.run(
        ["pytest"], capture_output=True, text=True
    )
    return result.stdout + result.stderr


def parse_pytest_output(output):
    errors = []
    current_error = None

    for line in output.split("\n"):
        if line.startswith("_________________________"):
            if current_error:
                errors.append(current_error)
            current_error = {"title": "", "body": ""}
        elif current_error is not None:
            if not current_error["title"]:
                current_error["title"] = line.strip()
            current_error["body"] += line + "\n"

    if current_error:
        errors.append(current_error)
    return errors


def create_github_issue(title, body):
    issue = {"title": title, "body": body}
    response = requests.post(ISSUES_URL, headers=headers, json=issue)
    return response.json()


def main():
    pytest_output = run_pytest()
    errors = parse_pytest_output(pytest_output)

    for error in errors:
        issue_response = create_github_issue(
            error["title"], error["body"]
        )
        print(f"Issue created: {issue_response.get('html_url')}")


if __name__ == "__main__":
    main()