| 1 |
#!/usr/bin/env python3 |
| 2 |
|
| 3 |
# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
| 4 |
# Use of this source code is governed by a BSD-style license that can be |
| 5 |
# found in the LICENSE file. |
| 6 |
|
| 7 |
"""Bot triggered by Github Actions every time a new issue, PR or comment |
| 8 |
is created. Assign labels, provide replies, closes issues, etc. depending |
| 9 |
on the situation. |
| 10 |
""" |
| 11 |
|
| 12 |
|
| 13 |
import functools |
| 14 |
import json |
| 15 |
import os |
| 16 |
import re |
| 17 |
from pprint import pprint as pp |
| 18 |
|
| 19 |
from github import Github |
| 20 |
|
| 21 |
ROOT_DIR = os.path.realpath( |
| 22 |
os.path.join(os.path.dirname(__file__), '..', '..') |
| 23 |
) |
| 24 |
SCRIPTS_DIR = os.path.join(ROOT_DIR, 'scripts') |
| 25 |
|
| 26 |
|
| 27 |
# --- constants |
| 28 |
|
| 29 |
# fmt: off |
| 30 |
LABELS_MAP = { |
| 31 |
# platforms |
| 32 |
"linux": [ |
| 33 |
"linux", "ubuntu", "redhat", "mint", "centos", "red hat", "archlinux", |
| 34 |
"debian", "alpine", "gentoo", "fedora", "slackware", "suse", "RHEL", |
| 35 |
"opensuse", "manylinux", "apt ", "apt-", "rpm", "yum", "kali", |
| 36 |
"/sys/class", "/proc/net", "/proc/disk", "/proc/smaps", |
| 37 |
"/proc/vmstat", |
| 38 |
], |
| 39 |
"windows": [ |
| 40 |
"windows", "win32", "WinError", "WindowsError", "win10", "win7", |
| 41 |
"win ", "mingw", "msys", "studio", "microsoft", "make.bat", |
| 42 |
"CloseHandle", "GetLastError", "NtQuery", "DLL", "MSVC", "TCHAR", |
| 43 |
"WCHAR", ".bat", "OpenProcess", "TerminateProcess", |
| 44 |
"windows error", "NtWow64", "NTSTATUS", "Visual Studio", |
| 45 |
], |
| 46 |
"macos": [ |
| 47 |
"macos", "mac ", "osx", "os x", "mojave", "sierra", "capitan", |
| 48 |
"yosemite", "catalina", "mojave", "big sur", "xcode", "darwin", |
| 49 |
"dylib", "m1", |
| 50 |
], |
| 51 |
"aix": ["aix"], |
| 52 |
"cygwin": ["cygwin"], |
| 53 |
"freebsd": ["freebsd"], |
| 54 |
"netbsd": ["netbsd"], |
| 55 |
"openbsd": ["openbsd"], |
| 56 |
"sunos": ["sunos", "solaris"], |
| 57 |
"wsl": ["wsl"], |
| 58 |
"unix": [ |
| 59 |
"psposix", "_psutil_posix", "waitpid", "statvfs", "/dev/tty", |
| 60 |
"/dev/pts", "posix", |
| 61 |
], |
| 62 |
"pypy": ["pypy"], |
| 63 |
"docker": ["docker", "docker-compose"], |
| 64 |
"vm": [ |
| 65 |
"docker", "docker-compose", "vmware", "lxc", "hyperv", "virtualpc", |
| 66 |
"virtualbox", "bhyve", "openvz", "lxc", "xen", "kvm", "qemu", "heroku", |
| 67 |
], |
| 68 |
# types |
| 69 |
"enhancement": ["enhancement"], |
| 70 |
"memleak": ["memory leak", "leaks memory", "memleak", "mem leak"], |
| 71 |
"api": ["idea", "proposal", "api", "feature"], |
| 72 |
"performance": ["performance", "speedup", "speed up", "slow", "fast"], |
| 73 |
"wheels": ["wheel", "wheels"], |
| 74 |
"scripts": [ |
| 75 |
"example script", "examples script", "example dir", "scripts/", |
| 76 |
], |
| 77 |
# bug |
| 78 |
"bug": [ |
| 79 |
"fail", "can't execute", "can't install", "cannot execute", |
| 80 |
"cannot install", "install error", "crash", "critical", |
| 81 |
], |
| 82 |
# doc |
| 83 |
"doc": [ |
| 84 |
"doc ", "document ", "documentation", "readthedocs", "pythonhosted", |
| 85 |
"HISTORY", "README", "dev guide", "devguide", "sphinx", "docfix", |
| 86 |
"index.rst", |
| 87 |
], |
| 88 |
# tests |
| 89 |
"tests": [ |
| 90 |
" test ", "tests", "travis", "coverage", "cirrus", |
| 91 |
"continuous integration", "unittest", "pytest", "unit test", |
| 92 |
], |
| 93 |
# critical errors |
| 94 |
"critical": [ |
| 95 |
"WinError", "WindowsError", "RuntimeError", "ZeroDivisionError", |
| 96 |
"SystemError", "MemoryError", "core dump", "segfault", |
| 97 |
"segmentation fault", |
| 98 |
], |
| 99 |
} |
| 100 |
|
| 101 |
OS_LABELS = [ |
| 102 |
"linux", "windows", "macos", "freebsd", "openbsd", "netbsd", "openbsd", |
| 103 |
"bsd", "sunos", "unix", "wsl", "aix", "cygwin", |
| 104 |
] |
| 105 |
# fmt: on |
| 106 |
|
| 107 |
LABELS_MAP['scripts'].extend( |
| 108 |
[x for x in os.listdir(SCRIPTS_DIR) if x.endswith('.py')] |
| 109 |
) |
| 110 |
|
| 111 |
ILLOGICAL_PAIRS = [ |
| 112 |
('bug', 'enhancement'), |
| 113 |
('doc', 'tests'), |
| 114 |
('scripts', 'doc'), |
| 115 |
('scripts', 'tests'), |
| 116 |
('bsd', 'freebsd'), |
| 117 |
('bsd', 'openbsd'), |
| 118 |
('bsd', 'netbsd'), |
| 119 |
] |
| 120 |
|
| 121 |
# --- replies |
| 122 |
|
| 123 |
REPLY_MISSING_PYTHON_HEADERS = """\ |
| 124 |
It looks like you're missing `Python.h` headers. This usually means you have \ |
| 125 |
to install them first, then retry psutil installation. |
| 126 |
Please read \ |
| 127 |
[INSTALL](https://github.com/giampaolo/psutil/blob/master/INSTALL.rst) \ |
| 128 |
instructions for your platform. \ |
| 129 |
This is an auto-generated response based on the text you submitted. \ |
| 130 |
If this was a mistake or you think there's a bug with psutil installation \ |
| 131 |
process, please add a comment to reopen this issue. |
| 132 |
""" |
| 133 |
|
| 134 |
# REPLY_UPDATE_CHANGELOG = """\ |
| 135 |
# """ |
| 136 |
|
| 137 |
|
| 138 |
# --- github API utils |
| 139 |
|
| 140 |
|
| 141 |
def is_pr(issue): |
| 142 |
return issue.pull_request is not None |
| 143 |
|
| 144 |
|
| 145 |
def has_label(issue, label): |
| 146 |
assigned = [x.name for x in issue.labels] |
| 147 |
return label in assigned |
| 148 |
|
| 149 |
|
| 150 |
def get_repo(): |
| 151 |
repo = os.environ['GITHUB_REPOSITORY'] |
| 152 |
token = os.environ['GITHUB_TOKEN'] |
| 153 |
return Github(token).get_repo(repo) |
| 154 |
|
| 155 |
|
| 156 |
# --- event utils |
| 157 |
|
| 158 |
|
| 159 |
@functools.lru_cache() |
| 160 |
def _get_event_data(): |
| 161 |
with open(os.environ["GITHUB_EVENT_PATH"]) as f: |
| 162 |
ret = json.load(f) |
| 163 |
pp(ret) |
| 164 |
return ret |
| 165 |
|
| 166 |
|
| 167 |
def is_event_new_issue(): |
| 168 |
data = _get_event_data() |
| 169 |
try: |
| 170 |
return data['action'] == 'opened' and 'issue' in data |
| 171 |
except KeyError: |
| 172 |
return False |
| 173 |
|
| 174 |
|
| 175 |
def is_event_new_pr(): |
| 176 |
data = _get_event_data() |
| 177 |
try: |
| 178 |
return data['action'] == 'opened' and 'pull_request' in data |
| 179 |
except KeyError: |
| 180 |
return False |
| 181 |
|
| 182 |
|
| 183 |
def get_issue(): |
| 184 |
data = _get_event_data() |
| 185 |
try: |
| 186 |
num = data['issue']['number'] |
| 187 |
except KeyError: |
| 188 |
num = data['pull_request']['number'] |
| 189 |
return get_repo().get_issue(number=num) |
| 190 |
|
| 191 |
|
| 192 |
# --- actions |
| 193 |
|
| 194 |
|
| 195 |
def log(msg): |
| 196 |
if '\n' in msg or "\r\n" in msg: |
| 197 |
print(f">>>\n{msg}\n<<<", flush=True) |
| 198 |
else: |
| 199 |
print(f">>> {msg} <<<", flush=True) |
| 200 |
|
| 201 |
|
| 202 |
def add_label(issue, label): |
| 203 |
def should_add(issue, label): |
| 204 |
if has_label(issue, label): |
| 205 |
log(f"already has label {label!r}") |
| 206 |
return False |
| 207 |
|
| 208 |
for left, right in ILLOGICAL_PAIRS: |
| 209 |
if label == left and has_label(issue, right): |
| 210 |
log(f"already has label f{label}") |
| 211 |
return False |
| 212 |
|
| 213 |
return not has_label(issue, label) |
| 214 |
|
| 215 |
if not should_add(issue, label): |
| 216 |
log(f"should not add label {label!r}") |
| 217 |
return |
| 218 |
|
| 219 |
log(f"add label {label!r}") |
| 220 |
issue.add_to_labels(label) |
| 221 |
|
| 222 |
|
| 223 |
def _guess_labels_from_text(text): |
| 224 |
assert isinstance(text, str), text |
| 225 |
for label, keywords in LABELS_MAP.items(): |
| 226 |
for keyword in keywords: |
| 227 |
if keyword.lower() in text.lower(): |
| 228 |
yield (label, keyword) |
| 229 |
|
| 230 |
|
| 231 |
def add_labels_from_text(issue, text): |
| 232 |
assert isinstance(text, str), text |
| 233 |
for label, keyword in _guess_labels_from_text(text): |
| 234 |
add_label(issue, label) |
| 235 |
|
| 236 |
|
| 237 |
def add_labels_from_new_body(issue, text): |
| 238 |
assert isinstance(text, str), text |
| 239 |
log("start searching for template lines in new issue/PR body") |
| 240 |
# add os label |
| 241 |
r = re.search(r"\* OS:.*?\n", text) |
| 242 |
log("search for 'OS: ...' line") |
| 243 |
if r: |
| 244 |
log("found") |
| 245 |
add_labels_from_text(issue, r.group(0)) |
| 246 |
else: |
| 247 |
log("not found") |
| 248 |
|
| 249 |
# add bug/enhancement label |
| 250 |
log("search for 'Bug fix: y/n' line") |
| 251 |
r = re.search(r"\* Bug fix:.*?\n", text) |
| 252 |
if ( |
| 253 |
is_pr(issue) |
| 254 |
and r is not None |
| 255 |
and not has_label(issue, "bug") |
| 256 |
and not has_label(issue, "enhancement") |
| 257 |
): |
| 258 |
log("found") |
| 259 |
s = r.group(0).lower() |
| 260 |
if 'yes' in s: |
| 261 |
add_label(issue, 'bug') |
| 262 |
else: |
| 263 |
add_label(issue, 'enhancement') |
| 264 |
else: |
| 265 |
log("not found") |
| 266 |
|
| 267 |
# add type labels |
| 268 |
log("search for 'Type: ...' line") |
| 269 |
r = re.search(r"\* Type:.*?\n", text) |
| 270 |
if r: |
| 271 |
log("found") |
| 272 |
s = r.group(0).lower() |
| 273 |
if 'doc' in s: |
| 274 |
add_label(issue, 'doc') |
| 275 |
if 'performance' in s: |
| 276 |
add_label(issue, 'performance') |
| 277 |
if 'scripts' in s: |
| 278 |
add_label(issue, 'scripts') |
| 279 |
if 'tests' in s: |
| 280 |
add_label(issue, 'tests') |
| 281 |
if 'wheels' in s: |
| 282 |
add_label(issue, 'wheels') |
| 283 |
if 'new-api' in s: |
| 284 |
add_label(issue, 'new-api') |
| 285 |
if 'new-platform' in s: |
| 286 |
add_label(issue, 'new-platform') |
| 287 |
else: |
| 288 |
log("not found") |
| 289 |
|
| 290 |
|
| 291 |
# --- events |
| 292 |
|
| 293 |
|
| 294 |
def on_new_issue(issue): |
| 295 |
def has_text(text): |
| 296 |
return text in issue.title.lower() or ( |
| 297 |
issue.body and text in issue.body.lower() |
| 298 |
) |
| 299 |
|
| 300 |
def body_mentions_python_h(): |
| 301 |
if not issue.body: |
| 302 |
return False |
| 303 |
body = issue.body.replace(' ', '') |
| 304 |
return ( |
| 305 |
"#include<Python.h>\n^~~~" in body |
| 306 |
or "#include<Python.h>\r\n^~~~" in body |
| 307 |
) |
| 308 |
|
| 309 |
log("searching for missing Python.h") |
| 310 |
if ( |
| 311 |
has_text("missing python.h") |
| 312 |
or has_text("python.h: no such file or directory") |
| 313 |
or body_mentions_python_h() |
| 314 |
): |
| 315 |
log("found mention of Python.h") |
| 316 |
issue.create_comment(REPLY_MISSING_PYTHON_HEADERS) |
| 317 |
issue.edit(state='closed') |
| 318 |
return |
| 319 |
|
| 320 |
|
| 321 |
def on_new_pr(issue): |
| 322 |
pass |
| 323 |
# pr = get_repo().get_pull(issue.number) |
| 324 |
# files = [x.filename for x in list(pr.get_files())] |
| 325 |
# if "HISTORY.rst" not in files: |
| 326 |
# issue.create_comment(REPLY_UPDATE_CHANGELOG) |
| 327 |
|
| 328 |
|
| 329 |
def main(): |
| 330 |
issue = get_issue() |
| 331 |
stype = "PR" if is_pr(issue) else "issue" |
| 332 |
log(f"running issue bot for {stype} {issue!r}") |
| 333 |
|
| 334 |
if is_event_new_issue(): |
| 335 |
log(f"created new issue {issue}") |
| 336 |
add_labels_from_text(issue, issue.title) |
| 337 |
if issue.body: |
| 338 |
add_labels_from_new_body(issue, issue.body) |
| 339 |
on_new_issue(issue) |
| 340 |
elif is_event_new_pr(): |
| 341 |
log(f"created new PR {issue}") |
| 342 |
add_labels_from_text(issue, issue.title) |
| 343 |
if issue.body: |
| 344 |
add_labels_from_new_body(issue, issue.body) |
| 345 |
on_new_pr(issue) |
| 346 |
else: |
| 347 |
log("unhandled event") |
| 348 |
|
| 349 |
|
| 350 |
if __name__ == '__main__': |
| 351 |
main() |