This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
forked from inTestiGator/pytest-blame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytest_blame.py
125 lines (115 loc) · 4.09 KB
/
pytest_blame.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""This tracks the last commit and prints out the results."""
import json
import re
import subprocess
import pytest
import requests
from git import Repo
pytest_plugins = "pytester"
# pylint: disable=W0601
def pytest_configure(config):
"""Regex to read the repo path"""
global SLUG
if config.pluginmanager.hasplugin("blame"):
rawProcess = subprocess.run(
["git", "config", "--get", "remote.origin.url"], stdout=subprocess.PIPE
)
output = rawProcess.stdout.decode("utf-8")
regexMatches = re.search(r".*(/|:)(.+?/.+?)\.git", output)
SLUG = regexMatches.group(2)
def pytest_addoption(parser):
"""Print stuff to header with --track"""
group = parser.getgroup("track")
group.addoption(
"--track",
action="store_true",
help="pytest-blame help \n--track: show last git commit",
)
def getstatus(sha):
"""Get status of CI check from github"""
# request data of the specific sha
response = requests.get(
"https://api.github.com/repos/" + SLUG + "/statuses/" + str(sha)
)
# read json data and convert it to list
statuses = json.loads(response.text)
# statuses will be an empty list if the state is failling or pending
if statuses == []:
check = "failure"
else:
state = statuses[0]
check = state["state"]
return check
# pylint: disable=E1101, C0200
def pytest_report_header():
"""Display github commits"""
if pytest.config.getoption("track"):
PATH = "."
repo = Repo(PATH)
commits = list(repo.iter_commits())
for i in range(len(commits)):
# check if the most recent commit is passing
if getstatus(commits[i].hexsha) == "success" and i == 0:
print(
"\nThe most recent commit is passing: ",
"https://github.com/" + SLUG + "/commit/" + commits[i].hexsha,
"\n",
commits[i].author,
":",
commits[i].message,
)
break
# check if no passing commit
elif i == len(commits) - 1 and getstatus(commits[i].hexsha) == "failure":
print(
"\nCan't find passing commit, the most recent commit is failing: ",
"https://github.com/" + SLUG + "/commit/" + commits[0].hexsha,
"\n",
commits[0].author,
":",
commits[0].message,
)
break
# check if current commit is failing
elif getstatus(commits[i].hexsha) == "failure":
pass
# find the most recent passing commit
else:
passingcommits = (
"\nMost recent passing commit: "
+ "https://github.com/"
+ SLUG
+ "/commit/"
+ commits[i].hexsha
+ "\n"
+ str(commits[i].author)
+ ": "
+ str(commits[i].message)
+ "\n"
+ "--------------------------------"
)
faillingcommits = ""
# looping through all failing commits
while i > 0:
faillingcommits = (
"\nFailing commit: "
+ "https://github.com/"
+ SLUG
+ "/commit/"
+ commits[i].hexsha
+ "\n"
+ str(commits[i - 1].author)
+ ": "
+ str(commits[i - 1].message)
+ faillingcommits
)
i -= 1
print(
passingcommits,
faillingcommits,
"\nThe last one is the most recent commit\n",
)
break
# give msg a default value
else:
print("\nCan't find commits")