您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 
 

100 行
3.2 KiB

  1. import json
  2. import os
  3. import re
  4. import shlex
  5. import subprocess
  6. import sys
  7. import urllib.request
  8. from functools import lru_cache
  9. @lru_cache(maxsize=None)
  10. def fetch_pr_data(pr_number, repo, endpoint=""):
  11. api_url = f"https://api.github.com/repos/{repo}/pulls/{pr_number}"
  12. if endpoint:
  13. api_url += f"/{endpoint}"
  14. req = urllib.request.Request(api_url)
  15. res = urllib.request.urlopen(req)
  16. return json.loads(res.read().decode('utf8'))
  17. def get_files_list(pr_number, repo="frappe/frappe"):
  18. return [change["filename"] for change in fetch_pr_data(pr_number, repo, "files")]
  19. def get_output(command, shell=True):
  20. print(command)
  21. command = shlex.split(command)
  22. return subprocess.check_output(command, shell=shell, encoding="utf8").strip()
  23. def has_skip_ci_label(pr_number, repo="frappe/frappe"):
  24. return has_label(pr_number, "Skip CI", repo)
  25. def has_run_server_tests_label(pr_number, repo="frappe/frappe"):
  26. return has_label(pr_number, "Run Server Tests", repo)
  27. def has_run_ui_tests_label(pr_number, repo="frappe/frappe"):
  28. return has_label(pr_number, "Run UI Tests", repo)
  29. def has_label(pr_number, label, repo="frappe/frappe"):
  30. return any([fetched_label["name"] for fetched_label in fetch_pr_data(pr_number, repo)["labels"] if fetched_label["name"] == label])
  31. def is_py(file):
  32. return file.endswith("py")
  33. def is_ci(file):
  34. return ".github" in file
  35. def is_frontend_code(file):
  36. return file.lower().endswith((".css", ".scss", ".less", ".sass", ".styl", ".js", ".ts", ".vue"))
  37. def is_docs(file):
  38. regex = re.compile(r'\.(md|png|jpg|jpeg|csv|svg)$|^.github|LICENSE')
  39. return bool(regex.search(file))
  40. if __name__ == "__main__":
  41. files_list = sys.argv[1:]
  42. build_type = os.environ.get("TYPE")
  43. pr_number = os.environ.get("PR_NUMBER")
  44. repo = os.environ.get("REPO_NAME")
  45. # this is a push build, run all builds
  46. if not pr_number:
  47. os.system('echo "::set-output name=build::strawberry"')
  48. os.system('echo "::set-output name=build-server::strawberry"')
  49. sys.exit(0)
  50. files_list = files_list or get_files_list(pr_number=pr_number, repo=repo)
  51. if not files_list:
  52. print("No files' changes detected. Build is shutting")
  53. sys.exit(0)
  54. ci_files_changed = any(f for f in files_list if is_ci(f))
  55. only_docs_changed = len(list(filter(is_docs, files_list))) == len(files_list)
  56. only_frontend_code_changed = len(list(filter(is_frontend_code, files_list))) == len(files_list)
  57. updated_py_file_count = len(list(filter(is_py, files_list)))
  58. only_py_changed = updated_py_file_count == len(files_list)
  59. if has_skip_ci_label(pr_number, repo):
  60. print("Found `Skip CI` label on pr, stopping build process.")
  61. sys.exit(0)
  62. elif ci_files_changed:
  63. print("CI related files were updated, running all build processes.")
  64. elif only_docs_changed:
  65. print("Only docs were updated, stopping build process.")
  66. sys.exit(0)
  67. elif only_frontend_code_changed and build_type == "server" and not has_run_server_tests_label(pr_number, repo):
  68. print("Only Frontend code was updated; Stopping Python build process.")
  69. sys.exit(0)
  70. elif build_type == "ui" and only_py_changed and not has_run_ui_tests_label(pr_number, repo):
  71. print("Only Python code was updated, stopping Cypress build process.")
  72. sys.exit(0)
  73. os.system('echo "::set-output name=build::strawberry"')