get_contributors.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from github import Github
  2. from subprocess import check_output
  3. import shlex
  4. from tqdm import tqdm
  5. import click
  6. from collections import defaultdict
  7. @click.command()
  8. @click.option(
  9. "--access-token",
  10. required=True,
  11. help="""
  12. Github Access token that has repo:public_repo and user:read:user permission.
  13. Create them at https://github.com/settings/tokens/new
  14. """,
  15. )
  16. @click.option(
  17. "--prev-release-commit",
  18. required=True,
  19. help="Last commit SHA of the previous release.",
  20. )
  21. @click.option(
  22. "--curr-release-commit",
  23. required=True,
  24. help="Last commit SHA of the current release.",
  25. )
  26. def run(access_token, prev_release_commit, curr_release_commit):
  27. print("Writing commit descriptions to 'commits.txt'...")
  28. check_output(
  29. (
  30. f"git log {prev_release_commit}..{curr_release_commit} "
  31. f"--pretty=format:'%s' > commits.txt"
  32. ),
  33. shell=True,
  34. )
  35. # Generate command
  36. cmd = []
  37. cmd.append(
  38. (
  39. f"git log {prev_release_commit}..{curr_release_commit} "
  40. f'--pretty=format:"%s" '
  41. f' | grep -Eo "#(\d+)"'
  42. )
  43. )
  44. joined = " && ".join(cmd)
  45. cmd = f"bash -c '{joined}'"
  46. cmd = shlex.split(cmd)
  47. print("Executing", cmd)
  48. # Sort the PR numbers
  49. pr_numbers = [int(line.lstrip("#")) for line in check_output(cmd).decode().split()]
  50. print("PR numbers", pr_numbers)
  51. # Use Github API to fetch the
  52. g = Github(access_token)
  53. ray_repo = g.get_repo("ray-project/ray")
  54. logins = set()
  55. for num in tqdm(pr_numbers):
  56. try:
  57. logins.add(ray_repo.get_pull(num).user.login)
  58. except Exception as e:
  59. print(e)
  60. print()
  61. print("Here's the list of contributors")
  62. print("=" * 10)
  63. print()
  64. print("@" + ", @".join(logins))
  65. print()
  66. print("=" * 10)
  67. # Organize commits
  68. NO_CATEGORY = "[NO_CATEGORY]"
  69. def get_category(line):
  70. if line[0] == "[":
  71. return (line.split("]")[0].strip(" ") + "]").upper()
  72. else:
  73. return NO_CATEGORY
  74. commits = defaultdict(list)
  75. with open("commits.txt") as file:
  76. for line in file.readlines():
  77. commits[get_category(line)].append(line.strip())
  78. with open("commits.txt", "a") as file:
  79. for category, commit_msgs in commits.items():
  80. file.write("\n{}\n".format(category))
  81. for commit_msg in commit_msgs:
  82. file.write("{}\n".format(commit_msg))
  83. if __name__ == "__main__":
  84. run()