blob: 83346045f89daaf199ba73dd68a155a4b4ab63b1 [file] [log] [blame]
Suraj Shetty0a6770a2020-10-14 10:21:31 +05301import sys
2import requests
3from urllib.parse import urlparse
4
5
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:
39 return any(
40 is_documentation_link(word)
41 for line in body.splitlines()
42 for word in line.split()
43 )
44
45
46def check_pull_request(number: str) -> "tuple[int, str]":
47 response = requests.get(f"https://api.github.com/repos/frappe/erpnext/pulls/{number}")
48 if not response.ok:
49 return 1, "Pull Request Not Found! ⚠️"
50
51 payload = response.json()
52 title = (payload.get("title") or "").lower().strip()
53 head_sha = (payload.get("head") or {}).get("sha")
54 body = (payload.get("body") or "").lower()
55
56 if (
57 not title.startswith("feat")
58 or not head_sha
59 or "no-docs" in body
60 or "backport" in body
61 ):
62 return 0, "Skipping documentation checks... 🏃"
63
64 if contains_documentation_link(body):
65 return 0, "Documentation Link Found. You're Awesome! 🎉"
66
67 return 1, "Documentation Link Not Found! ⚠️"
Suraj Shetty0a6770a2020-10-14 10:21:31 +053068
69
70if __name__ == "__main__":
Raffael Meyerd1550422023-01-24 07:12:44 +010071 exit_code, message = check_pull_request(sys.argv[1])
72 print(message)
73 sys.exit(exit_code)