blob: b4f9c39653c161d290d49c22c79b0c4b9b955831 [file] [log] [blame]
Suraj Shetty0a6770a2020-10-14 10:21:31 +05301import sys
Suraj Shetty0a6770a2020-10-14 10:21:31 +05302from urllib.parse import urlparse
3
Akhil Narang3effaf22024-03-27 11:37:26 +05304import requests
Suraj Shetty0a6770a2020-10-14 10:21:31 +05305
Raffael Meyerd1550422023-01-24 07:12:44 +01006WEBSITE_REPOS = [
Suraj Shetty0a6770a2020-10-14 10:21:31 +05307 "erpnext_com",
8 "frappe_io",
9]
10
Raffael Meyerd1550422023-01-24 07:12:44 +010011DOCUMENTATION_DOMAINS = [
12 "docs.erpnext.com",
13 "frappeframework.com",
14]
Suraj Shetty0a6770a2020-10-14 10:21:31 +053015
Suraj Shetty0a6770a2020-10-14 10:21:31 +053016
Raffael Meyerd1550422023-01-24 07:12:44 +010017def is_valid_url(url: str) -> bool:
18 parts = urlparse(url)
19 return all((parts.scheme, parts.netloc, parts.path))
20
21
22def is_documentation_link(word: str) -> bool:
23 if not word.startswith("http") or not is_valid_url(word):
24 return False
25
26 parsed_url = urlparse(word)
27 if parsed_url.netloc in DOCUMENTATION_DOMAINS:
28 return True
29
30 if parsed_url.netloc == "github.com":
31 parts = parsed_url.path.split("/")
32 if len(parts) == 5 and parts[1] == "frappe" and parts[2] in WEBSITE_REPOS:
33 return True
34
35 return False
36
37
38def contains_documentation_link(body: str) -> bool:
Akhil Narang3effaf22024-03-27 11:37:26 +053039 return any(is_documentation_link(word) for line in body.splitlines() for word in line.split())
Raffael Meyerd1550422023-01-24 07:12:44 +010040
41
42def check_pull_request(number: str) -> "tuple[int, str]":
43 response = requests.get(f"https://api.github.com/repos/frappe/erpnext/pulls/{number}")
44 if not response.ok:
45 return 1, "Pull Request Not Found! ⚠️"
46
47 payload = response.json()
48 title = (payload.get("title") or "").lower().strip()
49 head_sha = (payload.get("head") or {}).get("sha")
50 body = (payload.get("body") or "").lower()
51
Akhil Narang3effaf22024-03-27 11:37:26 +053052 if not title.startswith("feat") or not head_sha or "no-docs" in body or "backport" in body:
Raffael Meyerd1550422023-01-24 07:12:44 +010053 return 0, "Skipping documentation checks... 🏃"
54
55 if contains_documentation_link(body):
56 return 0, "Documentation Link Found. You're Awesome! 🎉"
57
58 return 1, "Documentation Link Not Found! ⚠️"
Suraj Shetty0a6770a2020-10-14 10:21:31 +053059
60
61if __name__ == "__main__":
Raffael Meyerd1550422023-01-24 07:12:44 +010062 exit_code, message = check_pull_request(sys.argv[1])
63 print(message)
64 sys.exit(exit_code)